Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ts solution to lc problem: No.0605 #1722

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions solution/0600-0699/0605.Can Place Flowers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@

**方法一:贪心**

我们直接遍历数组 $flowerbed$,对于每个位置 $i$,如果 $flowerbed[i]=0$,并且其左右相邻位置都为 $0$,则我们可以在该位置种花,否则不能。最后我们统计可以种下的花的数量,如果其不小于 $n$,则返回 $true$,否则返回 $false$。

时间复杂度 $O(n)$,其中 $n$ 是数组 $flowerbed$ 的长度。我们只需要遍历数组一次。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -123,6 +127,23 @@ func canPlaceFlowers(flowerbed []int, n int) bool {
}
```

### **TypeScript**

```ts
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
const m = flowerbed.length;
for (let i = 0; i < m; ++i) {
const l = i === 0 ? 0 : flowerbed[i - 1];
const r = i === m - 1 ? 0 : flowerbed[i + 1];
if (l + flowerbed[i] + r === 0) {
flowerbed[i] = 1;
--n;
}
}
return n <= 0;
}
```

### **PHP**

```php
Expand Down
23 changes: 23 additions & 0 deletions solution/0600-0699/0605.Can Place Flowers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

## Solutions

**Solution 1: Greedy**

We directly traverse the array $flowerbed$. For each position $i$, if $flowerbed[i]=0$ and its adjacent positions on the left and right are also $0$, then we can plant a flower at this position. Otherwise, we cannot. Finally, we count the number of flowers that can be planted. If it is not less than $n$, we return $true$, otherwise we return $false$.

The time complexity is $O(n)$, where $n$ is the length of the array $flowerbed$. We only need to traverse the array once. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -104,6 +110,23 @@ func canPlaceFlowers(flowerbed []int, n int) bool {
}
```

### **TypeScript**

```ts
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
const m = flowerbed.length;
for (let i = 0; i < m; ++i) {
const l = i === 0 ? 0 : flowerbed[i - 1];
const r = i === m - 1 ? 0 : flowerbed[i + 1];
if (l + flowerbed[i] + r === 0) {
flowerbed[i] = 1;
--n;
}
}
return n <= 0;
}
```

### **PHP**

```php
Expand Down
12 changes: 12 additions & 0 deletions solution/0600-0699/0605.Can Place Flowers/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function canPlaceFlowers(flowerbed: number[], n: number): boolean {
const m = flowerbed.length;
for (let i = 0; i < m; ++i) {
const l = i === 0 ? 0 : flowerbed[i - 1];
const r = i === m - 1 ? 0 : flowerbed[i + 1];
if (l + flowerbed[i] + r === 0) {
flowerbed[i] = 1;
--n;
}
}
return n <= 0;
}