Skip to content

Commit 5dbbd13

Browse files
authored
feat: add solutions to lc problem: No.1953 (#2819)
No.1953.Maximum Number of Weeks for Which You Can Work
1 parent 9a5717e commit 5dbbd13

File tree

8 files changed

+47
-12
lines changed

8 files changed

+47
-12
lines changed

lcp/LCP 34. 二叉树染色/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2034.%20%E4%BA%8C%
1818
>
1919
> 输出:`12`
2020
>
21-
> 解释:`结点 5、3、4 染成蓝色,获得最大的价值 5+3+4=12`
22-
> ![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)
21+
> 解释:`结点 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)
2322
2423
**示例 2:**
2524

lcp/LCP 52. 二叉搜索树染色/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2052.%20%E4%BA%8C%
3838
3939
**示例 2:**
4040

41-
> 输入:`root = [4,2,7,1,null,5,null,null,null,null,6]`
42-
> `ops = [[0,2,2],[1,1,5],[0,4,5],[1,5,7]]`
41+
> 输入:`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]]`
4342
>
4443
> 输出:`5`
4544
>

lcp/LCP 61. 气温变化趋势/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2061.%20%E6%B0%94%
2828
>
2929
> 输出:`2`
3030
>
31-
> 解释:如下表所示, 第 `2~4` 天两地气温变化趋势相同,且持续时间最长,因此返回 `4-2=2`
32-
> ![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}
31+
> 解释:如下表所示, 第 `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}
3332
3433
**示例 2:**
3534

lcp/LCP 74. 最强祝福力场/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2074.%20%E6%9C%80%
3737
> 输出:`3`
3838
>
3939
> 解释:如下图所示,
40-
> `forceField[0]、forceField[1]、forceField[3]` 重叠的区域力场强度最大,返回 `3`
41-
> ![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}
40+
> `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}
4241
4342
**提示:**
4443

lcp/LCP 80. 生物进化录/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/lcp/LCP%2080.%20%E7%94%9F%
3333
> 解释:树结构如下图所示,共存在 2 种记录方案:
3434
> 第 1 种方案为:0(记录编号 1 的节点) -> 1(回退至节点 0) -> 0(记录编号 2 的节点) -> 0((记录编号 3 的节点))
3535
> 第 2 种方案为:0(记录编号 2 的节点) -> 0(记录编号 3 的节点) -> 1(回退至节点 2) -> 1(回退至节点 0) -> 0(记录编号 1 的节点)
36-
> 返回字典序更小的 `"00110"`
37-
> ![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}
36+
> 返回字典序更小的 `"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}
3837
3938
**示例 2:**
4039

solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ tags:
7575

7676
## 解法
7777

78-
### 方法一
78+
### 方法一:贪心
79+
80+
我们考虑什么情况下不能完成所有阶段任务。如果存在一个项目 $i$,它的阶段任务数大于其余所有项目的阶段任务数之和再加 $1$,那么就不能完成所有阶段任务。否则,我们一定可以通过不同项目之间来回穿插的方式完成所有阶段任务。
81+
82+
我们记所有项目的阶段任务数之和为 $s$,最大的阶段任务数为 $mx$,那么其余所有项目的阶段任务数之和为 $rest = s - mx$。
83+
84+
如果 $mx \gt rest + 1$,那么就不能完成所有阶段任务,最多只能完成 $rest \times 2 + 1$ 个阶段任务。否则,我们可以完成所有阶段任务,数量为 $s$。
85+
86+
时间复杂度 $O(n)$,其中 $n$ 为项目数。空间复杂度 $O(1)$。
7987

8088
<!-- tabs:start -->
8189

@@ -129,6 +137,15 @@ func numberOfWeeks(milestones []int) int64 {
129137
}
130138
```
131139

140+
```ts
141+
function numberOfWeeks(milestones: number[]): number {
142+
const mx = Math.max(...milestones);
143+
const s = milestones.reduce((a, b) => a + b, 0);
144+
const rest = s - mx;
145+
return mx > rest + 1 ? rest * 2 + 1 : s;
146+
}
147+
```
148+
132149
<!-- tabs:end -->
133150

134151
<!-- end -->

solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ Thus, one milestone in project 0 will remain unfinished.
7272

7373
## Solutions
7474

75-
### Solution 1
75+
### Solution 1: Greedy
76+
77+
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.
78+
79+
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$.
80+
81+
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$.
82+
83+
The time complexity is $O(n)$, where $n$ is the number of projects. The space complexity is $O(1)$.
7684

7785
<!-- tabs:start -->
7886

@@ -126,6 +134,15 @@ func numberOfWeeks(milestones []int) int64 {
126134
}
127135
```
128136

137+
```ts
138+
function numberOfWeeks(milestones: number[]): number {
139+
const mx = Math.max(...milestones);
140+
const s = milestones.reduce((a, b) => a + b, 0);
141+
const rest = s - mx;
142+
return mx > rest + 1 ? rest * 2 + 1 : s;
143+
}
144+
```
145+
129146
<!-- tabs:end -->
130147

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

0 commit comments

Comments
 (0)