-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
problem: new problem solution - 605 . Can Place Flowers
- Loading branch information
Showing
3 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
64 changes: 64 additions & 0 deletions
64
problems/algorithms/canPlaceFlowers/CanPlaceFlowers.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Source : https://leetcode.com/problems/can-place-flowers/ | ||
// Author : squxq | ||
// Date : 2023-09-10 | ||
|
||
import { canPlaceFlowers } from "./CanPlaceFlowers"; | ||
|
||
describe("canPlaceFlowers", () => { | ||
it("should return true when there are enough empty spaces to plant all flowers", () => { | ||
expect(canPlaceFlowers([1, 0, 0, 0, 1], 1)).toBe(true); | ||
expect(canPlaceFlowers([0, 0, 0, 0, 0], 3)).toBe(true); | ||
expect(canPlaceFlowers([0, 0, 0, 0], 2)).toBe(true); | ||
expect(canPlaceFlowers([0, 0, 0, 0, 0], 0)).toBe(true); | ||
}); | ||
|
||
it("should return false when there are not enough empty spaces to plant all flowers", () => { | ||
expect(canPlaceFlowers([1, 0, 0, 0, 1], 2)).toBe(false); | ||
expect(canPlaceFlowers([1, 1, 1], 1)).toBe(false); | ||
expect(canPlaceFlowers([0, 0, 1, 0, 0], 2)).toBe(false); | ||
}); | ||
|
||
it("should handle edge cases and constraints", () => { | ||
// Edge cases | ||
expect(canPlaceFlowers([], 0)).toBe(true); // Empty flowerbed | ||
expect(canPlaceFlowers([0], 1)).toBe(true); // One empty space, one flower to plant | ||
expect(canPlaceFlowers([1], 0)).toBe(true); // One flower, no need to plant | ||
|
||
// Constraints | ||
const largeFlowerbed = Array.from({ length: 20000 }, () => 0); // 2 * 10^4 length flowerbed | ||
expect(canPlaceFlowers(largeFlowerbed, 10000)).toBe(true); // Half empty, half flowers | ||
expect(canPlaceFlowers(largeFlowerbed, 20001)).toBe(false); // More flowers than empty spaces | ||
expect(canPlaceFlowers(largeFlowerbed, 0)).toBe(true); // No flowers to plant | ||
}); | ||
|
||
it("should return false when there are no empty spaces and n is greater than 0", () => { | ||
expect(canPlaceFlowers([1, 1, 1, 1, 1], 1)).toBe(false); | ||
expect(canPlaceFlowers([1, 1, 1, 1, 1], 5)).toBe(false); | ||
expect(canPlaceFlowers([0, 1, 0, 1, 0], 2)).toBe(false); | ||
}); | ||
|
||
it("should return true when there are no flowers to plant (n is 0)", () => { | ||
expect(canPlaceFlowers([0, 0, 0, 0, 0], 0)).toBe(true); | ||
expect(canPlaceFlowers([1, 0, 1, 0, 1], 0)).toBe(true); | ||
}); | ||
|
||
it("should return true when there are no flowers and no empty spaces (n is 0)", () => { | ||
expect(canPlaceFlowers([], 0)).toBe(true); | ||
}); | ||
|
||
it("should return true when n is 0 and there are empty spaces between flowers", () => { | ||
expect(canPlaceFlowers([1, 0, 0, 1, 0, 0, 1], 0)).toBe(true); | ||
}); | ||
|
||
it("should handle large flowerbeds with all empty spaces", () => { | ||
const largeEmptyFlowerbed = Array.from({ length: 20000 }, () => 0); // All empty | ||
expect(canPlaceFlowers(largeEmptyFlowerbed, 10000)).toBe(true); // Half empty, half flowers | ||
expect(canPlaceFlowers(largeEmptyFlowerbed, 20000)).toBe(true); // All empty, no flowers to plant | ||
}); | ||
|
||
it("should handle large flowerbeds with all flowers", () => { | ||
const largeFullFlowerbed = Array.from({ length: 20000 }, () => 1); // All flowers | ||
expect(canPlaceFlowers(largeFullFlowerbed, 0)).toBe(true); // No flowers to plant | ||
expect(canPlaceFlowers(largeFullFlowerbed, 1)).toBe(false); // One flower to plant, no space | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Source : https://leetcode.com/problems/can-place-flowers/ | ||
// Author : squxq | ||
// Date : 2023-09-10 | ||
|
||
/***************************************************************************************************** | ||
* | ||
* 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's, where 0 means empty and 1 means not empty, | ||
* and an integer n, return true if n new flowers can be planted in the flowerbed without violating | ||
* the no-adjacent-flowers rule and false otherwise. | ||
* | ||
* 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: | ||
* | ||
* 1 <= flowerbed.length <= 2 * 10^4 | ||
* flowerbed[i] is 0 or 1. | ||
* There are no two adjacent flowers in flowerbed. | ||
* 0 <= n <= flowerbed.length | ||
******************************************************************************************************/ | ||
|
||
export function canPlaceFlowers(flowerbed: number[], n: number): boolean { | ||
// first/second/third submit | ||
|
||
// if (flowerbed.length === 1 && !flowerbed[0]) return true | ||
// let counter: number = 0 | ||
// for (let i: number = 0; i < flowerbed.length - 1; i++) { | ||
// if ((i === 0 || i === flowerbed.length - 2) && !flowerbed[i] && !flowerbed[i + 1]) { | ||
// console.log("here", i) | ||
// counter++ | ||
// continue | ||
// } | ||
// if (!flowerbed[i] && !flowerbed[i + 1] && !flowerbed[i + 2]) { | ||
// console.log("here", i) | ||
// counter++ | ||
// i++ | ||
// } | ||
// } | ||
// return counter >= n | ||
|
||
// fourth submit - logic optimization | ||
let counter: number = 0; | ||
for (let i: number = 0; i < flowerbed.length; i++) { | ||
if ( | ||
(i === 0 || flowerbed[i - 1] === 0) && | ||
flowerbed[i] === 0 && | ||
(i === flowerbed.length - 1 || flowerbed[i + 1] === 0) | ||
) { | ||
flowerbed[i] = 1; | ||
counter++; | ||
} | ||
} | ||
return counter >= n; | ||
} |