From d219e6f9d7aaf12caebf7c1cab1f19a841e31e89 Mon Sep 17 00:00:00 2001 From: loczek <30776250+loczek@users.noreply.github.com> Date: Tue, 1 Nov 2022 20:43:43 +0100 Subject: [PATCH] Create: 605-Can-Place-Flowers.ts --- typescript/605-Can-Place-Flowers.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 typescript/605-Can-Place-Flowers.ts diff --git a/typescript/605-Can-Place-Flowers.ts b/typescript/605-Can-Place-Flowers.ts new file mode 100644 index 000000000..505fe42a3 --- /dev/null +++ b/typescript/605-Can-Place-Flowers.ts @@ -0,0 +1,16 @@ +function canPlaceFlowers(flowerbed: number[], n: number): boolean { + flowerbed = [0, ...flowerbed, 0]; + + for (let i = 1; i < flowerbed.length - 1; i++) { + if ( + flowerbed[i - 1] == 0 && + flowerbed[i + 1] === 0 && + flowerbed[i] === 0 + ) { + flowerbed[i] = 1; + n -= 1; + } + } + + return n <= 0; +}