본문 바로가기
Problem Solving/LeetCode

[LeetCode] 605. Can Place Flowers - Java

by graycode 2026. 9. 11.

• 문제 링크

 

Can Place Flowers - LeetCode

Can you solve this real interview question? Can Place Flowers - You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots. Given an integer array flowerbed containing 0's and 1'

leetcode.com

 

• 풀이 코드

public class Solution {

    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int i = 0;
        while (i < flowerbed.length && n > 0) {
            if (flowerbed[i] == 1) i += 2;
            else if (i == flowerbed.length - 1 || flowerbed[i + 1] == 0) {
                i += 2;
                n--;
            } else i += 3;
        }

        return n <= 0;
    }

}

댓글