Skip to content

Commit 2a397b1

Browse files
committed
Apr-24
1 parent 5f3e839 commit 2a397b1

12 files changed

+317
-92
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5454
| [] | [grind75.md](./list/grind75.md) | 75/75 | - |
5555
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5656
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
57-
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 90/2210 | 4 vips |
57+
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 15/128 | 3 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 92/2210 | 4 vips |
5959

6060
**Solved**: 675 problems
6161

@@ -171,7 +171,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
171171
| Link | Problem(5) | Solution | Tag | Time | Space | Ref |
172172
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
173173
| [Leetcode-3343](https://leetcode.com/problems/count-number-of-balanced-permutations/) | Count Number Of Balanced Permutations | [c++](./leetcode/3343.count-number-of-balanced-permutations.cpp), [python3](./leetcode/3343.count-number-of-balanced-permutations.py) | Combinatorics | O\(N^3\) | O\(N^3\) | - |
174-
| [Leetcode-2338](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | Count The Number Of Ideal Arrays | [c++](./leetcode/2338.count-the-number-of-ideal-arrays.cpp), [python3](./leetcode/2338.count-the-number-of-ideal-arrays.py) | Combinatorics | \- | \- | - |
174+
| [Leetcode-2338](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | Count The Number Of Ideal Arrays | [c++](./leetcode/2338.count-the-number-of-ideal-arrays.cpp), [python3](./leetcode/2338.count-the-number-of-ideal-arrays.py) | Combinatorics | O\(NlogN\) | O\(N\) | - |
175175
| [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) | Combinatorics | O\(10\*\*\(N/2\) \* NlogN\) | O\(10\*\*\(N/2\)\) | - |
176176
| [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) | Combinatorics | O\(N\) | O\(1\) | - |
177177
| [Leetcode-62](https://leetcode.com/problems/unique-paths/) | Unique Paths | [c++](./leetcode/62.unique-paths.cpp), [python3](./leetcode/62.unique-paths.py) | Combinatorics | O\(MN\) | O\(MN\) | - |
@@ -358,7 +358,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
358358
| [Leetcode-322](https://leetcode.com/problems/coin-change/) | Coin Change | [c++](./leetcode/322.coin-change.cpp), [python3](./leetcode/322.coin-change.py) | Dynamic Programming | O\(K \* N\) | O\(N\) | - |
359359
| [Leetcode-518](https://leetcode.com/problems/coin-change-ii/) | Coin Change II | [c++](./leetcode/518.coin-change-ii.cpp), [python3](./leetcode/518.coin-change-ii.py) | Dynamic Programming | O\(NM\) | O\(NM\) | - |
360360
| [Leetcode-3343](https://leetcode.com/problems/count-number-of-balanced-permutations/) | Count Number Of Balanced Permutations | [c++](./leetcode/3343.count-number-of-balanced-permutations.cpp), [python3](./leetcode/3343.count-number-of-balanced-permutations.py) | Dynamic Programming | O\(N^3\) | O\(N^3\) | - |
361-
| [Leetcode-2338](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | Count The Number Of Ideal Arrays | [c++](./leetcode/2338.count-the-number-of-ideal-arrays.cpp), [python3](./leetcode/2338.count-the-number-of-ideal-arrays.py) | Dynamic Programming | \- | \- | - |
361+
| [Leetcode-2338](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | Count The Number Of Ideal Arrays | [c++](./leetcode/2338.count-the-number-of-ideal-arrays.cpp), [python3](./leetcode/2338.count-the-number-of-ideal-arrays.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
362362
| [Leetcode-2999](https://leetcode.com/problems/count-the-number-of-powerful-integers/) | Count The Number Of Powerful Integers | [c++](./leetcode/2999.count-the-number-of-powerful-integers.cpp), [python3](./leetcode/2999.count-the-number-of-powerful-integers.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
363363
| [Leetcode-338](https://leetcode.com/problems/counting-bits/) | Counting Bits | [c++](./leetcode/338.counting-bits.cpp), [python3](./leetcode/338.counting-bits.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
364364
| [Leetcode-91](https://leetcode.com/problems/decode-ways/) | Decode Ways | [c++](./leetcode/91.decode-ways.cpp), [python3](./leetcode/91.decode-ways.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
@@ -843,7 +843,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
843843

844844
## Two Pointers
845845

846-
| Link | Problem(43) | Solution | Tag | Time | Space | Ref |
846+
| Link | Problem(45) | Solution | Tag | Time | Space | Ref |
847847
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
848848
| [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\) | - |
849849
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Two Pointers | O\(N^3\) | O\(1\) | - |
@@ -872,6 +872,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
872872
| [Leetcode-2161](https://leetcode.com/problems/partition-array-according-to-given-pivot/) | Partition Array According To Given Pivot | [c++](./leetcode/2161.partition-array-according-to-given-pivot.cpp), [python3](./leetcode/2161.partition-array-according-to-given-pivot.py) | Two Pointers | O\(N\) | O\(N\) | - |
873873
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Two Pointers | O\(N\) | O\(N\) | - |
874874
| [Leetcode-19](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | Remove Nth Node From End Of List | [c++](./leetcode/19.remove-nth-node-from-end-of-list.cpp), [python3](./leetcode/19.remove-nth-node-from-end-of-list.py) | Two Pointers | O\(N\) | O\(1\) | - |
875+
| [Leetcode-344](https://leetcode.com/problems/reverse-string/) | Reverse String | [c++](./leetcode/344.reverse-string.cpp), [python3](./leetcode/344.reverse-string.py) | Two Pointers | O\(N\) | O\(1\) | - |
875876
| [Leetcode-189](https://leetcode.com/problems/rotate-array/) | Rotate Array | [c++](./leetcode/189.rotate-array.cpp), [python3](./leetcode/189.rotate-array.py) | Two Pointers | O\(N\) | O\(1\) | - |
876877
| [Leetcode-75](https://leetcode.com/problems/sort-colors/) | Sort Colors | [c++](./leetcode/75.sort-colors.cpp), [python3](./leetcode/75.sort-colors.py) | Two Pointers | O\(N\) | O\(1\) | - |
877878
| [Leetcode-148](https://leetcode.com/problems/sort-list/) | Sort List | [c++](./leetcode/148.sort-list.cpp), [python3](./leetcode/148.sort-list.py) | Two Pointers | O\(NlogN\) | O\(logN\) | - |
@@ -880,6 +881,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
880881
| [Leetcode-42](https://leetcode.com/problems/trapping-rain-water/) | Trapping Rain Water | [c++](./leetcode/42.trapping-rain-water.cpp), [python3](./leetcode/42.trapping-rain-water.py) | Two Pointers | O\(N\) | O\(N\) | - |
881882
| [Leetcode-167](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | Two Sum Ii Input Array Is Sorted | [c++](./leetcode/167.two-sum-ii-input-array-is-sorted.cpp), [python3](./leetcode/167.two-sum-ii-input-array-is-sorted.py) | Two Pointers | O\(N\) | O\(1\) | - |
882883
| [Leetcode-653](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/) | Two Sum Iv Input Is A Bst | [c++](./leetcode/653.two-sum-iv-input-is-a-bst.cpp), [python3](./leetcode/653.two-sum-iv-input-is-a-bst.py) | Two Pointers | O\(NH\) | O\(H\) | - |
884+
| [Leetcode-125](https://leetcode.com/problems/valid-palindrome/) | Valid Palindrome | [c++](./leetcode/125.valid-palindrome.cpp), [python3](./leetcode/125.valid-palindrome.py) | Two Pointers | O\(N\) | O\(1\) | - |
883885
| [Leetcode-680](https://leetcode.com/problems/valid-palindrome-ii/) | Valid Palindrome II | [c++](./leetcode/680.valid-palindrome-ii.cpp), [python3](./leetcode/680.valid-palindrome-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
884886
| [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\) | - |
885887
| [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 |
@@ -1467,7 +1469,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
14671469

14681470
## Other
14691471

1470-
| Link | Problem(104) | Solution | Tag | Time | Space | Ref |
1472+
| Link | Problem(102) | Solution | Tag | Time | Space | Ref |
14711473
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
14721474
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Other | \- | \- | - |
14731475
| [Leetcode-454](https://leetcode.com/problems/4sum-ii/) | 4Sum II | [c++](./leetcode/454.4sum-ii.cpp) | Other | \- | \- | - |
@@ -1542,7 +1544,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
15421544
| [Leetcode-143](https://leetcode.com/problems/reorder-list/) | Reorder List | [python3](./leetcode/143.reorder-list.py) | Other | \- | \- | - |
15431545
| [Leetcode-7](https://leetcode.com/problems/reverse-integer/) | Reverse Integer | [c++](./leetcode/7.reverse-integer.cpp), [python3](./leetcode/7.reverse-integer.py) | Other | \- | \- | - |
15441546
| [Leetcode-92](https://leetcode.com/problems/reverse-linked-list-ii/) | Reverse Linked List II | [c++](./leetcode/92.reverse-linked-list-ii.cpp), [python3](./leetcode/92.reverse-linked-list-ii.py) | Other | \- | \- | - |
1545-
| [Leetcode-344](https://leetcode.com/problems/reverse-string/) | Reverse String | [c++](./leetcode/344.reverse-string.cpp), [python3](./leetcode/344.reverse-string.py) | Other | \- | \- | - |
15461547
| [Leetcode-345](https://leetcode.com/problems/reverse-vowels-of-a-string/) | Reverse Vowels Of A String | [c++](./leetcode/345.reverse-vowels-of-a-string.cpp), [python3](./leetcode/345.reverse-vowels-of-a-string.py) | Other | \- | \- | - |
15471548
| [Leetcode-657](https://leetcode.com/problems/robot-return-to-origin/) | Robot Return To Origin | [python3](./leetcode/657.robot-return-to-origin.py) | Other | \- | \- | - |
15481549
| [Leetcode-61](https://leetcode.com/problems/rotate-list/) | Rotate List | [python3](./leetcode/61.rotate-list.py) | Other | \- | \- | - |
@@ -1559,7 +1560,6 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
15591560
| [Leetcode-170](https://leetcode.com/problems/two-sum-iii-data-structure-design/) | Two Sum Iii Data Structure Design | [c++](./leetcode/170.two-sum-iii-data-structure-design.cpp), [python3](./leetcode/170.two-sum-iii-data-structure-design.py) | Other | \- | \- | - |
15601561
| [Leetcode-929](https://leetcode.com/problems/unique-email-addresses/) | Unique Email Addresses | [python3](./leetcode/929.unique-email-addresses.py) | Other | \- | \- | - |
15611562
| [Leetcode-393](https://leetcode.com/problems/utf-8-validation/) | Utf 8 Validation | [c++](./leetcode/393.utf-8-validation.cpp), [python3](./leetcode/393.utf-8-validation.py) | Other | \- | \- | - |
1562-
| [Leetcode-125](https://leetcode.com/problems/valid-palindrome/) | Valid Palindrome | [c++](./leetcode/125.valid-palindrome.cpp), [python3](./leetcode/125.valid-palindrome.py) | Other | \- | \- | - |
15631563
| [Leetcode-408](https://leetcode.com/problems/valid-word-abbreviation/) | Valid Word Abbreviation | [c++](./leetcode/408.valid-word-abbreviation.cpp), [python3](./leetcode/408.valid-word-abbreviation.py) | Other | \- | \- | - |
15641564
| [Leetcode-286](https://leetcode.com/problems/walls-and-gates/) | Walls And Gates | [c++](./leetcode/286.walls-and-gates.cpp), [python3](./leetcode/286.walls-and-gates.py) | Other | \- | \- | - |
15651565
| [Leetcode-127](https://leetcode.com/problems/word-ladder/) | Word Ladder | [c++](./leetcode/127.word-ladder.cpp), [python3](./leetcode/127.word-ladder.py) | Other | \- | \- | - |

leetcode/125.valid-palindrome.cpp

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
1+
// Tag: Two Pointers, String
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
8+
// Given a string s, return true if it is a palindrome, or false otherwise.
9+
//  
10+
// Example 1:
11+
//
12+
// Input: s = "A man, a plan, a canal: Panama"
13+
// Output: true
14+
// Explanation: "amanaplanacanalpanama" is a palindrome.
15+
//
16+
// Example 2:
17+
//
18+
// Input: s = "race a car"
19+
// Output: false
20+
// Explanation: "raceacar" is not a palindrome.
21+
//
22+
// Example 3:
23+
//
24+
// Input: s = " "
25+
// Output: true
26+
// Explanation: s is an empty string "" after removing non-alphanumeric characters.
27+
// Since an empty string reads the same forward and backward, it is a palindrome.
28+
//
29+
//  
30+
// Constraints:
31+
//
32+
// 1 <= s.length <= 2 * 105
33+
// s consists only of printable ASCII characters.
34+
//
35+
//
36+
137
class Solution {
238
public:
339
bool isPalindrome(string s) {
4-
5-
int start = 0;
6-
int end = (int)(s.size() - 1);
40+
int l = 0;
41+
int r = s.size() - 1;
742

8-
while (start < end)
9-
{
10-
while (start < end && !isalnum(s[start]))
11-
{
12-
start++;
43+
while (l < r) {
44+
while (l < r && !isalnum(s[l])) {
45+
l++;
1346
}
1447

15-
while (start < end && !isalnum(s[end]))
16-
{
17-
end--;
48+
while (l < r && !isalnum(s[r])) {
49+
r--;
1850
}
1951

20-
if (tolower(s[start]) != tolower(s[end]))
21-
{
52+
if (tolower(s[l]) != tolower(s[r])) {
2253
return false;
2354
}
2455

25-
start++;
26-
end--;
56+
l++;
57+
r--;
2758
}
2859

2960
return true;
3061
}
31-
};
62+
};

leetcode/125.valid-palindrome.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
1-
class Solution(object):
2-
def isPalindrome(self, s):
3-
"""
4-
:type s: str
5-
:rtype: bool
6-
"""
1+
# Tag: Two Pointers, String
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
76

8-
if s is None:
9-
return True
7+
# A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
8+
# Given a string s, return true if it is a palindrome, or false otherwise.
9+
#  
10+
# Example 1:
11+
#
12+
# Input: s = "A man, a plan, a canal: Panama"
13+
# Output: true
14+
# Explanation: "amanaplanacanalpanama" is a palindrome.
15+
#
16+
# Example 2:
17+
#
18+
# Input: s = "race a car"
19+
# Output: false
20+
# Explanation: "raceacar" is not a palindrome.
21+
#
22+
# Example 3:
23+
#
24+
# Input: s = " "
25+
# Output: true
26+
# Explanation: s is an empty string "" after removing non-alphanumeric characters.
27+
# Since an empty string reads the same forward and backward, it is a palindrome.
28+
#
29+
#  
30+
# Constraints:
31+
#
32+
# 1 <= s.length <= 2 * 105
33+
# s consists only of printable ASCII characters.
34+
#
35+
#
1036

11-
start = 0
12-
end = len(s) - 1
13-
14-
while (start < end):
15-
while(start < end and not s[start].isalnum()):
16-
start += 1
37+
class Solution:
38+
def isPalindrome(self, s: str) -> bool:
39+
l = 0
40+
r = len(s) - 1
41+
while l < r:
42+
while l < r and not s[l].isalnum():
43+
l += 1
1744

18-
while(start < end and not s[end].isalnum()):
19-
end -= 1
45+
while l < r and not s[r].isalnum():
46+
r -= 1
2047

21-
if (s[start].lower() != s[end].lower()):
48+
if s[l].lower() != s[r].lower():
2249
return False
23-
24-
start += 1
25-
end -= 1
50+
51+
l += 1
52+
r -= 1
2653

2754
return True

leetcode/2338.count-the-number-of-ideal-arrays.cpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Tag: Math, Dynamic Programming, Combinatorics, Number Theory
2-
// Time: -
3-
// Space: -
2+
// Time: O(NlogN)
3+
// Space: O(N)
44
// Ref: -
55
// Note: -
66
// Video: Video: https://youtu.be/tAIPbGRZojY
@@ -46,9 +46,77 @@
4646
//
4747
//
4848

49+
int comb[10001][14] = { 1 }, dp[10001][14] = {}, mod = 1000000007;
50+
class Solution {
51+
public:
52+
int idealArrays(int n, int maxValue) {
53+
if (comb[1][1] == 0) { // one-time computation.
54+
for (int s = 1; s <= 10000; ++s)
55+
for (int r = 0; r < 14; ++r)
56+
comb[s][r] = r == 0 ? 1 : (comb[s - 1][r - 1] + comb[s - 1][r]) % mod;
57+
58+
for (int div = 1; div <= 10000; ++div) { // Sieve of Eratosthenes
59+
dp[div][0] += 1;
60+
for (int i = 2 * div; i <= 10000; i += div)
61+
for (int bars = 0; dp[div][bars]; ++bars)
62+
dp[i][bars + 1] += dp[div][bars];
63+
}
64+
}
65+
int res = 0;
66+
for (int i = 1; i <= maxValue; ++i)
67+
for (int bars = 0; bars < min(14, n) && dp[i][bars]; ++bars)
68+
res = (1LL * dp[i][bars] * comb[n - 1][bars] + res) % mod;
69+
return res;
70+
}
71+
};
72+
73+
int comb[10001][14] = {1};
4974
class Solution {
5075
public:
5176
int idealArrays(int n, int maxValue) {
52-
// TODO, check python first
77+
int k = min(n, 14);
78+
int mod = 1e9 + 7;
79+
unordered_map<int, vector<int>> divisors;
80+
for (int i = 1; i <= maxValue; i++) {
81+
for (int j = 2 * i; j <= maxValue; j += i) {
82+
divisors[j].push_back(i);
83+
}
84+
}
85+
86+
vector<vector<int>> dp(k + 1, vector<int>(maxValue + 1, 0));
87+
for (int i = 1; i <= maxValue; i++) {
88+
dp[1][i] = 1;
89+
}
90+
91+
for (int i = 2; i <= k; i++) {
92+
for (int j = 1; j <= maxValue; j++) {
93+
for (auto d: divisors[j]) {
94+
dp[i][j] = (dp[i][j] + dp[i - 1][d]) % mod;
95+
}
96+
}
97+
}
98+
99+
for (int i = 1; i <= k; i++) {
100+
for (int j = 1; j <= maxValue; j++) {
101+
dp[i][0] = (dp[i][0] + dp[i][j]) % mod;
102+
}
103+
}
104+
105+
if (comb[1][1] == 0) {
106+
for (int i = 1; i <= 10000; i++) {
107+
for (int j = 0; j < 14; j++) {
108+
comb[i][j] = j == 0 ? 1 : (comb[i - 1][j - 1] + comb[i - 1][j]) % mod;
109+
}
110+
}
111+
}
112+
113+
int res = 0;
114+
for (int i = 1; i <= k; i++) {
115+
if (dp[i][0] > 0) {
116+
res = (res + 1LL * comb[n - 1][i - 1] * dp[i][0] ) % mod;
117+
}
118+
}
119+
120+
return res;
53121
}
54122
};

leetcode/2338.count-the-number-of-ideal-arrays.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tag: Math, Dynamic Programming, Combinatorics, Number Theory
2-
# Time: -
3-
# Space: -
2+
# Time: O(NlogN)
3+
# Space: O(N)
44
# Ref: -
55
# Note: -
66
# Video: https://youtu.be/tAIPbGRZojY
@@ -46,6 +46,34 @@
4646
#
4747
#
4848

49+
from collections import defaultdict
50+
import math
51+
class Solution:
52+
def idealArrays(self, n: int, maxValue: int) -> int:
53+
mod = 10 ** 9 + 7
54+
k = min(n, 14)
55+
56+
dp = [[0] * (k + 1) for i in range(maxValue + 1)]
57+
58+
for i in range(1, maxValue + 1):
59+
dp[i][0] += 1
60+
for j in range(2 * i, maxValue + 1, i):
61+
for bars in range(k):
62+
if dp[i][bars] > 0:
63+
dp[j][bars + 1] += dp[i][bars]
64+
else:
65+
break
66+
67+
res = 0
68+
for i in range(1, maxValue + 1):
69+
for bars in range(k):
70+
if dp[i][bars] > 0:
71+
res = (res + math.comb(n - 1, bars) * dp[i][bars]) % mod
72+
else:
73+
break
74+
75+
return res
76+
4977
from collections import defaultdict
5078
import math
5179
class Solution:

leetcode/2799.count-complete-subarrays-in-an-array.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(N)
44
// Ref: -
55
// Note: -
6+
// Video: https://youtu.be/br1hgtnpq4k
67

78
// You are given an array nums consisting of positive integers.
89
// We call a subarray of an array complete if the following condition is satisfied:

0 commit comments

Comments
 (0)