Skip to content

Commit a13846d

Browse files
committed
more lc
1 parent 16b1f01 commit a13846d

13 files changed

Lines changed: 316 additions & 0 deletions
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
7+
freqs = {}
8+
9+
for i in range(len(s)):
10+
for k in range(minSize, maxSize + 1):
11+
if i + k - 1 < len(s):
12+
sub = s[i : i + k]
13+
14+
uniques = len(set(sub))
15+
16+
if uniques <= maxLetters:
17+
if sub not in freqs:
18+
freqs[sub] = 0
19+
freqs[sub] += 1
20+
21+
return 0 if len(freqs) == 0 else max(freqs.values())
22+
23+
24+
# end_submission
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def hasAllCodes(self, s: str, k: int) -> bool:
7+
st = set()
8+
9+
for i in range(len(s) - k + 1):
10+
st.add(s[i : i + k])
11+
12+
return len(st) == 2**k
13+
14+
15+
# end_submission
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def restoreArray(self, adjacentPairs: List[List[int]]) -> List[int]:
7+
num_edges = {}
8+
edges = {}
9+
10+
for a, b in adjacentPairs:
11+
if a not in num_edges:
12+
num_edges[a] = 0
13+
num_edges[a] += 1
14+
15+
if b not in num_edges:
16+
num_edges[b] = 0
17+
num_edges[b] += 1
18+
19+
if a not in edges:
20+
edges[a] = []
21+
edges[a].append(b)
22+
23+
if b not in edges:
24+
edges[b] = []
25+
edges[b].append(a)
26+
27+
prev = None
28+
cur = next(iter(k for k, v in num_edges.items() if v == 1))
29+
30+
res = [cur]
31+
32+
while len(res) != len(adjacentPairs) + 1:
33+
old_prev = prev
34+
prev = cur
35+
cur = edges[cur][0] if edges[cur][0] != old_prev else edges[cur][1]
36+
37+
res.append(cur)
38+
39+
return res
40+
41+
42+
# end_submission
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
7+
sm = sum(nums)
8+
9+
return (abs(sm - goal) + limit - 1) // limit
10+
11+
12+
# end_submission
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def maximumRemovals(self, s: str, p: str, removable: List[int]) -> int:
7+
l = 0
8+
r = len(removable)
9+
10+
ans = -1
11+
12+
while l <= r:
13+
m = (l + r) // 2
14+
15+
r_set = set(removable[:m])
16+
17+
p_idx = 0
18+
good = False
19+
20+
for i, ch in enumerate(s):
21+
if i not in r_set and ch == p[p_idx]:
22+
p_idx += 1
23+
24+
if p_idx == len(p):
25+
good = True
26+
break
27+
28+
if good:
29+
l = m + 1
30+
ans = m
31+
else:
32+
r = m - 1
33+
34+
return ans
35+
36+
37+
# end_submission
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def goodIndices(self, nums: List[int], k: int) -> List[int]:
7+
n = len(nums)
8+
9+
l = [1] * n
10+
r = [1] * n
11+
12+
for i in range(1, len(nums)):
13+
if nums[i] <= nums[i - 1]:
14+
l[i] = l[i - 1] + 1
15+
16+
for i in reversed(range(len(nums) - 1)):
17+
if nums[i] <= nums[i + 1]:
18+
r[i] = r[i + 1] + 1
19+
20+
return [i for i in range(k, n - k) if l[i - 1] >= k and r[i + 1] >= k]
21+
22+
23+
# end_submission
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def makeStringsEqual(self, s: str, target: str) -> bool:
7+
return (
8+
any(x == "1" for x in s) and any(x == "1" for x in target)
9+
) or s == target
10+
11+
12+
# end_submission
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def checkArray(self, nums: List[int], k: int) -> bool:
7+
q = deque()
8+
9+
diff = 0
10+
11+
for i, n in enumerate(nums):
12+
if len(q) > 0 and q[0][0] == i - k:
13+
diff -= q.popleft()[1]
14+
15+
if diff > n:
16+
return False
17+
18+
if diff < n:
19+
if i + k > len(nums):
20+
return False
21+
22+
q.append((i, n - diff))
23+
diff = n
24+
25+
return True
26+
27+
28+
# end_submission
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def earliestFinishTime(
7+
self,
8+
landStartTime: List[int],
9+
landDuration: List[int],
10+
waterStartTime: List[int],
11+
waterDuration: List[int],
12+
) -> int:
13+
ans = float("inf")
14+
15+
for s1, d1, s2, d2 in [
16+
(landStartTime, landDuration, waterStartTime, waterDuration),
17+
(waterStartTime, waterDuration, landStartTime, landDuration),
18+
]:
19+
s = min(t + d for t, d in zip(s1, d1))
20+
21+
for t, d in zip(s2, d2):
22+
ans = min(ans, max(s, t) + d)
23+
24+
return ans
25+
26+
27+
# end_submission
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from common import *
2+
3+
4+
# start_submission
5+
class Solution:
6+
def lastInteger(self, n: int) -> int:
7+
if n == 1:
8+
return n
9+
10+
ans = 1
11+
cnt = 1
12+
13+
while True:
14+
n = (n + 1) // 2
15+
16+
if n == 1:
17+
return ans
18+
19+
cnt *= 2
20+
21+
if n % 2 == 0:
22+
ans += cnt
23+
24+
n = (n + 1) // 2
25+
26+
if n == 1:
27+
return ans
28+
29+
cnt *= 2
30+
31+
32+
# end_submission

0 commit comments

Comments
 (0)