Let flowerbed be an integer array representing a flowerbed, where 0 denotes an empty plot and 1 denotes a planted plot. Determine whether it is possible to plant n additional flowers in the flowerbed such that no two planted flowers are adjacent.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
Example 2:
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
Constraints:
class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
count = 0
for i in range(len(flowerbed)):
if flowerbed[i] == 0: # Check if the current plot is empty
left_empty = (i == 0) or (flowerbed[i - 1] == 0) # Check left, handle edge
right_empty = (i == len(flowerbed) - 1) or (flowerbed[i + 1] == 0) # Check right, handle edge
if left_empty and right_empty:
flowerbed[i] = 1 # Plant a flower (conceptually - or modify if you need to)
count += 1
return count >= n
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
let count = 0;
for (let i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] === 0) { // Check if the current plot is empty
const leftEmpty = (i === 0) || (flowerbed[i - 1] === 0); // Check left, handle edge
const rightEmpty = (i === flowerbed.length - 1) || (flowerbed[i + 1] === 0); // Check right, handle edge
if (leftEmpty && rightEmpty) {
flowerbed[i] = 1; // Plant a flower (conceptually - or modify if you need to)
count++;
}
}
}
return count >= n;
}
func canPlaceFlowers(flowerbed []int, n int) bool {
count := 0
for i := 0; i < len(flowerbed); i++ {
if flowerbed[i] == 0 { // Check if the current plot is empty
leftEmpty := i == 0 || flowerbed[i-1] == 0 // Check left, handle edge
rightEmpty := i == len(flowerbed)-1 || flowerbed[i+1] == 0 // Check right, handle edge
if leftEmpty && rightEmpty {
flowerbed[i] = 1 // Plant a flower (conceptually - or modify if you need to)
count++
}
}
}
return count >= n
}
The code implements a solution to the “flowerbed planting” problem. Here’s a breakdown of what it does in general terms:
In essence, the code simulates walking along the flowerbed, checking each empty plot to see if it’s a valid place to plant a new flower, and keeping track of how many flowers it can plant. It then compares that number to the desired number of flowers to determine if the planting is possible.
If you liked this content I’d appreciate an upvote or a comment. That helps me improve the quality of my posts as well as getting to know more about you, my dear reader.
Muchas gracias!
Follow me for more content like this.
X | PeakD | Rumble | YouTube | Linked In | GitHub | PayPal.me | Medium
Down below you can find other ways to tip my work.
BankTransfer: "710969000019398639", // CLABE
BAT: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
ETH: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
BTC: "33xxUWU5kjcPk1Kr9ucn9tQXd2DbQ1b9tE",
ADA: "addr1q9l3y73e82hhwfr49eu0fkjw34w9s406wnln7rk9m4ky5fag8akgnwf3y4r2uzqf00rw0pvsucql0pqkzag5n450facq8vwr5e",
DOT: "1rRDzfMLPi88RixTeVc2beA5h2Q3z1K1Uk3kqqyej7nWPNf",
DOGE: "DRph8GEwGccvBWCe4wEQsWsTvQvsEH4QKH",
DAI: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875"