Skip to content

Commit 445c723

Browse files
committed
update
1 parent 3f0077b commit 445c723

File tree

38 files changed

+1433
-540
lines changed

38 files changed

+1433
-540
lines changed

README.md

Lines changed: 519 additions & 519 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
4+
prefix_count = defaultdict(int)
5+
prefix_count[0] = 1
6+
7+
total = 0
8+
res = 0
9+
for num in nums:
10+
if num % 2:
11+
total += 1
12+
res += prefix_count[total - k]
13+
prefix_count[total] += 1
14+
return res
15+
16+
# time O(n)
17+
# space O(n)
18+
# using array and prefix sum and hashmap to validate the gap subarray
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def maxSubArrayLen(self, nums: List[int], k: int) -> int:
3+
prefix_idx = {}
4+
prefix_idx[0] = - 1
5+
6+
total = 0
7+
res = 0
8+
for i, num in enumerate(nums):
9+
total += num
10+
if total - k in prefix_idx:
11+
res = max(res, i - prefix_idx[total - k])
12+
if total not in prefix_idx:
13+
prefix_idx[total] = i
14+
return res
15+
16+
# time O(n)
17+
# space O(n)
18+
# using array and prefix sum and hashmap to validate the gap subarray
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from sortedcontainers import SortedDict
2+
class Solution:
3+
def maxSumSubmatrix(self, matrix: List[List[int]], k: int) -> int:
4+
rows, cols = len(matrix), len(matrix[0])
5+
6+
prefix = [[0 for _ in range(cols + 1)] for _ in range(rows + 1)]
7+
for r in range(rows):
8+
total = 0
9+
for c in range(cols):
10+
total += matrix[r][c]
11+
prefix[r + 1][c + 1] = total
12+
13+
def get_sum(part_rows):
14+
prefix_dict = SortedDict()
15+
prefix_dict[0] = - 1
16+
total = 0
17+
res = float('-inf')
18+
for r, part_row in enumerate(part_rows):
19+
total += part_row
20+
idx = prefix_dict.bisect_left(total - k)
21+
if idx < len(prefix_dict):
22+
res = max(res, total - prefix_dict.peekitem(idx)[0])
23+
if total not in prefix_dict:
24+
prefix_dict[total] = r
25+
return res
26+
27+
res = float('-inf')
28+
for lc in range(cols):
29+
for rc in range(lc, cols):
30+
part_rows = [row[rc + 1] - row[lc] for i, row in enumerate(prefix) if i > 0]
31+
res = max(res, get_sum(part_rows))
32+
return res
33+
34+
# time O(C**2 * RlogR)
35+
# space O(RC)
36+
# using array and prefix sum and hashmap to validate the gap subarray and binary search
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
3+
prefix_idx = {}
4+
prefix_idx[0] = - 1
5+
6+
total = 0
7+
for i, num in enumerate(nums):
8+
total += num
9+
total %= k
10+
if total in prefix_idx and i - prefix_idx[total] >= 2:
11+
return True
12+
if total not in prefix_idx:
13+
prefix_idx[total] = i
14+
return False
15+
16+
# time O(n)
17+
# space O(n)
18+
# using array and prefix sum and hashmap to validate the gap subarray
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def findMaxLength(self, nums: List[int]) -> int:
3+
nums = [- 1 if num == 0 else 1 for num in nums]
4+
5+
prefix_idx = {}
6+
prefix_idx[0] = - 1
7+
total = 0
8+
res = 0
9+
for i, num in enumerate(nums):
10+
total += num
11+
if total in prefix_idx:
12+
res = max(res, i - prefix_idx[total])
13+
else:
14+
prefix_idx[total] = i
15+
return res
16+
17+
# time O(n)
18+
# space O(n)
19+
# using array and prefix sum and hashmap to validate the gap subarray
20+
'''
21+
1. if meet the same prefix in hashmap means the gap is balanced
22+
'''
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def splitArray(self, nums: List[int]) -> bool:
3+
prefix = [0 for _ in range(len(nums) + 1)]
4+
total = 0
5+
for i, num in enumerate(nums):
6+
total += num
7+
prefix[i + 1] = total
8+
9+
for j in range(3, len(nums) - 3):
10+
prefix_set = set()
11+
for i in range(1, j - 1):
12+
if prefix[i] == prefix[j] - prefix[i + 1]:
13+
prefix_set.add(prefix[i])
14+
for k in range(j + 2, len(nums) - 1):
15+
if prefix[k] - prefix[j + 1] == prefix[- 1] - prefix[k + 1]:
16+
if prefix[k] - prefix[j + 1] in prefix_set:
17+
return True
18+
return False
19+
20+
# time O(n**2)
21+
# space O(n)
22+
# using array and prefix sum and hashmap to validate the gap subarray
23+
'''
24+
1. xixjxkx
25+
0123456
26+
'''
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def subarraySum(self, nums: List[int], k: int) -> int:
4+
prefix_count = defaultdict(int)
5+
prefix_count[0] = 1
6+
total = 0
7+
res = 0
8+
for num in nums:
9+
total += num
10+
res += prefix_count[total - k]
11+
prefix_count[total] += 1
12+
return res
13+
14+
# time O(n)
15+
# space O(n)
16+
# using array and prefix sum and hashmap to validate the gap subarray
17+
'''
18+
1. if meet the prefix - k in hashmap means the gap is valid
19+
'''
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def subarraysDivByK(self, nums: List[int], k: int) -> int:
4+
prefix_count = defaultdict(int)
5+
prefix_count[0] = 1
6+
7+
total = 0
8+
res = 0
9+
for num in nums:
10+
total += num
11+
total %= k
12+
res += prefix_count[total]
13+
prefix_count[total] += 1
14+
return res
15+
16+
# time O(n), due to traverse
17+
# space O(n)
18+
# using array and prefix sum and hashmap to validate the gap subarray
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from collections import defaultdict
2+
class Solution:
3+
def numKLenSubstrNoRepeats(self, s: str, k: int) -> int:
4+
char_freq = defaultdict(int)
5+
res = 0
6+
left = 0
7+
for right in range(len(s)):
8+
char_freq[s[right]] += 1
9+
while right - left + 1 > k or char_freq[s[right]] > 1:
10+
char_freq[s[left]] -= 1
11+
if char_freq[s[left]] == 0:
12+
char_freq.pop(s[left])
13+
left += 1
14+
if len(char_freq) == k:
15+
res += 1
16+
return res
17+
18+
# time O(n)
19+
# space O(k)
20+
# using array and standard sliding window and hashmap

0 commit comments

Comments
 (0)