Skip to content

Commit e305dd6

Browse files
author
Jiayang Wu
committed
2 parents 466dc36 + 291ca56 commit e305dd6

File tree

25 files changed

+624
-0
lines changed

25 files changed

+624
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution(object):
2+
def isSubsequence(self, s, t):
3+
"""
4+
:type s: str
5+
:type t: str
6+
:rtype: bool
7+
"""
8+
i, j = 0, 0
9+
while i < len(s) and j < len(t):
10+
if s[i] == t[j]:
11+
i += 1
12+
j += 1
13+
return i == len(s)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def totalHammingDistance(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
if not nums:
8+
return 0
9+
res = 0
10+
mask = 1
11+
for i in range(32):
12+
cnt_one = 0
13+
for num in nums:
14+
cnt_one += 1 if num & mask else 0
15+
16+
res += cnt_one * (len(nums) - cnt_one)
17+
mask = mask << 1
18+
return res
19+
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def diameterOfBinaryTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root:
15+
return 0
16+
self.res = 0
17+
def height(node):
18+
if not node:
19+
return 0
20+
left_h = height(node.left)
21+
right_h = height(node.right)
22+
23+
self.res = max(self.res, left_h + right_h)
24+
return 1 + max(left_h, right_h)
25+
height(root)
26+
return self.res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def findLongestChain(self, pairs):
3+
"""
4+
:type pairs: List[List[int]]
5+
:rtype: int
6+
"""
7+
pairs = sorted(pairs, key = lambda x: x[1])
8+
9+
end = pairs[0][0] - 1
10+
res = 0
11+
for pair in pairs:
12+
if pair[0] > end:
13+
res += 1
14+
end = pair[1]
15+
16+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def numMatchingSubseq(self, S, words):
3+
"""
4+
:type S: str
5+
:type words: List[str]
6+
:rtype: int
7+
"""
8+
from collections import defaultdict
9+
10+
dic = defaultdict(list)
11+
for i, ch in enumerate(S):
12+
dic[ch].append(i)
13+
14+
res = 0
15+
for word in words:
16+
pre = -1
17+
flag = True
18+
for i, ch in enumerate(word):
19+
l = dic[ch]
20+
# 在l找第一个比pre大的元素
21+
idx = bisect.bisect(l, pre)
22+
23+
if idx == len(l):# 没找到
24+
flag = False
25+
break
26+
pre = l[idx]
27+
28+
if flag:
29+
res += 1
30+
31+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def countLetters(self, S):
3+
"""
4+
:type S: str
5+
:rtype: int
6+
"""
7+
res = 0
8+
for i in range(len(S)):
9+
for j in range(i + 1, len(S) + 1):
10+
substring = S[i:j]
11+
if substring == substring[0] * len(substring):
12+
res += 1
13+
# print substring
14+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import defaultdict
2+
class Solution(object):
3+
def criticalConnections(self, n, connections):
4+
"""
5+
:type n: int
6+
:type connections: List[List[int]]
7+
:rtype: List[List[int]]
8+
"""
9+
visited = set()
10+
low = [9999999] * n
11+
discover = [999999] * n
12+
parent = [-1] * n
13+
14+
graph = defaultdict(list)
15+
self.time = 0
16+
res = []
17+
for u, v in connections:
18+
graph[u].append(v)
19+
graph[v].append(u)
20+
21+
def dfs(u):
22+
visited.add(u)
23+
discover[u] = self.time
24+
low[u] = self.time
25+
self.time += 1
26+
27+
for v in graph[u]:
28+
if v not in visited:
29+
parent[v] = u
30+
dfs(v)
31+
low[u] = min(low[u], low[v])
32+
33+
if low[v] > discover[u]:
34+
res.append([u, v])
35+
elif v != parent[u]:
36+
low[u] = min(low[u], discover[v])
37+
38+
for i in range(n):
39+
if i not in visited:
40+
dfs(i)
41+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def uniqueOccurrences(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: bool
6+
"""
7+
d = collections.Counter(arr)
8+
return len(d.values()) == len(set(d.values()))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def arraysIntersection(self, arr1, arr2, arr3):
3+
"""
4+
:type arr1: List[int]
5+
:type arr2: List[int]
6+
:type arr3: List[int]
7+
:rtype: List[int]
8+
"""
9+
res = []
10+
record = [0 for _ in range(2005)]
11+
for num in arr1 + arr2 + arr3:
12+
record[num] += 1
13+
14+
for i in range(len(record)):
15+
if record[i] == 3:
16+
res.append(i)
17+
18+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def balancedStringSplit(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
if not s:
8+
return 0
9+
# print s
10+
l, r = 0, 0
11+
12+
for i in range(len(s)):
13+
if s[i] == "R":
14+
r += 1
15+
else:
16+
l += 1
17+
# print r, l
18+
if l == r:
19+
return 1 + self.balancedStringSplit(s[i + 1:])
20+
21+
return 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def missingNumber(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: int
6+
"""
7+
d = (arr[-1] - arr[0]) // len(arr)
8+
9+
k = arr[0]
10+
for num in arr:
11+
if num != k:
12+
return k
13+
k += d
14+
return k
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
def minAvailableDuration(self, slots1, slots2, duration):
3+
"""
4+
:type slots1: List[List[int]]
5+
:type slots2: List[List[int]]
6+
:type duration: int
7+
:rtype: List[int]
8+
"""
9+
slots1 = sorted(slots1, key = lambda x:x[0])
10+
slots2 = sorted(slots2, key = lambda x:x[0])
11+
slots = []
12+
i, j = 0, 0
13+
while i < len(slots1) and j < len(slots2):
14+
if slots1[i][1] < slots2[j][0]:
15+
i += 1
16+
continue
17+
elif slots1[i][0] > slots2[j][1]:
18+
j += 1
19+
continue
20+
start = max(slots1[i][0], slots2[j][0])
21+
end = min(slots1[i][1], slots2[j][1])
22+
slots.append([start, end])
23+
i += 1
24+
25+
print(slots)
26+
for start, end in slots:
27+
if end - start >= duration:
28+
return [start, start + duration]
29+
return []
30+
31+
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def probabilityOfHeads(self, prob, target):
3+
"""
4+
:type prob: List[float]
5+
:type target: int
6+
:rtype: float
7+
"""
8+
dp = [0 for _ in range(len(prob) + 1)]
9+
dp[1] = prob[0]
10+
dp[0] = 1 - prob[0]
11+
for i in range(1, len(prob)):
12+
new_dp = [0 for _ in range(len(prob) + 1)]
13+
for j in range(target + 1):
14+
new_dp[j] = dp[j] * (1 - prob[i]) + dp[j - 1] * prob[i]
15+
dp = new_dp[:]
16+
return dp[target]
17+
18+
# dp = [[0 for _ in range(len(prob) + 1)] for _ in range(len(prob))]
19+
# # dp[i][j] 表示前i个硬币里,有j个硬币正面朝上的概率
20+
# dp[0][1] = prob[0]
21+
# dp[0][0] = 1 - prob[0]
22+
# for i, p in enumerate(prob):
23+
# for j in range(target + 1):
24+
# if i > 0:
25+
# dp[i][j] += dp[i - 1][j] * (1 - p)
26+
# dp[i][j] += dp[i - 1][j - 1] * (p)
27+
# return dp[-1][target]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def maximizeSweetness(self, sweetness, K):
3+
"""
4+
:type sweetness: List[int]
5+
:type K: int
6+
:rtype: int
7+
"""
8+
if not K:
9+
return sum(sweetness)
10+
left, right = 0, sum(sweetness) // K
11+
res = 0
12+
while left <= right:
13+
mid = (left + right) // 2
14+
cnt = 0
15+
tmp = 0
16+
for s in sweetness:
17+
if tmp + s > mid:
18+
cnt += 1
19+
tmp = 0
20+
else:
21+
tmp += s
22+
23+
if cnt < K + 1:
24+
right = mid - 1
25+
else:
26+
left = mid + 1
27+
return left
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def checkStraightLine(self, coordinates):
3+
"""
4+
:type coordinates: List[List[int]]
5+
:rtype: bool
6+
"""
7+
c = sorted(coordinates, key = lambda x:x[0])
8+
k = None
9+
for i in range(len(c)):
10+
if i:
11+
x0, y0 = c[i - 1][0], c[i - 1][1]
12+
x1, y1 = c[i][0], c[i][1]
13+
14+
if x0 == x1:
15+
return False
16+
new_k = 1.0 * (y1 - y0) / (x1 - x0)
17+
if k and k != new_k:
18+
return False
19+
k = new_k
20+
21+
return True
22+
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def transformArray(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: List[int]
6+
"""
7+
flag = 1
8+
while flag:
9+
flag = 0
10+
res = [num for num in arr]
11+
for i in range(1, len(arr) - 1):
12+
if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]:
13+
res[i] -= 1
14+
flag = 1
15+
elif arr[i - 1] > arr[i] and arr[i] < arr[i + 1]:
16+
res[i] += 1
17+
flag = 1
18+
arr = res[:]
19+
return res
20+

0 commit comments

Comments
 (0)