diff --git "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" index a82936532556f..d17cceeb68727 100644 --- "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" +++ "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" @@ -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:** diff --git "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" index 6946db9116b47..b9a5b6567da63 100644 --- "a/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" +++ "b/lcp/LCP 52. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\346\237\223\350\211\262/README.md" @@ -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` > diff --git "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" index 73f21b1613e1d..5ab67e0378ee7 100644 --- "a/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" +++ "b/lcp/LCP 61. \346\260\224\346\270\251\345\217\230\345\214\226\350\266\213\345\212\277/README.md" @@ -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:** diff --git "a/lcp/LCP 74. \346\234\200\345\274\272\347\245\235\347\246\217\345\212\233\345\234\272/README.md" "b/lcp/LCP 74. \346\234\200\345\274\272\347\245\235\347\246\217\345\212\233\345\234\272/README.md" index 613d29b0bd0d2..c5080773759fa 100644 --- "a/lcp/LCP 74. \346\234\200\345\274\272\347\245\235\347\246\217\345\212\233\345\234\272/README.md" +++ "b/lcp/LCP 74. \346\234\200\345\274\272\347\245\235\347\246\217\345\212\233\345\234\272/README.md" @@ -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} **提示:** diff --git "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" index 4a55116645b3a..e67692c5ad6a1 100644 --- "a/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" +++ "b/lcp/LCP 80. \347\224\237\347\211\251\350\277\233\345\214\226\345\275\225/README.md" @@ -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](){: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](){:width=320px} **示例 2:** diff --git a/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md index a8b41c875be2e..f42a9f5a5bee3 100644 --- a/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md +++ b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README.md @@ -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)$。 @@ -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; +} +``` + diff --git a/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md index a968028ccccbe..4802a90c88f43 100644 --- a/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md +++ b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/README_EN.md @@ -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)$. @@ -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; +} +``` + diff --git a/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/Solution.ts b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/Solution.ts new file mode 100644 index 0000000000000..d013eff84b794 --- /dev/null +++ b/solution/1900-1999/1953.Maximum Number of Weeks for Which You Can Work/Solution.ts @@ -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; +}