Created
June 14, 2025 16:01
-
-
Save psy901/d2654dbfb9239bee2b8246a3664e463d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun canPlaceFlowers(flowerbed: IntArray, n: Int): Boolean { | |
| val length = flowerbed.size | |
| var pos = 0 | |
| var remaining = n | |
| while (pos < length && remaining > 0) { | |
| if (flowerbed[pos] == 1) { | |
| pos += 2 | |
| continue | |
| } | |
| val left = if (pos - 1 < 0) true else flowerbed[pos - 1] == 0 | |
| val right = if (pos >= length - 1) true else flowerbed[pos + 1] == 0 | |
| if (left && right) { | |
| pos += 2 | |
| remaining-- | |
| } else { | |
| pos ++ | |
| } | |
| } | |
| return remaining == 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment