Skip to content

Commit

Permalink
problem: new problem solution - 605 . Can Place Flowers
Browse files Browse the repository at this point in the history
  • Loading branch information
squxq committed Sep 10, 2023
1 parent cb1eeec commit a6cc5db
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 1768 | [Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | Algorithms | [TypeScript](./problems/algorithms/mergeStringsAlternately/MergeStringsAlternately.ts) | Easy |
| 1071 | [Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/) | Algorithms | [TypeScript](./problems/algorithms/greatestCommonDivisorOfStrings/GreatestCommonDivisorOfStrings.ts) | Easy |
| 1431 | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/) | Algorithms | [TypeScript](./problems/algorithms/kidsWithTheGreatestNumberOfCandies/KidsWithTheGreatestNumberOfCandies.ts) | Easy |
| 605 | [Can Place Flowers](https://leetcode.com/problems/can-place-flowers/) | Algorithms | [TypeScript](./problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
64 changes: 64 additions & 0 deletions problems/algorithms/canPlaceFlowers/CanPlaceFlowers.test.ts
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
});
});
61 changes: 61 additions & 0 deletions problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts
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;
}

0 comments on commit a6cc5db

Please sign in to comment.