A C# solution.


  • 0
    S
            public bool CanPlaceFlowers(int[] flowerbed, int n)
            {
                if (flowerbed == null || flowerbed.Length == 0)
                {
                    return false;
                }
                var idx = 0;
                while(n > 0 && idx < flowerbed.Length-1)
                {
                    if (flowerbed[idx] != 1)
                    {
                        if (flowerbed[idx+1] == 0 && (idx == 0 ||  flowerbed[idx-1] == 0))
                        {
                            flowerbed[idx] = 1;
                            n--;
                        }
                    }
                    idx++;
                }
                if (n > 0 && flowerbed[idx] == 0 && (idx == 0 || flowerbed[idx - 1] == 0))
                {
                    n--;
                }
                return n == 0;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.