Skip to content

Commit 3d8c2a4

Browse files
committedNov 13, 2019
2019-11-12
1 parent 59c6fc5 commit 3d8c2a4

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed
 
Lines changed: 41 additions & 0 deletions
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
Lines changed: 14 additions & 0 deletions
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
Lines changed: 31 additions & 0 deletions
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+
Lines changed: 27 additions & 0 deletions
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]
Lines changed: 27 additions & 0 deletions
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

0 commit comments

Comments
 (0)
Please sign in to comment.