[Array / String, Easy] Can Place Flowers

송재호·2025년 3월 9일
0

https://leetcode.com/problems/can-place-flowers/description/?envType=study-plan-v2&envId=leetcode-75

그냥 단순하게 앞뒤만 봐주면 될듯
꽃을 놓을 수 있는 경우 index + 1도 의미가 없기 때문에 i++도 같이 해줌

class Solution {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if (n == 0) return true;

        for (int i=0; i<flowerbed.length; i++) {
            if (i > 0 && flowerbed[i-1] == 1) continue;
            if (i < flowerbed.length -1 && flowerbed[i+1] == 1) continue;
            if (flowerbed[i] == 1) continue;

            flowerbed[i] = 1;
            n--;
            i++;

            if (n == 0) {
                return true;
            }
        }
        return false;
    }
}
profile
식지 않는 감자

0개의 댓글