Tuesday, July 4, 2023

Leetcode discuss post | Rewrite | After one downvote

I will quickly rewrite the following post - 

Always give the working solution, more efficient solution first; And then talk about my mistake in the content, and then reflect what I should do better next time. 

C# a solution learned after the contest 

Oct. 21, 2018

926. Flip String to Monotone Increasing

It is a medium level algorithm in weekly contest 107. There are several issues in my 10 minutes reading in the contest, I tried to solve a much hard problem with 0..01..1 concatenated by more 0..01..1. In order to improve my contest performance, I have to work on reading skills and try to avoid misunderstanding the problem. I tried to solve a much hard problem since the problem statement is not so clear with those examples.

I spent time to go over all discuss the first three pages after the contest, and then I wrote one solution based on one sharing.

I think that it is very easy to follow. The final string only has three choices, one is all 0's, second one is all 1's, and the third one is a series of 0 are followed by a series of 1. The last position of 0 can be denoted as variable i, which can be from 0 to n - 1. We can just get the minimum flips by going through all those n options. Hopefully my understand is correct.

The following C# code passes online judge

public class Solution {
    /// <summary>
    /// the idea is based on 
    /// https://leetcode.com/problems/flip-string-to-monotone-increasing/discuss/183862/Count-prefix-ones-and-suffix-zeros
    /// </summary>
    /// <param name="digits"></param>
    /// <returns></returns>
    public int MinFlipsMonoIncr(string digits)
        {
            if (digits == null || digits.Length == 0)
                return 0; 

            var leftOnes   = countOneLeftToRightInclusive(digits);
            var rightZeros = countZeroRightToLeftInclusive(digits);

            // get minimum flips
            var length = digits.Length;
            var minimumFlips = digits.Length; 
            for(int i = 0 ; i < digits.Length; i++)
            {
                var current = rightZeros[i];
                if(i > 0)
                {
                    current += leftOnes[i - 1];
                }
                
                minimumFlips = current < minimumFlips ? current : minimumFlips;
            }

            var lastOne = leftOnes[length - 1];
            minimumFlips = lastOne < minimumFlips ? lastOne : minimumFlips;

            return minimumFlips; 
        }

        private static int[] countOneLeftToRightInclusive(string digits)
        {
            var length = digits.Length;
            var ones = new int[length];

            int count = 0;
            ones[0] = 0; 
            for(int i = 0; i < length; i++)
            {               
                if(digits[i] - '0' == 1)
                {
                    count++;                   
                }

                ones[i] = count; 
            }

            return ones;
        }

        private static int[] countZeroRightToLeftInclusive(string digits)
        {
            var length = digits.Length;
            var zeros = new int[length];

            int count = 0;
            zeros[0] = 0;
            for(int i = length - 1; i >= 0; i--)
            {
                if(digits[i] - '0' == 0)
                {
                    count++;                    
                }

                zeros[i] = count; 
            }

            return zeros; 
        }
    }

No comments:

Post a Comment