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 solutions to lc problem: No.1953 #2819

Merged
merged 1 commit into from
May 16, 2024
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
3 changes: 1 addition & 2 deletions lcp/LCP 34. 二叉树染色/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2034.%20%E4%BA%8C%
>
> 输出:`12`
>
> 解释:`结点 5、3、4 染成蓝色,获得最大的价值 5+3+4=12`
> ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2034.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E6%9F%93%E8%89%B2/images/1616126267-BqaCRj-image.png)
> 解释:`结点 5、3、4 染成蓝色,获得最大的价值 5+3+4=12` > ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2034.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E6%9F%93%E8%89%B2/images/1616126267-BqaCRj-image.png)

**示例 2:**

Expand Down
3 changes: 1 addition & 2 deletions lcp/LCP 52. 二叉搜索树染色/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2052.%20%E4%BA%8C%

**示例 2:**

> 输入:`root = [4,2,7,1,null,5,null,null,null,null,6]`
> `ops = [[0,2,2],[1,1,5],[0,4,5],[1,5,7]]`
> 输入:`root = [4,2,7,1,null,5,null,null,null,null,6]` > `ops = [[0,2,2],[1,1,5],[0,4,5],[1,5,7]]`
>
> 输出:`5`
>
Expand Down
3 changes: 1 addition & 2 deletions lcp/LCP 61. 气温变化趋势/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%
>
> 输出:`2`
>
> 解释:如下表所示, 第 `2~4` 天两地气温变化趋势相同,且持续时间最长,因此返回 `4-2=2`
> ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2061.%20%E6%B0%94%E6%B8%A9%E5%8F%98%E5%8C%96%E8%B6%8B%E5%8A%BF/images/1663902654-hlrSvs-image.png){:width=1000px}
> 解释:如下表所示, 第 `2~4` 天两地气温变化趋势相同,且持续时间最长,因此返回 `4-2=2` > ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2061.%20%E6%B0%94%E6%B8%A9%E5%8F%98%E5%8C%96%E8%B6%8B%E5%8A%BF/images/1663902654-hlrSvs-image.png){:width=1000px}

**示例 2:**

Expand Down
3 changes: 1 addition & 2 deletions lcp/LCP 74. 最强祝福力场/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2074.%20%E6%9C%80%
> 输出:`3`
>
> 解释:如下图所示,
> `forceField[0]、forceField[1]、forceField[3]` 重叠的区域力场强度最大,返回 `3`
> ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2074.%20%E6%9C%80%E5%BC%BA%E7%A5%9D%E7%A6%8F%E5%8A%9B%E5%9C%BA/images/1681805437-HQkyZS-image.png){:width=500px}
> `forceField[0]、forceField[1]、forceField[3]` 重叠的区域力场强度最大,返回 `3` > ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2074.%20%E6%9C%80%E5%BC%BA%E7%A5%9D%E7%A6%8F%E5%8A%9B%E5%9C%BA/images/1681805437-HQkyZS-image.png){:width=500px}

**提示:**

Expand Down
3 changes: 1 addition & 2 deletions lcp/LCP 80. 生物进化录/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2080.%20%E7%94%9F%
> 解释:树结构如下图所示,共存在 2 种记录方案:
> 第 1 种方案为:0(记录编号 1 的节点) -> 1(回退至节点 0) -> 0(记录编号 2 的节点) -> 0((记录编号 3 的节点))
> 第 2 种方案为:0(记录编号 2 的节点) -> 0(记录编号 3 的节点) -> 1(回退至节点 2) -> 1(回退至节点 0) -> 0(记录编号 1 的节点)
> 返回字典序更小的 `"00110"`
> ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2080.%20%E7%94%9F%E7%89%A9%E8%BF%9B%E5%8C%96%E5%BD%95/images/1682319485-cRVudI-image.png){:width=120px}![进化 (3).gif](<https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2080.%20%E7%94%9F%E7%89%A9%E8%BF%9B%E5%8C%96%E5%BD%95/images/1682412701-waHdnm-%E8%BF%9B%E5%8C%96%20(3).gif>){:width=320px}
> 返回字典序更小的 `"00110"` > ![image.png](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2080.%20%E7%94%9F%E7%89%A9%E8%BF%9B%E5%8C%96%E5%BD%95/images/1682319485-cRVudI-image.png){:width=120px}![进化 (3).gif](<https://fastly.jsdelivr.net/gh/doocs/leetcode@main/lcp/LCP%2080.%20%E7%94%9F%E7%89%A9%E8%BF%9B%E5%8C%96%E5%BD%95/images/1682412701-waHdnm-%E8%BF%9B%E5%8C%96%20(3).gif>){:width=320px}

**示例 2:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ tags:

## 解法

### 方法一
### 方法一:贪心

我们考虑什么情况下不能完成所有阶段任务。如果存在一个项目 $i$,它的阶段任务数大于其余所有项目的阶段任务数之和再加 $1$,那么就不能完成所有阶段任务。否则,我们一定可以通过不同项目之间来回穿插的方式完成所有阶段任务。

我们记所有项目的阶段任务数之和为 $s$,最大的阶段任务数为 $mx$,那么其余所有项目的阶段任务数之和为 $rest = s - mx$。

如果 $mx \gt rest + 1$,那么就不能完成所有阶段任务,最多只能完成 $rest \times 2 + 1$ 个阶段任务。否则,我们可以完成所有阶段任务,数量为 $s$。

时间复杂度 $O(n)$,其中 $n$ 为项目数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -129,6 +137,15 @@ func numberOfWeeks(milestones []int) int64 {
}
```

```ts
function numberOfWeeks(milestones: number[]): number {
const mx = Math.max(...milestones);
const s = milestones.reduce((a, b) => a + b, 0);
const rest = s - mx;
return mx > rest + 1 ? rest * 2 + 1 : s;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,15 @@ Thus, one milestone in project 0 will remain unfinished.

## Solutions

### Solution 1
### Solution 1: Greedy

We consider under what circumstances we cannot complete all stage tasks. If there is a project $i$ whose number of stage tasks is greater than the sum of the number of stage tasks of all other projects plus $1$, then we cannot complete all stage tasks. Otherwise, we can definitely complete all stage tasks by interlacing between different projects.

We denote the sum of the number of stage tasks of all projects as $s$, and the maximum number of stage tasks as $mx$, then the sum of the number of stage tasks of all other projects is $rest = s - mx$.

If $mx > rest + 1$, then we cannot complete all stage tasks, and at most we can complete $rest \times 2 + 1$ stage tasks. Otherwise, we can complete all stage tasks, the number is $s$.

The time complexity is $O(n)$, where $n$ is the number of projects. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -126,6 +134,15 @@ func numberOfWeeks(milestones []int) int64 {
}
```

```ts
function numberOfWeeks(milestones: number[]): number {
const mx = Math.max(...milestones);
const s = milestones.reduce((a, b) => a + b, 0);
const rest = s - mx;
return mx > rest + 1 ? rest * 2 + 1 : s;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function numberOfWeeks(milestones: number[]): number {
const mx = Math.max(...milestones);
const s = milestones.reduce((a, b) => a + b, 0);
const rest = s - mx;
return mx > rest + 1 ? rest * 2 + 1 : s;
}
Loading