diff --git a/README.md b/README.md index 0e8e75c..6cb1e4e 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.test.ts b/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.test.ts new file mode 100644 index 0000000..b60051f --- /dev/null +++ b/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.test.ts @@ -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 + }); +}); diff --git a/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts b/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts new file mode 100644 index 0000000..c7237e5 --- /dev/null +++ b/problems/algorithms/canPlaceFlowers/CanPlaceFlowers.ts @@ -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; +}