Skip to content

Commit 268da41

Browse files
committed
Mar-31
1 parent c0a7020 commit 268da41

9 files changed

+371
-40
lines changed

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5656
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5757
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/128 | 3 vips |
58-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 66/2209 | 3 vips |
58+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 68/2209 | 3 vips |
5959

60-
**Solved**: 629 problems
60+
**Solved**: 631 problems
6161

6262
## 类型/Category
6363

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

268268
## Greedy
269269

270-
| Link | Problem(39) | Solution | Tag | Time | Space | Ref |
270+
| Link | Problem(40) | Solution | Tag | Time | Space | Ref |
271271
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
272272
| [Leetcode-870](https://leetcode.com/problems/advantage-shuffle/) | Advantage Shuffle | [c++](./leetcode/870.advantage-shuffle.cpp), [python3](./leetcode/870.advantage-shuffle.py) | Greedy | O\(NlogN\) | O\(N\) | - |
273273
| [Leetcode-2818](https://leetcode.com/problems/apply-operations-to-maximize-score/) | Apply Operations To Maximize Score | [c++](./leetcode/2818.apply-operations-to-maximize-score.cpp), [python3](./leetcode/2818.apply-operations-to-maximize-score.py) | Greedy | O\(\(N \+ K\) \* logN\) | O\(Max\) | - |
@@ -296,6 +296,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
296296
| [Leetcode-1888](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/) | Minimum Number Of Flips To Make The Binary String Alternating | [c++](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.cpp), [python3](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.py) | Greedy | O\(N\) | O\(1\) | - |
297297
| [Leetcode-435](https://leetcode.com/problems/non-overlapping-intervals/) | Non Overlapping Intervals | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python3](./leetcode/435.non-overlapping-intervals.py) | Greedy | O\(NlogN\) | O\(1\) | - |
298298
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Greedy | O\(N\) | O\(N\) | - |
299+
| [Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/) | Put Marbles In Bags | [c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py) | Greedy | O\(N\) | O\(N\) | - |
299300
| [Leetcode-3282](https://leetcode.com/problems/reach-end-of-array-with-max-score/) | Reach End Of Array With Max Score | [c++](./leetcode/3282.reach-end-of-array-with-max-score.cpp), [python3](./leetcode/3282.reach-end-of-array-with-max-score.py) | Greedy | O\(N\) | O\(1\) | - |
300301
| [Leetcode-3439](https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-i/) | Reschedule Meetings For Maximum Free Time I | [c++](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.cpp), [python3](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.py) | Greedy | O\(N\) | O\(N\) | - |
301302
| [Leetcode-632](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/) | Smallest Range Covering Elements From K Lists | [c++](./leetcode/632.smallest-range-covering-elements-from-k-lists.cpp), [python3](./leetcode/632.smallest-range-covering-elements-from-k-lists.py) | Greedy | O\(NlogN\) | O\(N\) | - |
@@ -723,7 +724,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
723724

724725
## Heap
725726

726-
| Link | Problem(35) | Solution | Tag | Time | Space | Ref |
727+
| Link | Problem(36) | Solution | Tag | Time | Space | Ref |
727728
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
728729
| [Leetcode-1094](https://leetcode.com/problems/car-pooling/) | Car Pooling | [c++](./leetcode/1094.car-pooling.cpp), [python3](./leetcode/1094.car-pooling.py) | Heap | O\(NlogN\) | O\(N\) | - |
729730
| [Leetcode-787](https://leetcode.com/problems/cheapest-flights-within-k-stops/) | Cheapest Flights Within K Stops | [c++](./leetcode/787.cheapest-flights-within-k-stops.cpp), [python3](./leetcode/787.cheapest-flights-within-k-stops.py) | Heap | O\(ElogV\) | O\(E\+V\) | - |
@@ -747,6 +748,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
747748
| [Leetcode-743](https://leetcode.com/problems/network-delay-time/) | Network Delay Time | [c++](./leetcode/743.network-delay-time.cpp), [python3](./leetcode/743.network-delay-time.py) | Heap | O\(ElogE\) | O\(E\+V\) | - |
748749
| [Leetcode-1514](https://leetcode.com/problems/path-with-maximum-probability/) | Path With Maximum Probability | [c++](./leetcode/1514.path-with-maximum-probability.cpp), [python3](./leetcode/1514.path-with-maximum-probability.py) | Heap | O\(VlogE\) | O\(V\+E\) | - |
749750
| [Leetcode-1631](https://leetcode.com/problems/path-with-minimum-effort/) | Path With Minimum Effort | [c++](./leetcode/1631.path-with-minimum-effort.cpp), [python3](./leetcode/1631.path-with-minimum-effort.py) | Heap | O\(NMlogNM\) | O\(NM\) | - |
751+
| [Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/) | Put Marbles In Bags | [c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py) | Heap | O\(N\) | O\(N\) | - |
750752
| [Leetcode-882](https://leetcode.com/problems/reachable-nodes-in-subdivided-graph/) | Reachable Nodes In Subdivided Graph | [c++](./leetcode/882.reachable-nodes-in-subdivided-graph.cpp), [python3](./leetcode/882.reachable-nodes-in-subdivided-graph.py) | Heap | O\(ElogE\) | O\(V\+E\) | - |
751753
| [Leetcode-239](https://leetcode.com/problems/sliding-window-maximum/) | Sliding Window Maximum | [c++](./leetcode/239.sliding-window-maximum.cpp), [python3](./leetcode/239.sliding-window-maximum.py) | Heap | O\(N\) | O\(K\) | - |
752754
| [Leetcode-480](https://leetcode.com/problems/sliding-window-median/) | Sliding Window Median | [c++](./leetcode/480.sliding-window-median.cpp), [python3](./leetcode/480.sliding-window-median.py) | Heap | O\(NlogK\) | O\(K\) | - |
@@ -807,14 +809,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
807809

808810
## Sliding Window
809811

810-
| Link | Problem(72) | Solution | Tag | Time | Space | Ref |
812+
| Link | Problem(73) | Solution | Tag | Time | Space | Ref |
811813
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
812814
| [Leetcode-3206](https://leetcode.com/problems/alternating-groups-i/) | Alternating Groups I | [c++](./leetcode/3206.alternating-groups-i.cpp), [python3](./leetcode/3206.alternating-groups-i.py) | Sliding Window | O\(N\) | O\(1\) | - |
813815
| [Leetcode-3208](https://leetcode.com/problems/alternating-groups-ii/) | Alternating Groups II | [c++](./leetcode/3208.alternating-groups-ii.cpp), [python3](./leetcode/3208.alternating-groups-ii.py) | Sliding Window | O\(N\) | O\(1\) | - |
814816
| [Leetcode-2968](https://leetcode.com/problems/apply-operations-to-maximize-frequency-score/) | Apply Operations To Maximize Frequency Score | [c++](./leetcode/2968.apply-operations-to-maximize-frequency-score.cpp), [python3](./leetcode/2968.apply-operations-to-maximize-frequency-score.py) | Sliding Window | O\(NlogN\) | O\(1\) | - |
815817
| [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\) | - |
816818
| [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\) | - |
817819
| [Leetcode-3306](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/) | Count Of Substrings Containing Every Vowel And K Consonants II | [c++](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp), [python3](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py) | Sliding Window | O\(N\) | O\(N\) | - |
820+
| [Leetcode-2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/) | Count Subarrays Where Max Element Appears At Least K Times | [c++](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.cpp), [python3](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.py) | Sliding Window | O\(N\) | O\(1\) | - |
818821
| [Leetcode-1652](https://leetcode.com/problems/defuse-the-bomb/) | Defuse The Bomb | [c++](./leetcode/1652.defuse-the-bomb.cpp), [python3](./leetcode/1652.defuse-the-bomb.py) | Sliding Window | O\(N\) | O\(1\) | - |
819822
| [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) | Sliding Window | O\(N\) | O\(K\) | - |
820823
| [Leetcode-658](https://leetcode.com/problems/find-k-closest-elements/) | Find K Closest Elements | [c++](./leetcode/658.find-k-closest-elements.cpp), [python3](./leetcode/658.find-k-closest-elements.py) | Sliding Window | O\(Log\(N \- K\)\) | O\(1\) | - |
@@ -884,7 +887,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
884887

885888
## Sorting
886889

887-
| Link | Problem(67) | Solution | Tag | Time | Space | Ref |
890+
| Link | Problem(68) | Solution | Tag | Time | Space | Ref |
888891
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
889892
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Sorting | O\(N^2\) | O\(1\) | - |
890893
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Sorting | O\(N^3\) | O\(1\) | - |
@@ -938,6 +941,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
938941
| [Leetcode-1040](https://leetcode.com/problems/moving-stones-until-consecutive-ii/) | Moving Stones Until Consecutive II | [c++](./leetcode/1040.moving-stones-until-consecutive-ii.cpp), [python3](./leetcode/1040.moving-stones-until-consecutive-ii.py) | Sorting | O\(NlogN\) | O\(1\) | - |
939942
| [Leetcode-435](https://leetcode.com/problems/non-overlapping-intervals/) | Non Overlapping Intervals | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python3](./leetcode/435.non-overlapping-intervals.py) | Sorting | O\(NlogN\) | O\(1\) | - |
940943
| [Leetcode-1976](https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/) | Number Of Ways To Arrive At Destination | [c++](./leetcode/1976.number-of-ways-to-arrive-at-destination.cpp), [python3](./leetcode/1976.number-of-ways-to-arrive-at-destination.py) | Sorting | O\(\(E \+ V\)logV\) | O\(E \+ V\) | - |
944+
| [Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/) | Put Marbles In Bags | [c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py) | Sorting | O\(N\) | O\(N\) | - |
941945
| [Leetcode-406](https://leetcode.com/problems/queue-reconstruction-by-height/) | Queue Reconstruction By Height | [c++](./leetcode/406.queue-reconstruction-by-height.cpp), [python3](./leetcode/406.queue-reconstruction-by-height.py) | Sorting | O\(NlogN\) | O\(1\) | - |
942946
| [Leetcode-1288](https://leetcode.com/problems/remove-covered-intervals/) | Remove Covered Intervals | [c++](./leetcode/1288.remove-covered-intervals.cpp), [python3](./leetcode/1288.remove-covered-intervals.py) | Sorting | O\(NlogN\) | O\(1\) | - |
943947
| [Leetcode-354](https://leetcode.com/problems/russian-doll-envelopes/) | Russian Doll Envelopes | [c++](./leetcode/354.russian-doll-envelopes.cpp), [python3](./leetcode/354.russian-doll-envelopes.py) | Sorting | O\(NlogN\) | O\(N\) | - |

leetcode/1358.number-of-substrings-containing-all-three-characters.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,32 @@ class Solution {
5959
}
6060
return res;
6161
}
62+
};
63+
64+
class Solution {
65+
public:
66+
int numberOfSubstrings(string s) {
67+
int n = s.size();
68+
vector<int> counter = vector<int>(3, 0);
69+
int count = 0;
70+
int res = 0;
71+
int i = 0;
72+
for (int j = 0; j < n; j++) {
73+
counter[s[j] - 'a'] += 1;
74+
if (counter[s[j] - 'a'] == 1) {
75+
count += 1;
76+
}
77+
78+
while (count == 3) {
79+
counter[s[i] - 'a'] -= 1;
80+
if (counter[s[i] - 'a'] == 0) {
81+
count -= 1;
82+
}
83+
i += 1;
84+
}
85+
86+
res += i;
87+
}
88+
return res;
89+
}
6290
};

leetcode/1358.number-of-substrings-containing-all-three-characters.py

+19
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ def numberOfSubstrings(self, s: str) -> int:
5151

5252
i += 1
5353

54+
return res
55+
56+
from collections import defaultdict
57+
class Solution:
58+
def numberOfSubstrings(self, s: str) -> int:
59+
n = len(s)
60+
counter = defaultdict(int)
61+
i = 0
62+
res = 0
63+
for j in range(n):
64+
counter[s[j]] += 1
65+
while len(counter) == 3:
66+
counter[s[i]] -= 1
67+
if counter[s[i]] == 0:
68+
del counter[s[i]]
69+
i += 1
70+
71+
res += i
72+
5473
return res

leetcode/2551.put-marbles-in-bags.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Tag: Array, Greedy, Sorting, Heap (Priority Queue)
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/HnwRRXPEmDE
7+
8+
// You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k.
9+
// Divide the marbles into the k bags according to the following rules:
10+
//
11+
// No bag is empty.
12+
// If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag.
13+
// If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] + weights[j].
14+
//
15+
// The score after distributing the marbles is the sum of the costs of all the k bags.
16+
// Return the difference between the maximum and minimum scores among marble distributions.
17+
//  
18+
// Example 1:
19+
//
20+
// Input: weights = [1,3,5,1], k = 2
21+
// Output: 4
22+
// Explanation:
23+
// The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
24+
// The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
25+
// Thus, we return their difference 10 - 6 = 4.
26+
//
27+
// Example 2:
28+
//
29+
// Input: weights = [1, 3], k = 2
30+
// Output: 0
31+
// Explanation: The only distribution possible is [1],[3].
32+
// Since both the maximal and minimal score are the same, we return 0.
33+
//
34+
//  
35+
// Constraints:
36+
//
37+
// 1 <= k <= weights.length <= 105
38+
// 1 <= weights[i] <= 109
39+
//
40+
//
41+
42+
class Solution {
43+
public:
44+
long long putMarbles(vector<int>& weights, int k) {
45+
int n = weights.size();
46+
if (k == n) {
47+
return 0;
48+
}
49+
50+
k = k - 1;
51+
vector<int> splits(n - 1, 0);
52+
for (int i = 0; i < n - 1; i++) {
53+
splits[i] = weights[i] + weights[i + 1];
54+
55+
}
56+
sort(splits.begin(), splits.end());
57+
58+
long long diff = 0;
59+
for (int i = 0; i < k; i++) {
60+
diff += splits[splits.size() - i - 1] - splits[i];
61+
}
62+
63+
return diff;
64+
}
65+
};
66+
67+
class Solution {
68+
public:
69+
long long putMarbles(vector<int>& weights, int k) {
70+
int n = weights.size();
71+
if (k == n) {
72+
return 0;
73+
}
74+
75+
k = k - 1;
76+
vector<int> splits(n - 1, 0);
77+
for (int i = 0; i < n - 1; i++) {
78+
splits[i] = weights[i] + weights[i + 1];
79+
}
80+
81+
long long diff = 0;
82+
nth_element(splits.begin(), splits.begin() + k, splits.end());
83+
for (int i = 0; i < k; i++) {
84+
diff -= splits[i];
85+
}
86+
87+
nth_element(splits.begin(), splits.begin() + splits.size() - k - 1, splits.end());
88+
for (int i = 0; i < k; i++) {
89+
diff += splits[splits.size() - i - 1];
90+
}
91+
92+
return diff;
93+
}
94+
};

leetcode/2551.put-marbles-in-bags.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Tag: Array, Greedy, Sorting, Heap (Priority Queue)
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/HnwRRXPEmDE
7+
8+
# You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k.
9+
# Divide the marbles into the k bags according to the following rules:
10+
#
11+
# No bag is empty.
12+
# If the ith marble and jth marble are in a bag, then all marbles with an index between the ith and jth indices should also be in that same bag.
13+
# If a bag consists of all the marbles with an index from i to j inclusively, then the cost of the bag is weights[i] + weights[j].
14+
#
15+
# The score after distributing the marbles is the sum of the costs of all the k bags.
16+
# Return the difference between the maximum and minimum scores among marble distributions.
17+
#  
18+
# Example 1:
19+
#
20+
# Input: weights = [1,3,5,1], k = 2
21+
# Output: 4
22+
# Explanation:
23+
# The distribution [1],[3,5,1] results in the minimal score of (1+1) + (3+1) = 6.
24+
# The distribution [1,3],[5,1], results in the maximal score of (1+3) + (5+1) = 10.
25+
# Thus, we return their difference 10 - 6 = 4.
26+
#
27+
# Example 2:
28+
#
29+
# Input: weights = [1, 3], k = 2
30+
# Output: 0
31+
# Explanation: The only distribution possible is [1],[3].
32+
# Since both the maximal and minimal score are the same, we return 0.
33+
#
34+
#  
35+
# Constraints:
36+
#
37+
# 1 <= k <= weights.length <= 105
38+
# 1 <= weights[i] <= 109
39+
#
40+
#
41+
42+
class Solution:
43+
def putMarbles(self, weights: List[int], k: int) -> int:
44+
n = len(weights)
45+
if k == n:
46+
return 0
47+
k = k - 1
48+
splits = [weights[i] + weights[i + 1] for i in range(n - 1)]
49+
splits.sort()
50+
diff = 0
51+
for i in range(k):
52+
diff += splits[len(splits) - i - 1] - splits[i]
53+
54+
return diff
55+
56+
import random
57+
class Solution:
58+
def putMarbles(self, weights: List[int], k: int) -> int:
59+
n = len(weights)
60+
if k == n:
61+
return 0
62+
k = k - 1
63+
splits = [weights[i] + weights[i + 1] for i in range(n - 1)]
64+
65+
diff = 0
66+
self.nth_element(splits, 0, len(splits) - 1, k)
67+
for i in range(k):
68+
diff -= splits[i]
69+
70+
self.nth_element(splits, 0, len(splits) - 1, len(splits) - k - 1)
71+
for i in range(k):
72+
diff += splits[len(splits) - i - 1]
73+
74+
return diff
75+
76+
def nth_element(self, splits: list, l: int, r: int, target: int) -> int:
77+
pivot = random.randint(l, r)
78+
splits[pivot], splits[r] = splits[r], splits[pivot]
79+
80+
i = l
81+
j = r - 1
82+
83+
while i <= j:
84+
while i <= j and splits[i] < splits[r]:
85+
i += 1
86+
while i <= j and splits[j] >= splits[r]:
87+
j -= 1
88+
if i <= j:
89+
splits[i], splits[j] = splits[j], splits[i]
90+
i += 1
91+
j -= 1
92+
93+
splits[i], splits[r] = splits[r], splits[i]
94+
95+
if target == i:
96+
return splits[i]
97+
elif target > i:
98+
return self.nth_element(splits, i + 1, r, target)
99+
else:
100+
return self.nth_element(splits, l, i - 1, target)

0 commit comments

Comments
 (0)