• 문제 링크
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;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 628. Maximum Product of Three Numbers - Java (0) | 2026.09.13 |
|---|---|
| [LeetCode] 617. Merge Two Binary Trees - Java (0) | 2026.09.12 |
| [LeetCode] 599. Minimum Index Sum of Two Lists - Java (0) | 2026.09.10 |
| [LeetCode] 598. Range Addition II - Java (0) | 2026.09.09 |
| [LeetCode] 594. Longest Harmonious Subsequence - Java (0) | 2026.09.08 |
댓글