Java 12 ms


  • 0
    V
        public boolean canPlaceFlowers(int[] fb, int n) 
        {
            int toRet = 0, prevInd = -1, diff, i = 0;
            for(; i < fb.length; i++)
            {
                if(fb[i] == 0)
                    continue;
                diff = i - (prevInd == -1? 0: prevInd);
                diff /= 2;
                toRet += (diff - (prevInd == -1? 0: 1));
                prevInd = i;
            }
            if(prevInd == -1)
                toRet += (i + 1) / 2;
            else
                toRet += (i - 1 - prevInd) / 2;
            return toRet >= n;
        }

Log in to reply
 

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