Skip to content

File tree

4 files changed

+47
-3
lines changed
  • best-time-to-buy-and-sell-stock-with-cooldown
  • form-array-by-concatenating-subarrays-of-another-array
  • minimum-elements-to-add-to-form-a-given-sum

4 files changed

+47
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/form-array-by-concatenating-subarrays-of-another-array/
49+
50+
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-cooldown/
51+
4852
https://leetcode-cn.com/problems/minimum-elements-to-add-to-form-a-given-sum/
4953

5054
https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths-ii/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxProfit(prices: number[]): number {
2+
const n = prices.length;
3+
let r1 = -prices[0];
4+
let r2 = 0;
5+
let r3 = 0;
6+
for (let i = 1; i < n; i++) {
7+
const newR1 = Math.max(r1, r3 - prices[i]);
8+
const newR2 = r1 + prices[i];
9+
const newR3 = Math.max(r3, r2);
10+
r1 = newR1;
11+
r2 = newR2;
12+
r3 = newR3;
13+
}
14+
return Math.max(r3, r2);
15+
}
16+
export default maxProfit;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function canChoose(groups: number[][], nums: number[]): boolean {
2+
let i = 0;
3+
for (let k = 0; k < nums.length && i < groups.length;) {
4+
if (check(groups[i], nums, k)) {
5+
k += groups[i].length;
6+
i++;
7+
} else {
8+
k++;
9+
}
10+
}
11+
return i === groups.length;
12+
}
13+
14+
function check(g: number[], nums: number[], k: number) {
15+
if (k + g.length > nums.length) {
16+
return false;
17+
}
18+
for (let j = 0; j < g.length; j++) {
19+
if (g[j] !== nums[k + j]) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
export default canChoose;
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
function minElements(nums: number[], limit: number, goal: number): number {
2-
3-
return Math.ceil(Math.abs(goal - nums.reduce((a, b) => a + b)) / limit);
2+
return Math.ceil(Math.abs(goal - nums.reduce((a, b) => a + b)) / limit);
43
}
5-
export default minElements
4+
export default minElements;

0 commit comments

Comments
 (0)