Skip to content

Commit c8ace80

Browse files
committed
Mar-4
1 parent a007714 commit c8ace80

9 files changed

+317
-29
lines changed

README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3636

3737
| Status | List | Progress | Notes |
3838
| ----- | ----- | ----- | ----- |
39-
| [22%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 12/54 | - |
39+
| [24%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 13/54 | - |
4040
| [47%] | [leetcode-google.md](./list/leetcode-google.md) | 226/471 | 2 vips |
4141
| [52%] | [leetcode-75.md](./list/leetcode-75.md) | 39/75 | - |
4242
| [52%] | [leetcode-topics.md](./list/leetcode-topics.md) | 75/144 | - |
@@ -52,10 +52,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5252
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5353
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5454
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
55-
| [🔲] | [9c-top.md](./list/9c-top.md) | 47/56 | 2 vips |
56-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 44/2208 | 3 vips |
55+
| [🔲] | [9c-top.md](./list/9c-top.md) | 49/56 | 2 vips |
56+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 45/2208 | 3 vips |
5757

58-
**Solved**: 569 problems
58+
**Solved**: 571 problems
5959

6060
## 类型/Category
6161

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

101101
## Math
102102

103-
| Link | Problem(31) | Solution | Tag | Time | Space | Ref |
103+
| Link | Problem(32) | 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\) | - |
107107
| [Leetcode-224](https://leetcode.com/problems/basic-calculator/) | Basic Calculator | [c++](./leetcode/224.basic-calculator.cpp), [python3](./leetcode/224.basic-calculator.py) | Math | O\(N\) | O\(N\) | - |
108108
| [Leetcode-227](https://leetcode.com/problems/basic-calculator-ii/) | Basic Calculator II | [c++](./leetcode/227.basic-calculator-ii.cpp), [python3](./leetcode/227.basic-calculator-ii.py) | Math | O\(N\) | O\(N\) | - |
109109
| [Leetcode-932](https://leetcode.com/problems/beautiful-array/) | Beautiful Array | [c++](./leetcode/932.beautiful-array.cpp), [python3](./leetcode/932.beautiful-array.py) | Math | O\(N\) | O\(N\) | - |
110+
| [Leetcode-1780](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | Check If Number Is A Sum Of Powers Of Three | [c++](./leetcode/1780.check-if-number-is-a-sum-of-powers-of-three.cpp), [python3](./leetcode/1780.check-if-number-is-a-sum-of-powers-of-three.py) | Math | O\(LogN\) | O\(1\) | - |
110111
| [Leetcode-3274](https://leetcode.com/problems/check-if-two-chessboard-squares-have-the-same-color/) | Check If Two Chessboard Squares Have The Same Color | [c++](./leetcode/3274.check-if-two-chessboard-squares-have-the-same-color.cpp), [python3](./leetcode/3274.check-if-two-chessboard-squares-have-the-same-color.py) | Math | O\(1\) | O\(1\) | - |
111112
| [Leetcode-3280](https://leetcode.com/problems/convert-date-to-binary/) | Convert Date To Binary | [c++](./leetcode/3280.convert-date-to-binary.cpp), [python3](./leetcode/3280.convert-date-to-binary.py) | Math | O\(1\) | O\(1\) | - |
112113
| [Leetcode-2364](https://leetcode.com/problems/count-number-of-bad-pairs/) | Count Number Of Bad Pairs | [c++](./leetcode/2364.count-number-of-bad-pairs.cpp), [python3](./leetcode/2364.count-number-of-bad-pairs.py) | Math | O\(N\) | O\(N\) | - |
@@ -376,7 +377,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
376377

377378
## Binary Search
378379

379-
| Link | Problem(52) | Solution | Tag | Time | Space | Ref |
380+
| Link | Problem(53) | Solution | Tag | Time | Space | Ref |
380381
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
381382
| [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\) | - |
382383
| [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\) | - |
@@ -387,6 +388,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
387388
| [Leetcode-154](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | Find Minimum In Rotated Sorted Array II | [c++](./leetcode/154.find-minimum-in-rotated-sorted-array-ii.cpp), [python3](./leetcode/154.find-minimum-in-rotated-sorted-array-ii.py) | Binary Search | O\(logN\) ~ O\(N\) | O\(1\) | - |
388389
| [Leetcode-162](https://leetcode.com/problems/find-peak-element/) | Find Peak Element | [c++](./leetcode/162.find-peak-element.cpp), [python3](./leetcode/162.find-peak-element.py) | Binary Search | O\(logN\) | O\(1\) | - |
389390
| [Leetcode-287](https://leetcode.com/problems/find-the-duplicate-number/) | Find The Duplicate Number | [c++](./leetcode/287.find-the-duplicate-number.cpp), [python3](./leetcode/287.find-the-duplicate-number.py) | Binary Search | O\(N\) | O\(1\) | - |
391+
| [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) | Binary Search | O\(N\) | O\(N\) | - |
390392
| [Leetcode-278](https://leetcode.com/problems/first-bad-version/) | First Bad Version | [c++](./leetcode/278.first-bad-version.cpp), [python3](./leetcode/278.first-bad-version.py) | Binary Search | O\(logN\) | O\(1\) | - |
391393
| [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) | Binary Search | O\(NlogN\) | O\(1\) | - |
392394
| [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) | Binary Search | O\(N\) | O\(1\) | - |
@@ -458,7 +460,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
458460

459461
## Hash Table
460462

461-
| Link | Problem(86) | Solution | Tag | Time | Space | Ref |
463+
| Link | Problem(87) | Solution | Tag | Time | Space | Ref |
462464
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
463465
| [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\) | - |
464466
| [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\) | - |
@@ -477,6 +479,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
477479
| [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\) | - |
478480
| [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\) | - |
479481
| [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\) | - |
482+
| [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\) | - |
480483
| [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\) | - |
481484
| [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\) | - |
482485
| [Leetcode-387](https://leetcode.com/problems/first-unique-character-in-a-string/) | First Unique Character In A String | [c++](./leetcode/387.first-unique-character-in-a-string.cpp), [python3](./leetcode/387.first-unique-character-in-a-string.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -714,7 +717,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
714717

715718
## Sliding Window
716719

717-
| Link | Problem(51) | Solution | Tag | Time | Space | Ref |
720+
| Link | Problem(52) | Solution | Tag | Time | Space | Ref |
718721
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
719722
| [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\) | - |
720723
| [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\) | - |
@@ -723,6 +726,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
723726
| [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\) | - |
724727
| [Leetcode-2156](https://leetcode.com/problems/find-substring-with-given-hash-value/) | Find Substring With Given Hash Value | [c++](./leetcode/2156.find-substring-with-given-hash-value.cpp), [python3](./leetcode/2156.find-substring-with-given-hash-value.py) | Sliding Window | O\(N\) | O\(1\) | - |
725728
| [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) | Sliding Window | O\(N\) | O\(N\) | - |
729+
| [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) | Sliding Window | O\(N\) | O\(N\) | - |
726730
| [Leetcode-2730](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/) | Find The Longest Semi Repetitive Substring | [c++](./leetcode/2730.find-the-longest-semi-repetitive-substring.cpp), [python3](./leetcode/2730.find-the-longest-semi-repetitive-substring.py) | Sliding Window | O\(N\) | O\(1\) | - |
727731
| [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) | Sliding Window | O\(NlogN\) | O\(1\) | - |
728732
| [Leetcode-904](https://leetcode.com/problems/fruit-into-baskets/) | Fruit Into Baskets | [c++](./leetcode/904.fruit-into-baskets.cpp), [python3](./leetcode/904.fruit-into-baskets.py) | Sliding Window | O\(N\) | O\(1\) | - |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Tag: Math
2+
// Time: O(LogN)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/_C41NB1Mw6w
7+
8+
// Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
9+
// An integer y is a power of three if there exists an integer x such that y == 3x.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: n = 12
14+
// Output: true
15+
// Explanation: 12 = 31 + 32
16+
//
17+
// Example 2:
18+
//
19+
// Input: n = 91
20+
// Output: true
21+
// Explanation: 91 = 30 + 32 + 34
22+
//
23+
// Example 3:
24+
//
25+
// Input: n = 21
26+
// Output: false
27+
//
28+
//  
29+
// Constraints:
30+
//
31+
// 1 <= n <= 107
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
bool checkPowersOfThree(int n) {
38+
while (n > 0) {
39+
if (n % 3 == 2) {
40+
return false;
41+
} else {
42+
n = n / 3;
43+
}
44+
}
45+
return true;
46+
}
47+
};
48+
49+
class Solution {
50+
public:
51+
bool checkPowersOfThree(int n) {
52+
vector<long long> table(15, 1);
53+
for (int i = 1; i < 15; i++) {
54+
table[i] = table[i - 1] * 3;
55+
}
56+
57+
for (int i = 14; i >= 0; i--) {
58+
if (n >= 2 * table[i]) {
59+
return false;
60+
} else if (n >= table[i]) {
61+
n -= table[i];
62+
}
63+
}
64+
65+
return true;
66+
}
67+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tag: Math
2+
# Time: O(LogN)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/_C41NB1Mw6w
7+
8+
# Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.
9+
# An integer y is a power of three if there exists an integer x such that y == 3x.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: n = 12
14+
# Output: true
15+
# Explanation: 12 = 31 + 32
16+
#
17+
# Example 2:
18+
#
19+
# Input: n = 91
20+
# Output: true
21+
# Explanation: 91 = 30 + 32 + 34
22+
#
23+
# Example 3:
24+
#
25+
# Input: n = 21
26+
# Output: false
27+
#
28+
#  
29+
# Constraints:
30+
#
31+
# 1 <= n <= 107
32+
#
33+
#
34+
35+
class Solution:
36+
def checkPowersOfThree(self, n: int) -> bool:
37+
while n > 0:
38+
if n % 3 == 2:
39+
return False
40+
n = n // 3
41+
42+
return True
43+
44+
class Solution:
45+
def checkPowersOfThree(self, n: int) -> bool:
46+
table = [3 ** i for i in range(15)]
47+
for i in range(len(table) - 1, -1, -1):
48+
if n >= 2 * table[i]:
49+
return False
50+
elif n >= table[i]:
51+
n -= table[i]
52+
53+
return True

leetcode/240.search-a-2d-matrix-ii.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@
3838
class Solution {
3939
public:
4040
bool searchMatrix(vector<vector<int>>& matrix, int target) {
41-
int m = matrix.size();
42-
int n = matrix[0].size();
41+
int n = matrix.size();
42+
int m = matrix[0].size();
4343

4444
int i = 0;
45-
int j = n - 1;
45+
int j = m - 1;
4646

47-
while (i < m && j >= 0) {
47+
while (i < n && j >= 0) {
4848
if (target == matrix[i][j]) {
4949
return true;
5050
}
5151

52-
if (target > matrix[i][j]) {
53-
i++;
54-
} else {
52+
if (matrix[i][j] > target) {
5553
j--;
54+
} else {
55+
i++;
5656
}
5757
}
5858

leetcode/240.search-a-2d-matrix-ii.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,20 @@
3737

3838
class Solution:
3939
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
40-
m = len(matrix)
41-
n = len(matrix[0])
40+
n = len(matrix)
41+
m = len(matrix[0])
4242

4343
i = 0
44-
j = n - 1
44+
j = m - 1
4545

46-
while i < m and j >= 0:
47-
48-
if target == matrix[i][j]:
46+
while i < n and j >= 0:
47+
print(i, j)
48+
if matrix[i][j] == target:
4949
return True
50-
51-
if target > matrix[i][j]:
52-
i += 1
53-
else:
50+
51+
if matrix[i][j] > target:
5452
j -= 1
53+
else:
54+
i += 1
5555

5656
return False
57-
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Tag: Array, Hash Table, Binary Search, Sliding Window
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed integer array nums and an integer k.
8+
// A subarray is called equal if all of its elements are equal. Note that the empty subarray is an equal subarray.
9+
// Return the length of the longest possible equal subarray after deleting at most k elements from nums.
10+
// A subarray is a contiguous, possibly empty sequence of elements within an array.
11+
//  
12+
// Example 1:
13+
//
14+
// Input: nums = [1,3,2,3,1,3], k = 3
15+
// Output: 3
16+
// Explanation: It's optimal to delete the elements at index 2 and index 4.
17+
// After deleting them, nums becomes equal to [1, 3, 3, 3].
18+
// The longest equal subarray starts at i = 1 and ends at j = 3 with length equal to 3.
19+
// It can be proven that no longer equal subarrays can be created.
20+
//
21+
// Example 2:
22+
//
23+
// Input: nums = [1,1,2,2,1,1], k = 2
24+
// Output: 4
25+
// Explanation: It's optimal to delete the elements at index 2 and index 3.
26+
// After deleting them, nums becomes equal to [1, 1, 1, 1].
27+
// The array itself is an equal subarray, so the answer is 4.
28+
// It can be proven that no longer equal subarrays can be created.
29+
//
30+
//  
31+
// Constraints:
32+
//
33+
// 1 <= nums.length <= 105
34+
// 1 <= nums[i] <= nums.length
35+
// 0 <= k <= nums.length
36+
//
37+
//
38+
39+
class Solution {
40+
public:
41+
int longestEqualSubarray(vector<int>& nums, int k) {
42+
int n = nums.size();
43+
unordered_map<int, int> counter;
44+
int res = 0;
45+
int i = 0;
46+
for (int j = 0; j < n; j++) {
47+
counter[nums[j]] += 1;
48+
res = max(res, counter[nums[j]]);
49+
if (j - i + 1 - res > k) {
50+
counter[nums[i]] -= 1;
51+
i += 1;
52+
}
53+
}
54+
55+
return res;
56+
57+
}
58+
};
59+
60+
class Solution {
61+
public:
62+
int longestEqualSubarray(vector<int>& nums, int k) {
63+
int n = nums.size();
64+
unordered_map<int, vector<int>> counter;
65+
for (int i = 0; i < n; i++) {
66+
counter[nums[i]].push_back(i);
67+
}
68+
int res = 0;
69+
for (auto &[num, indexes]: counter) {
70+
int count = 0;
71+
int i = 0;
72+
for (int j = 0; j < indexes.size(); j++) {
73+
count += 1;
74+
while (indexes[j] - indexes[i] + 1 - count > k) {
75+
count -= 1;
76+
i += 1;
77+
}
78+
res = max(res, count);
79+
}
80+
}
81+
82+
return res;
83+
84+
}
85+
};

0 commit comments

Comments
 (0)