Skip to content

Commit edc012b

Browse files
committed
Mar-12
1 parent a6425d0 commit edc012b

9 files changed

+331
-70
lines changed

README.md

Lines changed: 12 additions & 8 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
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5454
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5555
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
56-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 51/2208 | 3 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 53/2208 | 3 vips |
5757

58-
**Solved**: 585 problems
58+
**Solved**: 586 problems
5959

6060
## 类型/Category
6161

@@ -127,7 +127,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
127127
| [Leetcode-973](https://leetcode.com/problems/k-closest-points-to-origin/) | K Closest Points To Origin | [c++](./leetcode/973.k-closest-points-to-origin.cpp), [python3](./leetcode/973.k-closest-points-to-origin.py) | Math | O\(NlogN\) | O\(K\) | - |
128128
| [Leetcode-149](https://leetcode.com/problems/max-points-on-a-line/) | Max Points On A Line | [c++](./leetcode/149.max-points-on-a-line.cpp), [python3](./leetcode/149.max-points-on-a-line.py) | Math | O\(N^2\) | O\(N\) | - |
129129
| [Leetcode-1610](https://leetcode.com/problems/maximum-number-of-visible-points/) | Maximum Number Of Visible Points | [c++](./leetcode/1610.maximum-number-of-visible-points.cpp), [python3](./leetcode/1610.maximum-number-of-visible-points.py) | Math | O\(NlogN\) | O\(N\) | - |
130-
| [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) | Math | \- | \- | - |
130+
| [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) | Math | O\(N \* sqrt\(Num\)\) | O\(N\) | - |
131131
| [Leetcode-462](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | Minimum Moves To Equal Array Elements II | [c++](./leetcode/462.minimum-moves-to-equal-array-elements-ii.cpp), [python3](./leetcode/462.minimum-moves-to-equal-array-elements-ii.py) | Math | O\(NlogN\) | O\(1\) | - |
132132
| [Leetcode-43](https://leetcode.com/problems/multiply-strings/) | Multiply Strings | [c++](./leetcode/43.multiply-strings.cpp), [python3](./leetcode/43.multiply-strings.py) | Math | O\(NM\) | O\(N\+M\) | - |
133133
| [Leetcode-326](https://leetcode.com/problems/power-of-three/) | Power Of Three | [c++](./leetcode/326.power-of-three.cpp), [python3](./leetcode/326.power-of-three.py) | Math | O\(1\) | O\(1\) | - |
@@ -387,8 +387,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
387387

388388
## Binary Search
389389

390-
| Link | Problem(57) | Solution | Tag | Time | Space | Ref |
390+
| Link | Problem(58) | Solution | Tag | Time | Space | Ref |
391391
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
392+
| [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\) | - |
392393
| [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\) | - |
393394
| [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\) | - |
394395
| [Leetcode-1901](https://leetcode.com/problems/find-a-peak-element-ii/) | Find A Peak Element II | [c++](./leetcode/1901.find-a-peak-element-ii.cpp), [python3](./leetcode/1901.find-a-peak-element-ii.py) | Binary Search | O\(N \* logM\) | O\(1\) | - |
@@ -580,8 +581,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
580581

581582
## Prefix Sum
582583

583-
| Link | Problem(20) | Solution | Tag | Time | Space | Ref |
584+
| Link | Problem(21) | Solution | Tag | Time | Space | Ref |
584585
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
586+
| [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) | Prefix Sum | O\(NlogN\) | O\(1\) | - |
585587
| [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\) | - |
586588
| [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\) | - |
587589
| [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\) | - |
@@ -739,10 +741,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
739741

740742
## Sliding Window
741743

742-
| Link | Problem(63) | Solution | Tag | Time | Space | Ref |
744+
| Link | Problem(64) | Solution | Tag | Time | Space | Ref |
743745
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
744746
| [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\) | - |
745747
| [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\) | - |
748+
| [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\) | - |
746749
| [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\) | - |
747750
| [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\) | - |
748751
| [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\) | - |
@@ -777,7 +780,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
777780
| [Leetcode-1610](https://leetcode.com/problems/maximum-number-of-visible-points/) | Maximum Number Of Visible Points | [c++](./leetcode/1610.maximum-number-of-visible-points.cpp), [python3](./leetcode/1610.maximum-number-of-visible-points.py) | Sliding Window | O\(NlogN\) | O\(N\) | - |
778781
| [Leetcode-1456](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) | Maximum Number Of Vowels In A Substring Of Given Length | [c++](./leetcode/1456.maximum-number-of-vowels-in-a-substring-of-given-length.cpp), [python3](./leetcode/1456.maximum-number-of-vowels-in-a-substring-of-given-length.py) | Sliding Window | O\(N\) | O\(1\) | - |
779782
| [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) | Sliding Window | O\(N\) | O\(1\) | - |
780-
| [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) | Sliding Window | \- | \- | - |
783+
| [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) | Sliding Window | O\(N \* sqrt\(Num\)\) | O\(N\) | - |
781784
| [Leetcode-2841](https://leetcode.com/problems/maximum-sum-of-almost-unique-subarray/) | Maximum Sum Of Almost Unique Subarray | [c++](./leetcode/2841.maximum-sum-of-almost-unique-subarray.cpp), [python3](./leetcode/2841.maximum-sum-of-almost-unique-subarray.py) | Sliding Window | O\(N\) | O\(N\) | - |
782785
| [Leetcode-2461](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | Maximum Sum Of Distinct Subarrays With Length K | [c++](./leetcode/2461.maximum-sum-of-distinct-subarrays-with-length-k.cpp), [python3](./leetcode/2461.maximum-sum-of-distinct-subarrays-with-length-k.py) | Sliding Window | O\(N\) | O\(N\) | - |
783786
| [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) | Sliding Window | O\(NlogN\) | O\(1\) | - |
@@ -807,12 +810,13 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
807810

808811
## Sorting
809812

810-
| Link | Problem(49) | Solution | Tag | Time | Space | Ref |
813+
| Link | Problem(50) | Solution | Tag | Time | Space | Ref |
811814
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
812815
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Sorting | O\(N^2\) | O\(1\) | - |
813816
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Sorting | O\(N^3\) | O\(1\) | - |
814817
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Sorting | O\(MlogM\) | O\(N\) | - |
815818
| [Leetcode-870](https://leetcode.com/problems/advantage-shuffle/) | Advantage Shuffle | [c++](./leetcode/870.advantage-shuffle.cpp), [python3](./leetcode/870.advantage-shuffle.py) | Sorting | O\(NlogN\) | O\(N\) | - |
819+
| [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) | Sorting | O\(NlogN\) | O\(1\) | - |
816820
| [Leetcode-455](https://leetcode.com/problems/assign-cookies/) | Assign Cookies | [c++](./leetcode/455.assign-cookies.cpp), [python3](./leetcode/455.assign-cookies.py) | Sorting | O\(NlogN\) | O\(1\) | - |
817821
| [Leetcode-853](https://leetcode.com/problems/car-fleet/) | Car Fleet | [c++](./leetcode/853.car-fleet.cpp), [python3](./leetcode/853.car-fleet.py) | Sorting | O\(NlogN\) | O\(N\) | - |
818822
| [Leetcode-217](https://leetcode.com/problems/contains-duplicate/) | Contains Duplicate | [c++](./leetcode/217.contains-duplicate.cpp), [python3](./leetcode/217.contains-duplicate.py) | Sorting | O\(N\) | O\(N\) | - |

leetcode/2529.maximum-count-of-positive-integer-and-negative-integer.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(1)
44
// Ref: -
55
// Note: std::lower_bound
6+
// Video: https://youtu.be/W2a6_PNNN0g
67

78
// Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
89
//
@@ -42,6 +43,35 @@
4243
class Solution {
4344
public:
4445
int maximumCount(vector<int>& nums) {
45-
46+
int n = nums.size();
47+
int pos = upper_bound(nums.begin(), nums.end(), 0) - nums.begin();
48+
int neg = lower_bound(nums.begin(), nums.end(), 0) - nums.begin();
49+
50+
return max(n - pos, neg);
51+
}
52+
};
53+
54+
class Solution {
55+
public:
56+
int maximumCount(vector<int>& nums) {
57+
int n = nums.size();
58+
int pos = lower_bound(nums, 1);
59+
int neg = lower_bound(nums, 0);
60+
61+
return max(n - pos, neg);
62+
}
63+
64+
int lower_bound(vector<int>& nums, int target) {
65+
int l = 0;
66+
int r = nums.size();
67+
while (l < r) {
68+
int mid = l + (r - l) / 2;
69+
if (nums[mid] >= target) {
70+
r = mid;
71+
} else {
72+
l = mid + 1;
73+
}
74+
}
75+
return l;
4676
}
47-
};
77+
};

leetcode/2529.maximum-count-of-positive-integer-and-negative-integer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Space: O(1)
44
# Ref: -
55
# Note: std::lower_bound
6+
# Video: https://youtu.be/W2a6_PNNN0g
67

78
# Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
89
#
@@ -39,6 +40,15 @@
3940
# Follow up: Can you solve the problem in O(log(n)) time complexity?
4041
#
4142

43+
import bisect
44+
class Solution:
45+
def maximumCount(self, nums: List[int]) -> int:
46+
n = len(nums)
47+
pos = bisect.bisect_right(nums, 0)
48+
neg = bisect.bisect_left(nums, 0)
49+
50+
return max(n - pos, neg)
51+
4252
class Solution(object):
4353
def maximumCount(self, nums):
4454
"""
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Tag: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
2+
// Time: O(NlogN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed integer array nums and an integer k.
8+
// You can perform the following operation on the array at most k times:
9+
//
10+
// Choose any index i from the array and increase or decrease nums[i] by 1.
11+
//
12+
// The score of the final array is the frequency of the most frequent element in the array.
13+
// Return the maximum score you can achieve.
14+
// The frequency of an element is the number of occurences of that element in the array.
15+
//  
16+
// Example 1:
17+
//
18+
// Input: nums = [1,2,6,4], k = 3
19+
// Output: 3
20+
// Explanation: We can do the following operations on the array:
21+
// - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
22+
// - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
23+
// - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
24+
// The element 2 is the most frequent in the final array so our score is 3.
25+
// It can be shown that we cannot achieve a better score.
26+
//
27+
// Example 2:
28+
//
29+
// Input: nums = [1,4,4,2,4], k = 0
30+
// Output: 3
31+
// Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
32+
//
33+
//  
34+
// Constraints:
35+
//
36+
// 1 <= nums.length <= 105
37+
// 1 <= nums[i] <= 109
38+
// 0 <= k <= 1014
39+
//
40+
//
41+
42+
class Solution {
43+
public:
44+
int maxFrequencyScore(vector<int>& nums, long long k) {
45+
int n = nums.size();
46+
sort(nums.begin(), nums.end());
47+
vector<long long> prefix = {0};
48+
for (auto x: nums) {
49+
prefix.push_back(prefix.back() + x);
50+
}
51+
52+
int l = 1;
53+
int r = n;
54+
while (l < r) {
55+
int m = l + (r - l + 1) / 2;
56+
if (fit(prefix, m, k)) {
57+
l = m;
58+
} else {
59+
r = m - 1;
60+
}
61+
}
62+
return l;
63+
}
64+
bool fit(vector<long long>& prefix, long long m, long long k) {
65+
int n = prefix.size() - 1;
66+
for (int i = 0; i <= n - m; i++) {
67+
if ((prefix[i + m] - prefix[i + m / 2]) - (prefix[i + (m + 1) / 2] - prefix[i]) <= k) {
68+
return true;
69+
}
70+
}
71+
72+
return false;
73+
}
74+
};
75+
76+
class Solution {
77+
public:
78+
int maxFrequencyScore(vector<int>& nums, long long k) {
79+
int n = nums.size();
80+
sort(nums.begin(), nums.end());
81+
int i = 0;
82+
int res = 0;
83+
long long op = 0;
84+
for (int j = 0; j < n; j++) {
85+
op += nums[j] - nums[(i + j) / 2];
86+
while (op > k) {
87+
op += nums[i] - nums[(i + j + 1) / 2];
88+
i += 1;
89+
}
90+
res = max(res, j - i + 1);
91+
}
92+
return res;
93+
}
94+
};
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Tag: Array, Binary Search, Sliding Window, Sorting, Prefix Sum
2+
# Time: O(NlogN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a 0-indexed integer array nums and an integer k.
8+
# You can perform the following operation on the array at most k times:
9+
#
10+
# Choose any index i from the array and increase or decrease nums[i] by 1.
11+
#
12+
# The score of the final array is the frequency of the most frequent element in the array.
13+
# Return the maximum score you can achieve.
14+
# The frequency of an element is the number of occurences of that element in the array.
15+
#  
16+
# Example 1:
17+
#
18+
# Input: nums = [1,2,6,4], k = 3
19+
# Output: 3
20+
# Explanation: We can do the following operations on the array:
21+
# - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
22+
# - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
23+
# - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
24+
# The element 2 is the most frequent in the final array so our score is 3.
25+
# It can be shown that we cannot achieve a better score.
26+
#
27+
# Example 2:
28+
#
29+
# Input: nums = [1,4,4,2,4], k = 0
30+
# Output: 3
31+
# Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
32+
#
33+
#  
34+
# Constraints:
35+
#
36+
# 1 <= nums.length <= 105
37+
# 1 <= nums[i] <= 109
38+
# 0 <= k <= 1014
39+
#
40+
#
41+
42+
class Solution:
43+
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
44+
n = len(nums)
45+
nums.sort()
46+
prefix = [0]
47+
for x in nums:
48+
prefix.append(prefix[-1] + x)
49+
50+
l = 1
51+
r = n
52+
53+
while l < r:
54+
m = l + (r - l + 1) // 2
55+
if self.fit(prefix, m, k):
56+
l = m
57+
else:
58+
r = m - 1
59+
60+
return l
61+
62+
def fit(self, prefix: list, m: int, k: int) -> bool:
63+
n = len(prefix) - 1
64+
for i in range(n - m + 1):
65+
if (prefix[i + m] - prefix[i + m // 2]) - (prefix[i + (m + 1) // 2] - prefix[i]) <= k:
66+
return True
67+
68+
return False
69+
70+
class Solution:
71+
def maxFrequencyScore(self, nums: List[int], k: int) -> int:
72+
n = len(nums)
73+
nums.sort()
74+
op = 0
75+
i = 0
76+
res = 0
77+
for j in range(n):
78+
op += nums[j] - nums[(i + j) // 2]
79+
while op > k:
80+
op += nums[i] - nums[(i + j + 1) // 2]
81+
i += 1
82+
83+
res = max(res, j - i + 1)
84+
return res

0 commit comments

Comments
 (0)