Skip to content

Commit 9ebcd65

Browse files
committed
May-10
1 parent 40a1329 commit 9ebcd65

11 files changed

+506
-10
lines changed

README.md

Lines changed: 13 additions & 6 deletions
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) | 16/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 113/2209 | 4 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 116/2210 | 5 vips |
5959

60-
**Solved**: 697 problems
60+
**Solved**: 701 problems
6161

6262
## 类型/Category
6363

@@ -252,14 +252,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
252252

253253
## Enumeration
254254

255-
| Link | Problem(8) | Solution | Tag | Time | Space | Ref |
255+
| Link | Problem(9) | Solution | Tag | Time | Space | Ref |
256256
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
257257
| [Leetcode-1534](https://leetcode.com/problems/count-good-triplets/) | Count Good Triplets | [c++](./leetcode/1534.count-good-triplets.cpp), [python3](./leetcode/1534.count-good-triplets.py) | Enumeration | O\(N^3\) | O\(1\) | - |
258258
| [Leetcode-204](https://leetcode.com/problems/count-primes/) | Count Primes | [c++](./leetcode/204.count-primes.cpp), [python3](./leetcode/204.count-primes.py) | Enumeration | O\(NlogN\) | O\(N\) | - |
259259
| [Leetcode-2843](https://leetcode.com/problems/count-symmetric-integers/) | Count Symmetric Integers | [c++](./leetcode/2843.count-symmetric-integers.cpp), [python3](./leetcode/2843.count-symmetric-integers.py) | Enumeration | O\(N\) | O\(1\) | - |
260260
| [Leetcode-3272](https://leetcode.com/problems/find-the-count-of-good-integers/) | Find The Count Of Good Integers | [c++](./leetcode/3272.find-the-count-of-good-integers.cpp), [python3](./leetcode/3272.find-the-count-of-good-integers.py) | Enumeration | O\(10\*\*\(N/2\) \* NlogN\) | O\(10\*\*\(N/2\)\) | - |
261261
| [Leetcode-3411](https://leetcode.com/problems/maximum-subarray-with-equal-products/) | Maximum Subarray With Equal Products | [c++](./leetcode/3411.maximum-subarray-with-equal-products.cpp), [python3](./leetcode/3411.maximum-subarray-with-equal-products.py) | Enumeration | O\(N \* sqrt\(Num\)\) | O\(N\) | - |
262262
| [Leetcode-1863](https://leetcode.com/problems/sum-of-all-subset-xor-totals/) | Sum Of All Subset Xor Totals | [c++](./leetcode/1863.sum-of-all-subset-xor-totals.cpp), [python3](./leetcode/1863.sum-of-all-subset-xor-totals.py) | Enumeration | O\(N\) | O\(1\) | - |
263+
| [Lintcode-918](https://www.lintcode.com/problem/3sum-smaller/) | 3Sum Smaller | [c++](./lintcode/918.3sum-smaller.cpp), [python3](./lintcode/918.3sum-smaller.py) | Enumeration | O\(N^2\) | O\(1\) | Leetcode-259 |
263264
| [Lintcode-366](https://www.lintcode.com/problem/fibonacci/) | Fibonacci | [c++](./lintcode/366.fibonacci.cpp), [python3](./lintcode/366.fibonacci.py) | Enumeration | O\(N\) | O\(N\) | - |
264265
| [Lintcode-640](https://www.lintcode.com/problem/one-edit-distance/) | One Edit Distance | [c++](./lintcode/640.one-edit-distance.cpp), [python3](./lintcode/640.one-edit-distance.py) | Enumeration | O\(N\) | O\(1\) | Leetcode-161 |
265266

@@ -298,7 +299,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
298299

299300
## Greedy
300301

301-
| Link | Problem(47) | Solution | Tag | Time | Space | Ref |
302+
| Link | Problem(49) | Solution | Tag | Time | Space | Ref |
302303
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
303304
| [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\) | - |
304305
| [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\) | - |
@@ -327,6 +328,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
327328
| [Leetcode-1589](https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/) | Maximum Sum Obtained Of Any Permutation | [c++](./leetcode/1589.maximum-sum-obtained-of-any-permutation.cpp), [python3](./leetcode/1589.maximum-sum-obtained-of-any-permutation.py) | Greedy | O\(NlogN\) | O\(N\) | - |
328329
| [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) | Greedy | O\(NlogN\) | O\(1\) | - |
329330
| [Leetcode-1007](https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/) | Minimum Domino Rotations For Equal Row | [c++](./leetcode/1007.minimum-domino-rotations-for-equal-row.cpp), [python3](./leetcode/1007.minimum-domino-rotations-for-equal-row.py) | Greedy | O\(N\) | O\(1\) | - |
331+
| [Leetcode-2918](https://leetcode.com/problems/minimum-equal-sum-of-two-arrays-after-replacing-zeros/) | Minimum Equal Sum Of Two Arrays After Replacing Zeros | [c++](./leetcode/2918.minimum-equal-sum-of-two-arrays-after-replacing-zeros.cpp), [python3](./leetcode/2918.minimum-equal-sum-of-two-arrays-after-replacing-zeros.py) | Greedy | O\(N\) | O\(1\) | - |
330332
| [Leetcode-452](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | Minimum Number Of Arrows To Burst Balloons | [c++](./leetcode/452.minimum-number-of-arrows-to-burst-balloons.cpp), [python3](./leetcode/452.minimum-number-of-arrows-to-burst-balloons.py) | Greedy | O\(NlogN\) | O\(1\) | - |
331333
| [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\) | - |
332334
| [Leetcode-1326](https://leetcode.com/problems/minimum-number-of-taps-to-open-to-water-a-garden/) | Minimum Number Of Taps To Open To Water A Garden | [c++](./leetcode/1326.minimum-number-of-taps-to-open-to-water-a-garden.cpp), [python3](./leetcode/1326.minimum-number-of-taps-to-open-to-water-a-garden.py) | Greedy | O\(N\) | O\(N\) | - |
@@ -347,6 +349,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
347349
| [Lintcode-3672](https://www.lintcode.com/problem/connecting-cities-with-minimum-cost/) | Connecting Cities With Minimum Cost | [c++](./lintcode/3672.connecting-cities-with-minimum-cost.cpp), [python3](./lintcode/3672.connecting-cities-with-minimum-cost.py) | Greedy | O\(ElogE\) | O\(E\+V\) | Leetcode-1135 |
348350
| [Lintcode-861](https://www.lintcode.com/problem/k-empty-slots/) | K Empty Slots | [c++](./lintcode/861.k-empty-slots.cpp), [python3](./lintcode/861.k-empty-slots.py) | Greedy | O\(N\) | O\(N\) | Leetcode-683 |
349351
| [Lintcode-919](https://www.lintcode.com/problem/meeting-rooms-ii/) | Meeting Rooms II | [c++](./lintcode/919.meeting-rooms-ii.cpp), [python3](./lintcode/919.meeting-rooms-ii.py) | Greedy | O\(NlogN\) | O\(N\) | Leetcode-253 |
352+
| [Lintcode-3827](https://www.lintcode.com/problem/merge-operations-to-turn-array-into-a-palindrome/) | Merge Operations To Turn Array Into A Palindrome | [c++](./lintcode/3827.merge-operations-to-turn-array-into-a-palindrome.cpp), [python3](./lintcode/3827.merge-operations-to-turn-array-into-a-palindrome.py) | Greedy | O\(N\) | O\(1\) | - |
350353

351354
## Dynamic Programming
352355

@@ -457,7 +460,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
457460

458461
## Binary Search
459462

460-
| Link | Problem(82) | Solution | Tag | Time | Space | Ref |
463+
| Link | Problem(83) | Solution | Tag | Time | Space | Ref |
461464
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
462465
| [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) | Binary Search | O\(NlogN\) | O\(1\) | - |
463466
| [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\) | - |
@@ -540,6 +543,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
540543
| [Lintcode-617](https://www.lintcode.com/problem/maximum-average-subarray-ii/) | Maximum Average Subarray II | [c++](./lintcode/617.maximum-average-subarray-ii.cpp), [python3](./lintcode/617.maximum-average-subarray-ii.py) | Binary Search | O\(Nlog\(A/ε\)\) | O\(N\) | Leetcode-644 |
541544
| [Lintcode-585](https://www.lintcode.com/problem/maximum-number-in-mountain-sequence/) | Maximum Number In Mountain Sequence | [c++](./lintcode/585.maximum-number-in-mountain-sequence.cpp), [python3](./lintcode/585.maximum-number-in-mountain-sequence.py) | Binary Search | O\(logN\) | O\(1\) | - |
542545
| [Lintcode-600](https://www.lintcode.com/problem/smallest-rectangle-enclosing-black-pixels/) | Smallest Rectangle Enclosing Black Pixels | [c++](./lintcode/600.smallest-rectangle-enclosing-black-pixels.cpp), [python3](./lintcode/600.smallest-rectangle-enclosing-black-pixels.py) | Binary Search | O\(N \* logM \+ M \* logN\) | O\(1\) | Leetcode-302 |
546+
| [Lintcode-3630](https://www.lintcode.com/problem/two-sum-less-than-target/) | Two Sum Less Than Target | [c++](./lintcode/3630.two-sum-less-than-target.cpp), [python3](./lintcode/3630.two-sum-less-than-target.py) | Binary Search | O\(NlogN\) | O\(1\) | Leetcode-1099 |
543547
| [Lintcode-183](https://www.lintcode.com/problem/wood-cut/) | Wood Cut | [c++](./lintcode/183.wood-cut.cpp), [python3](./lintcode/183.wood-cut.py) | Binary Search | O\(NlogA\) | O\(1\) | - |
544548

545549
## Linked List
@@ -867,7 +871,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
867871

868872
## Two Pointers
869873

870-
| Link | Problem(59) | Solution | Tag | Time | Space | Ref |
874+
| Link | Problem(62) | Solution | Tag | Time | Space | Ref |
871875
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
872876
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
873877
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
@@ -922,12 +926,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
922926
| [Leetcode-611](https://leetcode.com/problems/valid-triangle-number/) | Valid Triangle Number | [c++](./leetcode/611.valid-triangle-number.cpp), [python3](./leetcode/611.valid-triangle-number.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
923927
| [Leetcode-2105](https://leetcode.com/problems/watering-plants-ii/) | Watering Plants II | [c++](./leetcode/2105.watering-plants-ii.cpp), [python3](./leetcode/2105.watering-plants-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
924928
| [Leetcode-1712](https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/) | Ways To Split Array Into Three Subarrays | [c++](./leetcode/1712.ways-to-split-array-into-three-subarrays.cpp), [python3](./leetcode/1712.ways-to-split-array-into-three-subarrays.py) | Two Pointers | O\(N\) | O\(N\) | - |
929+
| [Lintcode-918](https://www.lintcode.com/problem/3sum-smaller/) | 3Sum Smaller | [c++](./lintcode/918.3sum-smaller.cpp), [python3](./lintcode/918.3sum-smaller.py) | Two Pointers | O\(N^2\) | O\(1\) | Leetcode-259 |
925930
| [Lintcode-861](https://www.lintcode.com/problem/k-empty-slots/) | K Empty Slots | [c++](./lintcode/861.k-empty-slots.cpp), [python3](./lintcode/861.k-empty-slots.py) | Two Pointers | O\(N\) | O\(N\) | Leetcode-683 |
926931
| [Lintcode-3736](https://www.lintcode.com/problem/kth-smallest-subarray-sum/) | Kth Smallest Subarray Sum | [c++](./lintcode/3736.kth-smallest-subarray-sum.cpp), [python3](./lintcode/3736.kth-smallest-subarray-sum.py) | Two Pointers | O\(NlogA\) | O\(1\) | Leetcode-1918 |
927932
| [Lintcode-386](https://www.lintcode.com/problem/longest-substring-with-at-most-k-distinct-characters/) | Longest Substring With At Most K Distinct Characters | [c++](./lintcode/386.longest-substring-with-at-most-k-distinct-characters.cpp), [python3](./lintcode/386.longest-substring-with-at-most-k-distinct-characters.py) | Two Pointers | O\(N\) | O\(K\) | Leetcode-340 |
928933
| [Lintcode-928](https://www.lintcode.com/problem/longest-substring-with-at-most-two-distinct-characters/) | Longest Substring With At Most Two Distinct Characters | [c++](./lintcode/928.longest-substring-with-at-most-two-distinct-characters.cpp), [python3](./lintcode/928.longest-substring-with-at-most-two-distinct-characters.py) | Two Pointers | O\(N\) | O\(1\) | Leetcode-159 |
929934
| [Lintcode-883](https://www.lintcode.com/problem/max-consecutive-ones-ii/) | Max Consecutive Ones II | [c++](./lintcode/883.max-consecutive-ones-ii.cpp), [python3](./lintcode/883.max-consecutive-ones-ii.py) | Two Pointers | O\(N\) | O\(1\) | Leetcode-487 |
930935
| [Lintcode-3653](https://www.lintcode.com/problem/meeting-scheduler/) | Meeting Scheduler | [c++](./lintcode/3653.meeting-scheduler.cpp), [python3](./lintcode/3653.meeting-scheduler.py) | Two Pointers | O\(N \+ M\) | O\(1\) | Leetcode-1229 |
936+
| [Lintcode-3827](https://www.lintcode.com/problem/merge-operations-to-turn-array-into-a-palindrome/) | Merge Operations To Turn Array Into A Palindrome | [c++](./lintcode/3827.merge-operations-to-turn-array-into-a-palindrome.cpp), [python3](./lintcode/3827.merge-operations-to-turn-array-into-a-palindrome.py) | Two Pointers | O\(N\) | O\(1\) | - |
937+
| [Lintcode-3630](https://www.lintcode.com/problem/two-sum-less-than-target/) | Two Sum Less Than Target | [c++](./lintcode/3630.two-sum-less-than-target.cpp), [python3](./lintcode/3630.two-sum-less-than-target.py) | Two Pointers | O\(NlogN\) | O\(1\) | Leetcode-1099 |
931938

932939
## Sliding Window
933940

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Tag: Array, Greedy
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/o5nHLgCQXG8
7+
8+
// You are given two arrays nums1 and nums2 consisting of positive integers.
9+
// You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.
10+
// Return the minimum equal sum you can obtain, or -1 if it is impossible.
11+
//  
12+
// Example 1:
13+
//
14+
// Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
15+
// Output: 12
16+
// Explanation: We can replace 0's in the following way:
17+
// - Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
18+
// - Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
19+
// Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
20+
//
21+
// Example 2:
22+
//
23+
// Input: nums1 = [2,0,2,0], nums2 = [1,4]
24+
// Output: -1
25+
// Explanation: It is impossible to make the sum of both arrays equal.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= nums1.length, nums2.length <= 105
31+
// 0 <= nums1[i], nums2[i] <= 106
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
long long minSum(vector<int>& nums1, vector<int>& nums2) {
38+
long long zero1 = count(nums1.begin(), nums1.end(), 0);
39+
long long zero2 = count(nums2.begin(), nums2.end(), 0);
40+
long long s1 = accumulate(nums1.begin(), nums1.end(), zero1);
41+
long long s2 = accumulate(nums2.begin(), nums2.end(), zero2);
42+
if ((s1 < s2 && zero1 == 0) || (s2 < s1 && zero2 == 0)) {
43+
return -1;
44+
}
45+
46+
return max(s1, s2);
47+
}
48+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Tag: Array, Greedy
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/o5nHLgCQXG8
7+
8+
# You are given two arrays nums1 and nums2 consisting of positive integers.
9+
# You have to replace all the 0's in both arrays with strictly positive integers such that the sum of elements of both arrays becomes equal.
10+
# Return the minimum equal sum you can obtain, or -1 if it is impossible.
11+
#  
12+
# Example 1:
13+
#
14+
# Input: nums1 = [3,2,0,1,0], nums2 = [6,5,0]
15+
# Output: 12
16+
# Explanation: We can replace 0's in the following way:
17+
# - Replace the two 0's in nums1 with the values 2 and 4. The resulting array is nums1 = [3,2,2,1,4].
18+
# - Replace the 0 in nums2 with the value 1. The resulting array is nums2 = [6,5,1].
19+
# Both arrays have an equal sum of 12. It can be shown that it is the minimum sum we can obtain.
20+
#
21+
# Example 2:
22+
#
23+
# Input: nums1 = [2,0,2,0], nums2 = [1,4]
24+
# Output: -1
25+
# Explanation: It is impossible to make the sum of both arrays equal.
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= nums1.length, nums2.length <= 105
31+
# 0 <= nums1[i], nums2[i] <= 106
32+
#
33+
#
34+
35+
class Solution:
36+
def minSum(self, nums1: List[int], nums2: List[int]) -> int:
37+
zero1 = 0
38+
sum1 = 0
39+
for x in nums1:
40+
zero1 += x == 0
41+
sum1 += x
42+
43+
zero2 = 0
44+
sum2 = 0
45+
for x in nums2:
46+
zero2 += x == 0
47+
sum2 += x
48+
49+
if (sum1 + zero1 < sum2 + zero2 and zero1 == 0) or (sum2 + zero2 < sum1 + zero1 and zero2 == 0):
50+
return -1
51+
return max(sum1 + zero1, sum2 + zero2)
52+

leetcode/360.sort-transformed-array.vip

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Tag: Binary Search, Two Pointers, Array
2+
// Time: O(NlogN)
3+
// Space: O(1)
4+
// Ref: Leetcode-1099
5+
// Note: -
6+
7+
// Given an array of integers `nums` and an integer `target`, if there exists `i < j` such that `nums[i] + nums[j] < target`, then return **the maximum and that satisfy this condition**.
8+
// If no `i, j` satisfying the above condition exists, then `-1` is returned.
9+
//
10+
// Example 1:
11+
// ```
12+
// Input:
13+
// nums = [2, 7, 11, 15], target = 24
14+
// Output:
15+
// 22
16+
// Explanation:
17+
// 7 + 15 = 22 < 24
18+
// 11 + 15 = 26 > 24
19+
// 22 is the maximum sum that satisfies the condition
20+
// ```
21+
//
22+
// Example 2:
23+
// ```
24+
// Input:
25+
// nums = [3, 5, 1, 9, 7], target = 3
26+
// Output:
27+
// -1
28+
// Explanation:
29+
// Cannot find two elements whose sum is less than 3
30+
// ```
31+
//
32+
// $1 \leq nums.length \leq 1000$
33+
// $-1000 \leq nums[i] \leq 1000$
34+
// $-2000 \leq target \leq 2000$
35+
36+
class Solution {
37+
public:
38+
/**
39+
* @param nums: An array of integer
40+
* @param target: An integer
41+
* @return: The sum of two numbers smaller than target
42+
*/
43+
int twoSumLessThanTarget(vector<int> &nums, int target) {
44+
// write your code here
45+
sort(nums.begin(), nums.end());
46+
int l = 0;
47+
int r = nums.size() - 1;
48+
int res = INT_MIN;
49+
while (l < r) {
50+
if (nums[l] + nums[r] < target) {
51+
res = max(res, nums[l] + nums[r]);
52+
l += 1;
53+
} else {
54+
r -= 1;
55+
}
56+
}
57+
return res == INT_MIN ? -1 : res;
58+
}
59+
};

0 commit comments

Comments
 (0)