Skip to content

Commit 4eda83a

Browse files
committed
Mar-6
1 parent 764f4cb commit 4eda83a

7 files changed

+360
-45
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5353
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5454
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
5555
| [🔲] | [9c-top.md](./list/9c-top.md) | 50/56 | 2 vips |
56-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 46/2208 | 3 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 47/2208 | 3 vips |
5757

58-
**Solved**: 573 problems
58+
**Solved**: 575 problems
5959

6060
## 类型/Category
6161

@@ -100,7 +100,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
100100

101101
## Math
102102

103-
| Link | Problem(33) | Solution | Tag | Time | Space | Ref |
103+
| Link | Problem(34) | Solution | Tag | Time | Space | Ref |
104104
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
105105
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
106106
| [Leetcode-504](https://leetcode.com/problems/base-7/) | Base 7 | [c++](./leetcode/504.base-7.cpp), [python3](./leetcode/504.base-7.py) | Math | O\(N\) | O\(1\) | - |
@@ -117,6 +117,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
117117
| [Leetcode-168](https://leetcode.com/problems/excel-sheet-column-title/) | Excel Sheet Column Title | [c++](./leetcode/168.excel-sheet-column-title.cpp), [python3](./leetcode/168.excel-sheet-column-title.py) | Math | O\(logN\) | O\(1\) | - |
118118
| [Leetcode-282](https://leetcode.com/problems/expression-add-operators/) | Expression Add Operators | [c++](./leetcode/282.expression-add-operators.cpp), [python3](./leetcode/282.expression-add-operators.py) | Math | O\(2^N\) | O\(N\) | - |
119119
| [Leetcode-172](https://leetcode.com/problems/factorial-trailing-zeroes/) | Factorial Trailing Zeroes | [c++](./leetcode/172.factorial-trailing-zeroes.cpp), [python3](./leetcode/172.factorial-trailing-zeroes.py) | Math | O\(logN\) | O\(1\) | - |
120+
| [Leetcode-2965](https://leetcode.com/problems/find-missing-and-repeated-values/) | Find Missing And Repeated Values | [c++](./leetcode/2965.find-missing-and-repeated-values.cpp), [python3](./leetcode/2965.find-missing-and-repeated-values.py) | Math | O\(N^2\) | O\(N^2\) | - |
120121
| [Leetcode-2269](https://leetcode.com/problems/find-the-k-beauty-of-a-number/) | Find The K Beauty Of A Number | [c++](./leetcode/2269.find-the-k-beauty-of-a-number.cpp), [python3](./leetcode/2269.find-the-k-beauty-of-a-number.py) | Math | O\(N\) | O\(N\) | - |
121122
| [Leetcode-2698](https://leetcode.com/problems/find-the-punishment-number-of-an-integer/) | Find The Punishment Number Of An Integer | [c++](./leetcode/2698.find-the-punishment-number-of-an-integer.cpp), [python3](./leetcode/2698.find-the-punishment-number-of-an-integer.py) | Math | O\(N \* 2^K\) | O\(K\) | - |
122123
| [Leetcode-202](https://leetcode.com/problems/happy-number/) | Happy Number | [c++](./leetcode/202.happy-number.cpp), [python3](./leetcode/202.happy-number.py) | Math | O\(S\) | O\(1\) | - |
@@ -379,7 +380,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
379380

380381
## Binary Search
381382

382-
| Link | Problem(54) | Solution | Tag | Time | Space | Ref |
383+
| Link | Problem(55) | Solution | Tag | Time | Space | Ref |
383384
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
384385
| [Leetcode-704](https://leetcode.com/problems/binary-search/) | Binary Search | [c++](./leetcode/704.binary-search.cpp), [python3](./leetcode/704.binary-search.py) | Binary Search | O\(logN\) | O\(1\) | - |
385386
| [Leetcode-3048](https://leetcode.com/problems/earliest-second-to-mark-indices-i/) | Earliest Second To Mark Indices I | [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp), [python3](./leetcode/3048.earliest-second-to-mark-indices-i.py) | Binary Search | O\(NlogN\) | O\(N\) | - |
@@ -404,6 +405,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
404405
| [Leetcode-2024](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | Maximize The Confusion Of An Exam | [c++](./leetcode/2024.maximize-the-confusion-of-an-exam.cpp), [python3](./leetcode/2024.maximize-the-confusion-of-an-exam.py) | Binary Search | O\(N\) | O\(1\) | - |
405406
| [Leetcode-2779](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/) | Maximum Beauty Of An Array After Applying Operation | [c++](./leetcode/2779.maximum-beauty-of-an-array-after-applying-operation.cpp), [python3](./leetcode/2779.maximum-beauty-of-an-array-after-applying-operation.py) | Binary Search | O\(NlogN\) | O\(1\) | - |
406407
| [Leetcode-2529](https://leetcode.com/problems/maximum-count-of-positive-integer-and-negative-integer/) | Maximum Count Of Positive Integer And Negative Integer | [c++](./leetcode/2529.maximum-count-of-positive-integer-and-negative-integer.cpp), [python3](./leetcode/2529.maximum-count-of-positive-integer-and-negative-integer.py) | Binary Search | O\(logN\) | O\(1\) | - |
408+
| [Leetcode-2106](https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | Maximum Fruits Harvested After At Most K Steps | [c++](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.cpp), [python3](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.py) | Binary Search | O\(N\) | O\(1\) | - |
407409
| [Leetcode-718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/) | Maximum Length Of Repeated Subarray | [c++](./leetcode/718.maximum-length-of-repeated-subarray.cpp), [python3](./leetcode/718.maximum-length-of-repeated-subarray.py) | Binary Search | O\(NM\) | O\(NM\) | - |
408410
| [Leetcode-1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) | Maximum Profit In Job Scheduling | [c++](./leetcode/1235.maximum-profit-in-job-scheduling.cpp), [python3](./leetcode/1235.maximum-profit-in-job-scheduling.py) | Binary Search | O\(NlogN\) | O\(N\) | - |
409411
| [Leetcode-2271](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/) | Maximum White Tiles Covered By A Carpet | [c++](./leetcode/2271.maximum-white-tiles-covered-by-a-carpet.cpp), [python3](./leetcode/2271.maximum-white-tiles-covered-by-a-carpet.py) | Binary Search | O\(NlogN\) | O\(1\) | - |
@@ -463,7 +465,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
463465

464466
## Hash Table
465467

466-
| Link | Problem(87) | Solution | Tag | Time | Space | Ref |
468+
| Link | Problem(88) | Solution | Tag | Time | Space | Ref |
467469
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
468470
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
469471
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -482,6 +484,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
482484
| [Leetcode-438](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | Find All Anagrams In A String | [c++](./leetcode/438.find-all-anagrams-in-a-string.cpp), [python3](./leetcode/438.find-all-anagrams-in-a-string.py) | Hash Table | O\(N\) | O\(K\) | - |
483485
| [Leetcode-448](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | Find All Numbers Disappeared In An Array | [c++](./leetcode/448.find-all-numbers-disappeared-in-an-array.cpp), [python3](./leetcode/448.find-all-numbers-disappeared-in-an-array.py) | Hash Table | O\(N\) | O\(1\) | - |
484486
| [Leetcode-1261](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/) | Find Elements In A Contaminated Binary Tree | [c++](./leetcode/1261.find-elements-in-a-contaminated-binary-tree.cpp), [python3](./leetcode/1261.find-elements-in-a-contaminated-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
487+
| [Leetcode-2965](https://leetcode.com/problems/find-missing-and-repeated-values/) | Find Missing And Repeated Values | [c++](./leetcode/2965.find-missing-and-repeated-values.cpp), [python3](./leetcode/2965.find-missing-and-repeated-values.py) | Hash Table | O\(N^2\) | O\(N^2\) | - |
485488
| [Leetcode-2831](https://leetcode.com/problems/find-the-longest-equal-subarray/) | Find The Longest Equal Subarray | [c++](./leetcode/2831.find-the-longest-equal-subarray.cpp), [python3](./leetcode/2831.find-the-longest-equal-subarray.py) | Hash Table | O\(N\) | O\(N\) | - |
486489
| [Leetcode-3160](https://leetcode.com/problems/find-the-number-of-distinct-colors-among-the-balls/) | Find The Number Of Distinct Colors Among The Balls | [c++](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.cpp), [python3](./leetcode/3160.find-the-number-of-distinct-colors-among-the-balls.py) | Hash Table | O\(N\) | O\(N\) | - |
487490
| [Leetcode-1980](https://leetcode.com/problems/find-unique-binary-string/) | Find Unique Binary String | [c++](./leetcode/1980.find-unique-binary-string.cpp), [python3](./leetcode/1980.find-unique-binary-string.py) | Hash Table | O\(N\) | O\(1\) | - |
@@ -563,13 +566,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
563566

564567
## Prefix Sum
565568

566-
| Link | Problem(19) | Solution | Tag | Time | Space | Ref |
569+
| Link | Problem(20) | Solution | Tag | Time | Space | Ref |
567570
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
568571
| [Leetcode-525](https://leetcode.com/problems/contiguous-array/) | Contiguous Array | [c++](./leetcode/525.contiguous-array.cpp), [python3](./leetcode/525.contiguous-array.py) | Prefix Sum | O\(N\) | O\(N\) | - |
569572
| [Leetcode-1838](https://leetcode.com/problems/frequency-of-the-most-frequent-element/) | Frequency Of The Most Frequent Element | [c++](./leetcode/1838.frequency-of-the-most-frequent-element.cpp), [python3](./leetcode/1838.frequency-of-the-most-frequent-element.py) | Prefix Sum | O\(NlogN\) | O\(1\) | - |
570573
| [Leetcode-1208](https://leetcode.com/problems/get-equal-substrings-within-budget/) | Get Equal Substrings Within Budget | [c++](./leetcode/1208.get-equal-substrings-within-budget.cpp), [python3](./leetcode/1208.get-equal-substrings-within-budget.py) | Prefix Sum | O\(N\) | O\(1\) | - |
571574
| [Leetcode-1004](https://leetcode.com/problems/max-consecutive-ones-iii/) | Max Consecutive Ones III | [c++](./leetcode/1004.max-consecutive-ones-iii.cpp), [python3](./leetcode/1004.max-consecutive-ones-iii.py) | Prefix Sum | O\(N\) | O\(1\) | - |
572575
| [Leetcode-2024](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/) | Maximize The Confusion Of An Exam | [c++](./leetcode/2024.maximize-the-confusion-of-an-exam.cpp), [python3](./leetcode/2024.maximize-the-confusion-of-an-exam.py) | Prefix Sum | O\(N\) | O\(1\) | - |
576+
| [Leetcode-2106](https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | Maximum Fruits Harvested After At Most K Steps | [c++](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.cpp), [python3](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.py) | Prefix Sum | O\(N\) | O\(1\) | - |
573577
| [Leetcode-1423](https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/) | Maximum Points You Can Obtain From Cards | [c++](./leetcode/1423.maximum-points-you-can-obtain-from-cards.cpp), [python3](./leetcode/1423.maximum-points-you-can-obtain-from-cards.py) | Prefix Sum | O\(N\) | O\(1\) | - |
574578
| [Leetcode-2271](https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/) | Maximum White Tiles Covered By A Carpet | [c++](./leetcode/2271.maximum-white-tiles-covered-by-a-carpet.cpp), [python3](./leetcode/2271.maximum-white-tiles-covered-by-a-carpet.py) | Prefix Sum | O\(NlogN\) | O\(1\) | - |
575579
| [Leetcode-1658](https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/) | Minimum Operations To Reduce X To Zero | [c++](./leetcode/1658.minimum-operations-to-reduce-x-to-zero.cpp), [python3](./leetcode/1658.minimum-operations-to-reduce-x-to-zero.py) | Prefix Sum | O\(N\) | O\(1\) | - |
@@ -721,7 +725,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
721725

722726
## Sliding Window
723727

724-
| Link | Problem(53) | Solution | Tag | Time | Space | Ref |
728+
| Link | Problem(54) | Solution | Tag | Time | Space | Ref |
725729
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
726730
| [Leetcode-220](https://leetcode.com/problems/contains-duplicate-iii/) | Contains Duplicate III | [c++](./leetcode/220.contains-duplicate-iii.cpp), [python3](./leetcode/220.contains-duplicate-iii.py) | Sliding Window | O\(N\) | O\(N\) | - |
727731
| [Leetcode-2953](https://leetcode.com/problems/count-complete-substrings/) | Count Complete Substrings | [c++](./leetcode/2953.count-complete-substrings.cpp), [python3](./leetcode/2953.count-complete-substrings.py) | Sliding Window | O\(N\) | O\(N\) | - |
@@ -747,6 +751,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
747751
| [Leetcode-643](https://leetcode.com/problems/maximum-average-subarray-i/) | Maximum Average Subarray I | [c++](./leetcode/643.maximum-average-subarray-i.cpp), [python3](./leetcode/643.maximum-average-subarray-i.py) | Sliding Window | O\(N\) | O\(1\) | - |
748752
| [Leetcode-2779](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/) | Maximum Beauty Of An Array After Applying Operation | [c++](./leetcode/2779.maximum-beauty-of-an-array-after-applying-operation.cpp), [python3](./leetcode/2779.maximum-beauty-of-an-array-after-applying-operation.py) | Sliding Window | O\(NlogN\) | O\(1\) | - |
749753
| [Leetcode-1695](https://leetcode.com/problems/maximum-erasure-value/) | Maximum Erasure Value | [c++](./leetcode/1695.maximum-erasure-value.cpp), [python3](./leetcode/1695.maximum-erasure-value.py) | Sliding Window | O\(N\) | O\(N\) | - |
754+
| [Leetcode-2106](https://leetcode.com/problems/maximum-fruits-harvested-after-at-most-k-steps/) | Maximum Fruits Harvested After At Most K Steps | [c++](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.cpp), [python3](./leetcode/2106.maximum-fruits-harvested-after-at-most-k-steps.py) | Sliding Window | O\(N\) | O\(1\) | - |
750755
| [Leetcode-718](https://leetcode.com/problems/maximum-length-of-repeated-subarray/) | Maximum Length Of Repeated Subarray | [c++](./leetcode/718.maximum-length-of-repeated-subarray.cpp), [python3](./leetcode/718.maximum-length-of-repeated-subarray.py) | Sliding Window | O\(NM\) | O\(NM\) | - |
751756
| [Leetcode-3090](https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/) | Maximum Length Substring With Two Occurrences | [c++](./leetcode/3090.maximum-length-substring-with-two-occurrences.cpp), [python3](./leetcode/3090.maximum-length-substring-with-two-occurrences.py) | Sliding Window | O\(N\) | O\(N\) | - |
752757
| [Leetcode-1297](https://leetcode.com/problems/maximum-number-of-occurrences-of-a-substring/) | Maximum Number Of Occurrences Of A Substring | [c++](./leetcode/1297.maximum-number-of-occurrences-of-a-substring.cpp), [python3](./leetcode/1297.maximum-number-of-occurrences-of-a-substring.py) | Sliding Window | O\(N\) | O\(N^2\) | - |
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Tag: Array, Binary Search, Sliding Window, Prefix Sum
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Fruits are available at some positions on an infinite x-axis. You are given a 2D integer array fruits where fruits[i] = [positioni, amounti] depicts amounti fruits at the position positioni. fruits is already sorted by positioni in ascending order, and each positioni is unique.
8+
// You are also given an integer startPos and an integer k. Initially, you are at the position startPos. From any position, you can either walk to the left or right. It takes one step to move one unit on the x-axis, and you can walk at most k steps in total. For every position you reach, you harvest all the fruits at that position, and the fruits will disappear from that position.
9+
// Return the maximum total number of fruits you can harvest.
10+
//  
11+
// Example 1:
12+
//
13+
//
14+
// Input: fruits = [[2,8],[6,3],[8,6]], startPos = 5, k = 4
15+
// Output: 9
16+
// Explanation:
17+
// The optimal way is to:
18+
// - Move right to position 6 and harvest 3 fruits
19+
// - Move right to position 8 and harvest 6 fruits
20+
// You moved 3 steps and harvested 3 + 6 = 9 fruits in total.
21+
//
22+
// Example 2:
23+
//
24+
//
25+
// Input: fruits = [[0,9],[4,1],[5,7],[6,2],[7,4],[10,9]], startPos = 5, k = 4
26+
// Output: 14
27+
// Explanation:
28+
// You can move at most k = 4 steps, so you cannot reach position 0 nor 10.
29+
// The optimal way is to:
30+
// - Harvest the 7 fruits at the starting position 5
31+
// - Move left to position 4 and harvest 1 fruit
32+
// - Move right to position 6 and harvest 2 fruits
33+
// - Move right to position 7 and harvest 4 fruits
34+
// You moved 1 + 3 = 4 steps and harvested 7 + 1 + 2 + 4 = 14 fruits in total.
35+
//
36+
// Example 3:
37+
//
38+
//
39+
// Input: fruits = [[0,3],[6,4],[8,5]], startPos = 3, k = 2
40+
// Output: 0
41+
// Explanation:
42+
// You can move at most k = 2 steps and cannot reach any position with fruits.
43+
//
44+
//  
45+
// Constraints:
46+
//
47+
// 1 <= fruits.length <= 105
48+
// fruits[i].length == 2
49+
// 0 <= startPos, positioni <= 2 * 105
50+
// positioni-1 < positioni for any i > 0 (0-indexed)
51+
// 1 <= amounti <= 104
52+
// 0 <= k <= 2 * 105
53+
//
54+
//
55+
56+
class Solution {
57+
public:
58+
int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {
59+
int n = fruits.size();
60+
int l = lower_bound(fruits.begin(), fruits.end(), vector<int>{startPos - k}, [](const vector<int> &a, const vector<int> &b) {
61+
return a[0] < b[0];
62+
}) - fruits.begin();
63+
int r = l;
64+
int pick = 0;
65+
int res = 0;
66+
while (r < n && fruits[r][0] <= startPos + k) {
67+
pick += fruits[r][1];
68+
while (min((fruits[r][0] - startPos) * 2 + (startPos - fruits[l][0]), (startPos - fruits[l][0]) * 2 + (fruits[r][0] - startPos)) > k) {
69+
pick -= fruits[l][1];
70+
l += 1;
71+
}
72+
res = max(res, pick);
73+
r += 1;
74+
}
75+
return res;
76+
}
77+
78+
int pickFruits(vector<vector<int>>& fruits, vector<int>& prefix, int l, int r) {
79+
auto i = lower_bound(fruits.begin(), fruits.end(), vector<int>{l}, [](const vector<int>& a, const vector<int>& b) {
80+
return a[0] < b[0];
81+
}) - fruits.begin();
82+
83+
auto j = upper_bound(fruits.begin(), fruits.end(), vector<int>{r}, [](const vector<int>& a, const vector<int>& b) {
84+
return a[0] < b[0];
85+
}) - fruits.begin() - 1;
86+
87+
return (i <= j) ? (prefix[j + 1] - prefix[i]) : 0;
88+
}
89+
};
90+
91+
class Solution {
92+
public:
93+
int maxTotalFruits(vector<vector<int>>& fruits, int startPos, int k) {
94+
vector<int> prefix = {0};
95+
for (auto& fruit : fruits) {
96+
prefix.push_back(prefix.back() + fruit[1]);
97+
}
98+
99+
int res = 0;
100+
for (int d = 0; d <= k / 2; d++) {
101+
int r = startPos + d;
102+
int l = startPos - (k - d * 2);
103+
res = max(res, pickFruits(fruits, prefix, l, r));
104+
105+
l = startPos - d;
106+
r = startPos + (k - d * 2);
107+
res = max(res, pickFruits(fruits, prefix, l, r));
108+
}
109+
110+
return res;
111+
}
112+
113+
int pickFruits(vector<vector<int>>& fruits, vector<int>& prefix, int l, int r) {
114+
auto i = lower_bound(fruits.begin(), fruits.end(), vector<int>{l}, [](const vector<int>& a, const vector<int>& b) {
115+
return a[0] < b[0];
116+
}) - fruits.begin();
117+
118+
auto j = upper_bound(fruits.begin(), fruits.end(), vector<int>{r}, [](const vector<int>& a, const vector<int>& b) {
119+
return a[0] < b[0];
120+
}) - fruits.begin() - 1;
121+
122+
return (i <= j) ? (prefix[j + 1] - prefix[i]) : 0;
123+
}
124+
};

0 commit comments

Comments
 (0)