Skip to content

Commit d814bd9

Browse files
author
Shuo
committed
A: Path Sum III
1 parent ed1b98c commit d814bd9

File tree

23 files changed

+393
-61
lines changed

23 files changed

+393
-61
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ LeetCode Problems' Solutions
6262

6363
| # | Title | Solution | Difficulty |
6464
| :-: | - | - | :-: |
65+
| <span id="1425">1425</span> | [Constrained Subset Sum](https://leetcode.com/problems/constrained-subset-sum "带限制的子序列和") | [Go](problems/constrained-subset-sum) | Hard |
66+
| <span id="1424">1424</span> | [Diagonal Traverse II](https://leetcode.com/problems/diagonal-traverse-ii "对角线遍历 II") | [Go](problems/diagonal-traverse-ii) | Medium |
67+
| <span id="1423">1423</span> | [Maximum Points You Can Obtain from Cards](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards "可获得的最大点数") | [Go](problems/maximum-points-you-can-obtain-from-cards) | Medium |
68+
| <span id="1422">1422</span> | [Maximum Score After Splitting a String](https://leetcode.com/problems/maximum-score-after-splitting-a-string "分割字符串的最大得分") | [Go](problems/maximum-score-after-splitting-a-string) | Easy |
69+
| <span id="1421">1421</span> | [NPV Queries](https://leetcode.com/problems/npv-queries) 🔒 | [MySQL](problems/npv-queries) | Medium |
6570
| <span id="1420">1420</span> | [Build Array Where You Can Find The Maximum Exactly K Comparisons](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons "生成数组") | [Go](problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons) | Hard |
6671
| <span id="1419">1419</span> | [Minimum Number of Frogs Croaking](https://leetcode.com/problems/minimum-number-of-frogs-croaking "数青蛙") | [Go](problems/minimum-number-of-frogs-croaking) | Medium |
6772
| <span id="1418">1418</span> | [Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant "点菜展示表") | [Go](problems/display-table-of-food-orders-in-a-restaurant) | Medium |

problems/binary-tree-vertical-order-traversal/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@
9090
</pre>
9191

9292
### Related Topics
93-
[[Hash Table](../../tag/hash-table/README.md)]
93+
[[Depth-first Search](../../tag/depth-first-search/README.md)]
94+
[[Breadth-first Search](../../tag/breadth-first-search/README.md)]
9495

9596
### Similar Questions
9697
1. [Binary Tree Level Order Traversal](../binary-tree-level-order-traversal) (Medium)

problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
[< Previous](../minimum-number-of-frogs-croaking "Minimum Number of Frogs Croaking")
99

10-
Next >
10+
[Next >](../npv-queries "NPV Queries")
1111

1212
## [1420. Build Array Where You Can Find The Maximum Exactly K Comparisons (Hard)](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons "生成数组")
1313

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../diagonal-traverse-ii "Diagonal Traverse II")
9+
                
10+
Next >
11+
12+
## [1425. Constrained Subset Sum (Hard)](https://leetcode.com/problems/constrained-subset-sum "带限制的子序列和")
13+
14+
<p>Given an integer array&nbsp;<code>nums</code>&nbsp;and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subset of that array such that for every&nbsp;two <strong>consecutive</strong> integers in the subset,&nbsp;<code>nums[i]</code>&nbsp;and&nbsp;<code>nums[j]</code>, where&nbsp;<code>i &lt; j</code>, the condition&nbsp;<code>j - i &lt;= k</code>&nbsp;is satisfied.</p>
15+
16+
<p>A&nbsp;<em>subset</em>&nbsp;of an array is&nbsp;obtained by deleting some number of elements (can be&nbsp;zero) from the array, leaving the remaining elements in their original order.</p>
17+
18+
<p>&nbsp;</p>
19+
<p><strong>Example 1:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> nums = [10,2,-10,5,20], k = 2
23+
<strong>Output:</strong> 37
24+
<b>Explanation:</b> The subset is [10, 2, 5, 20].
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
31+
<strong>Output:</strong> -1
32+
<b>Explanation:</b> The subset must be non-empty, so we choose the largest number.
33+
</pre>
34+
35+
<p><strong>Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> nums = [10,-2,-10,-5,20], k = 2
39+
<strong>Output:</strong> 23
40+
<b>Explanation:</b> The subset is [10, -2, -5, 20].
41+
</pre>
42+
43+
<p>&nbsp;</p>
44+
<p><strong>Constraints:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= k &lt;= nums.length &lt;= 10^5</code></li>
48+
<li><code>-10^4&nbsp;&lt;= nums[i] &lt;= 10^4</code></li>
49+
</ul>
50+
51+
### Related Topics
52+
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
53+
54+
### Hints
55+
<details>
56+
<summary>Hint 1</summary>
57+
Use dynamic programming.
58+
</details>
59+
60+
<details>
61+
<summary>Hint 2</summary>
62+
Let dp[i] be the solution for the prefix of the array that ends at index i, if the element at index i is in the subset.
63+
</details>
64+
65+
<details>
66+
<summary>Hint 3</summary>
67+
dp[i] = nums[i] + max(0, dp[i-k], dp[i-k+1], ..., dp[i-1])
68+
</details>
69+
70+
<details>
71+
<summary>Hint 4</summary>
72+
Use a heap with the sliding window technique to optimize the dp.
73+
</details>

problems/design-underground-system/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838

3939
<p>&nbsp;</p>
4040
<p><strong>Example 1:</strong></p>
41-
<strong>Input</strong>
4241

4342
<pre>
43+
<strong>Input</strong>
4444
[&quot;UndergroundSystem&quot;,&quot;checkIn&quot;,&quot;checkIn&quot;,&quot;checkIn&quot;,&quot;checkOut&quot;,&quot;checkOut&quot;,&quot;checkOut&quot;,&quot;getAverageTime&quot;,&quot;getAverageTime&quot;,&quot;checkIn&quot;,&quot;getAverageTime&quot;,&quot;checkOut&quot;,&quot;getAverageTime&quot;]
4545
[[],[45,&quot;Leyton&quot;,3],[32,&quot;Paradise&quot;,8],[27,&quot;Leyton&quot;,10],[45,&quot;Waterloo&quot;,15],[27,&quot;Waterloo&quot;,20],[32,&quot;Cambridge&quot;,22],[&quot;Paradise&quot;,&quot;Cambridge&quot;],[&quot;Leyton&quot;,&quot;Waterloo&quot;],[10,&quot;Leyton&quot;,24],[&quot;Leyton&quot;,&quot;Waterloo&quot;],[10,&quot;Waterloo&quot;,38],[&quot;Leyton&quot;,&quot;Waterloo&quot;]]
4646

@@ -64,9 +64,9 @@ undergroundSystem.getAverageTime(&quot;Leyton&quot;, &quot;Waterloo&quot;); &nbs
6464
</pre>
6565

6666
<p><strong>Example 2:</strong></p>
67-
<strong>Input</strong>
6867

6968
<pre>
69+
<strong>Input</strong>
7070
[&quot;UndergroundSystem&quot;,&quot;checkIn&quot;,&quot;checkOut&quot;,&quot;getAverageTime&quot;,&quot;checkIn&quot;,&quot;checkOut&quot;,&quot;getAverageTime&quot;,&quot;checkIn&quot;,&quot;checkOut&quot;,&quot;getAverageTime&quot;]
7171
[[],[10,&quot;Leyton&quot;,3],[10,&quot;Paradise&quot;,8],[&quot;Leyton&quot;,&quot;Paradise&quot;],[5,&quot;Leyton&quot;,10],[5,&quot;Paradise&quot;,16],[&quot;Leyton&quot;,&quot;Paradise&quot;],[2,&quot;Leyton&quot;,21],[2,&quot;Paradise&quot;,30],[&quot;Leyton&quot;,&quot;Paradise&quot;]]
7272

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../maximum-points-you-can-obtain-from-cards "Maximum Points You Can Obtain from Cards")
9+
                
10+
[Next >](../constrained-subset-sum "Constrained Subset Sum")
11+
12+
## [1424. Diagonal Traverse II (Medium)](https://leetcode.com/problems/diagonal-traverse-ii "对角线遍历 II")
13+
14+
Given a list of lists of integers,&nbsp;<code>nums</code>,&nbsp;return all elements of <code>nums</code> in diagonal order as shown in the below images.
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
18+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width: 158px; height: 143px;" /></strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [[1,2,3],[4,5,6],[7,8,9]]
22+
<strong>Output:</strong> [1,4,2,7,5,3,8,6,9]
23+
</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
27+
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png" style="width: 230px; height: 177px;" /></strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
31+
<strong>Output:</strong> [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
32+
</pre>
33+
34+
<p><strong>Example 3:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
38+
<strong>Output:</strong> [1,4,2,5,3,8,6,9,7,10,11]
39+
</pre>
40+
41+
<p><strong>Example 4:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> nums = [[1,2,3,4,5,6]]
45+
<strong>Output:</strong> [1,2,3,4,5,6]
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>1 &lt;= nums.length &lt;= 10^5</code></li>
53+
<li><code>1 &lt;= nums[i].length &lt;=&nbsp;10^5</code></li>
54+
<li><code>1 &lt;= nums[i][j] &lt;= 10^9</code></li>
55+
<li>There at most <code>10^5</code> elements in <code>nums</code>.</li>
56+
</ul>
57+
58+
### Related Topics
59+
[[Sort](../../tag/sort/README.md)]
60+
[[Array](../../tag/array/README.md)]
61+
62+
### Hints
63+
<details>
64+
<summary>Hint 1</summary>
65+
Notice that numbers with equal sums of row and column indexes belong to the same diagonal.
66+
</details>
67+
68+
<details>
69+
<summary>Hint 2</summary>
70+
Store them in tuples (sum, row, val), sort them, and then regroup the answer.
71+
</details>

problems/long-pressed-name/README.md

+16-34
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,45 @@
1616
<p>You examine the <code>typed</code>&nbsp;characters of the keyboard.&nbsp; Return <code>True</code> if it is possible that it was your friends name, with some characters (possibly none) being long pressed.</p>
1717

1818
<p>&nbsp;</p>
19-
2019
<p><strong>Example 1:</strong></p>
2120

2221
<pre>
23-
<strong>Input: </strong>name = <span id="example-input-1-1">&quot;alex&quot;</span>, typed = <span id="example-input-1-2">&quot;aaleex&quot;</span>
24-
<strong>Output: </strong><span id="example-output-1">true</span>
25-
<strong>Explanation: </strong>'a' and 'e' in 'alex' were long pressed.
22+
<strong>Input:</strong> name = &quot;alex&quot;, typed = &quot;aaleex&quot;
23+
<strong>Output:</strong> true
24+
<strong>Explanation: </strong>&#39;a&#39; and &#39;e&#39; in &#39;alex&#39; were long pressed.
2625
</pre>
2726

28-
<div>
2927
<p><strong>Example 2:</strong></p>
3028

3129
<pre>
32-
<strong>Input: </strong>name = <span id="example-input-2-1">&quot;saeed&quot;</span>, typed = <span id="example-input-2-2">&quot;ssaaedd&quot;</span>
33-
<strong>Output: </strong><span id="example-output-2">false</span>
34-
<strong>Explanation: </strong>'e' must have been pressed twice, but it wasn't in the typed output.
30+
<strong>Input:</strong> name = &quot;saeed&quot;, typed = &quot;ssaaedd&quot;
31+
<strong>Output:</strong> false
32+
<strong>Explanation: </strong>&#39;e&#39; must have been pressed twice, but it wasn&#39;t in the typed output.
3533
</pre>
3634

37-
<div>
3835
<p><strong>Example 3:</strong></p>
3936

4037
<pre>
41-
<strong>Input: </strong>name = <span id="example-input-3-1">&quot;leelee&quot;</span>, typed = <span id="example-input-3-2">&quot;lleeelee&quot;</span>
42-
<strong>Output: </strong><span id="example-output-3">true</span>
38+
<strong>Input:</strong> name = &quot;leelee&quot;, typed = &quot;lleeelee&quot;
39+
<strong>Output:</strong> true
4340
</pre>
4441

45-
<div>
4642
<p><strong>Example 4:</strong></p>
4743

4844
<pre>
49-
<strong>Input: </strong>name = <span id="example-input-4-1">&quot;laiden&quot;</span>, typed = <span id="example-input-4-2">&quot;laiden&quot;</span>
50-
<strong>Output: </strong><span id="example-output-4">true</span>
51-
<strong>Explanation: </strong>It's not necessary to long press any character.
45+
<strong>Input:</strong> name = &quot;laiden&quot;, typed = &quot;laiden&quot;
46+
<strong>Output:</strong> true
47+
<strong>Explanation: </strong>It&#39;s not necessary to long press any character.
5248
</pre>
5349

5450
<p>&nbsp;</p>
55-
</div>
56-
</div>
57-
</div>
58-
59-
<p><strong>Note:</strong></p>
51+
<p><strong>Constraints:</strong></p>
6052

61-
<ol>
62-
<li><code>name.length &lt;= 1000</code></li>
63-
<li><code>typed.length &lt;= 1000</code></li>
53+
<ul>
54+
<li><code>1 &lt;= name.length &lt;= 1000</code></li>
55+
<li><code>1 &lt;= typed.length &lt;= 1000</code></li>
6456
<li>The characters of <code>name</code> and <code>typed</code> are lowercase letters.</li>
65-
</ol>
66-
67-
<div>
68-
<p>&nbsp;</p>
69-
70-
<div>
71-
<div>
72-
<div>&nbsp;</div>
73-
</div>
74-
</div>
75-
</div>
57+
</ul>
7658

7759
### Related Topics
7860
[[Two Pointers](../../tag/two-pointers/README.md)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!--|This file generated by command(leetcode description); DO NOT EDIT. |-->
2+
<!--+----------------------------------------------------------------------+-->
3+
<!--|@author openset <[email protected]> |-->
4+
<!--|@link https://github.com/openset |-->
5+
<!--|@home https://github.com/openset/leetcode |-->
6+
<!--+----------------------------------------------------------------------+-->
7+
8+
[< Previous](../maximum-score-after-splitting-a-string "Maximum Score After Splitting a String")
9+
                
10+
[Next >](../diagonal-traverse-ii "Diagonal Traverse II")
11+
12+
## [1423. Maximum Points You Can Obtain from Cards (Medium)](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards "可获得的最大点数")
13+
14+
<p>There are several cards&nbsp;<strong>arranged in a row</strong>, and each card has an associated number of points&nbsp;The points are given in the integer array&nbsp;<code>cardPoints</code>.</p>
15+
16+
<p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>
17+
18+
<p>Your score is the sum of the points of the cards you have taken.</p>
19+
20+
<p>Given the integer array <code>cardPoints</code> and the integer <code>k</code>, return the <em>maximum score</em> you can obtain.</p>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Example 1:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> cardPoints = [1,2,3,4,5,6,1], k = 3
27+
<strong>Output:</strong> 12
28+
<strong>Explanation:</strong> After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
29+
</pre>
30+
31+
<p><strong>Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> cardPoints = [2,2,2], k = 2
35+
<strong>Output:</strong> 4
36+
<strong>Explanation:</strong> Regardless of which two cards you take, your score will always be 4.
37+
</pre>
38+
39+
<p><strong>Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> cardPoints = [9,7,7,9,7,7,9], k = 7
43+
<strong>Output:</strong> 55
44+
<strong>Explanation:</strong> You have to take all the cards. Your score is the sum of points of all cards.
45+
</pre>
46+
47+
<p><strong>Example 4:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong> cardPoints = [1,1000,1], k = 1
51+
<strong>Output:</strong> 1
52+
<strong>Explanation:</strong> You cannot take the card in the middle. Your best score is 1.
53+
</pre>
54+
55+
<p><strong>Example 5:</strong></p>
56+
57+
<pre>
58+
<strong>Input:</strong> cardPoints = [1,79,80,1,1,1,200,1], k = 3
59+
<strong>Output:</strong> 202
60+
</pre>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>1 &lt;= cardPoints.length &lt;= 10^5</code></li>
67+
<li><code>1 &lt;= cardPoints[i] &lt;= 10^4</code></li>
68+
<li><code>1 &lt;= k &lt;= cardPoints.length</code></li>
69+
</ul>
70+
71+
### Related Topics
72+
[[Array](../../tag/array/README.md)]
73+
[[Dynamic Programming](../../tag/dynamic-programming/README.md)]
74+
[[Sliding Window](../../tag/sliding-window/README.md)]
75+
76+
### Hints
77+
<details>
78+
<summary>Hint 1</summary>
79+
Let the sum of all points be total_pts. You need to remove a sub-array from cardPoints with length n - k.
80+
</details>
81+
82+
<details>
83+
<summary>Hint 2</summary>
84+
Keep a window of size n - k over the array. The answer is max(answer, total_pts - sumOfCurrentWindow)
85+
</details>

0 commit comments

Comments
 (0)