-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
100 changed files
with
4,665 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
__________________________________________________________________________________________________ | ||
sample 88 ms submission | ||
class Solution: | ||
def minSwap(self, A: List[int], B: List[int]) -> int: | ||
n = len(A) | ||
swap, noswap = [n] * n, [n] * n | ||
swap[0], noswap[0] = 1, 0 | ||
for i in range(1, len(A)): | ||
if A[i] > A[i-1] and B[i] > B[i-1] and A[i] > B[i-1] and B[i] > A[i-1]: | ||
noswap[i] = min(noswap[i-1], swap[i-1]) | ||
swap[i] = min(noswap[i-1], swap[i-1]) + 1 | ||
elif A[i] > A[i-1] and B[i] > B[i-1]: | ||
noswap[i] = noswap[i-1] | ||
swap[i] = swap[i-1] + 1 | ||
elif A[i] > B[i-1] and B[i] > A[i-1]: | ||
noswap[i] = swap[i-1] | ||
swap[i] = noswap[i-1] + 1 | ||
|
||
return min(swap[-1], noswap[-1]) | ||
__________________________________________________________________________________________________ | ||
|
||
sample 96 ms submission | ||
class Solution: | ||
def minSwap(self, A: List[int], B: List[int]) -> int: | ||
|
||
minSwap, minNotSwap= 1, 0 | ||
for i in range(1, len(A)): | ||
swap, notSwap = len(A), len(A) | ||
|
||
if A[i] > B[i-1] and B[i] > A[i-1]: | ||
swap = 1 + minNotSwap | ||
notSwap = minSwap | ||
if A[i] > A[i-1] and B[i] > B[i-1]: | ||
swap = min(swap,1 + minSwap) | ||
notSwap = min(notSwap, minNotSwap) | ||
|
||
minSwap, minNotSwap = swap, notSwap | ||
|
||
return min(minSwap, minNotSwap) | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 712 ms submission | ||
class Solution: | ||
def eventualSafeNodes(self, graph): | ||
# return t if i will be in a cycle, f if it will not. | ||
|
||
def explore(i): | ||
visited[i] = 0 | ||
for v in graph[i]: | ||
# if neighbor has been visited before or after exploration, it is in a cycle | ||
# -1 => unvisited; 0 => visited; 1 => no cycle? | ||
if visited[v] == 0 or (visited[v] == -1 and explore(v)): return True | ||
visited[i] = 1 | ||
res.append(i) | ||
return False | ||
visited, res = [-1] * len(graph), [] | ||
for i in range(len(graph)): | ||
if visited[i] == -1: explore(i) | ||
return sorted(res) | ||
__________________________________________________________________________________________________ | ||
|
||
sample 724 ms submission | ||
class Solution: | ||
def eventualSafeNodes(self, graph: List[List[int]]) -> List[int]: | ||
#color[i], 0 means not visited. 1 means safe. 2 means unsafe. | ||
color=[0] * len(graph) | ||
res=[] | ||
for i in range(len(graph)): | ||
if self.dfs(graph,i,color): | ||
res.append(i) | ||
res.sort() | ||
return res | ||
|
||
def dfs(self,graph,i,color): | ||
if color[i] != 0: | ||
return color[i] == 1 | ||
color[i] = 2 | ||
for e in graph[i]: | ||
if not self.dfs(graph,e,color): | ||
return False | ||
color[i] = 1 | ||
return True | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,115 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 604 ms submission | ||
class Solution: | ||
def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]: | ||
if not grid or not grid[0]: | ||
return [] | ||
|
||
m, n = len(grid), len(grid[0]) | ||
EMPTY = 0 | ||
BRICK = 1 | ||
DROP = 2 | ||
WAIT = 3 | ||
|
||
def count(x, y): | ||
cnt = 1 | ||
grid[x][y] = DROP | ||
|
||
if x > 0 and grid[x - 1][y] == BRICK: | ||
cnt += count(x - 1, y) | ||
if y > 0 and grid[x][y - 1] == BRICK: | ||
cnt += count(x, y - 1) | ||
if x + 1 < m and grid[x + 1][y] == BRICK: | ||
cnt += count(x + 1, y) | ||
if y + 1 < n and grid[x][y + 1] == BRICK: | ||
cnt += count(x, y + 1) | ||
|
||
return cnt | ||
|
||
for x, y in hits: | ||
if grid[x][y] == BRICK: | ||
grid[x][y] = WAIT | ||
|
||
for y in range(n): | ||
if grid[0][y] == BRICK: | ||
count(0, y) | ||
|
||
ans = [] | ||
|
||
for x, y in hits[::-1]: | ||
if grid[x][y] == WAIT: | ||
grid[x][y] = BRICK | ||
|
||
if ( | ||
x == 0 or | ||
(x > 0 and grid[x - 1][y] == DROP) or | ||
(y > 0 and grid[x][y - 1] == DROP) or | ||
(x + 1 < m and grid[x + 1][y] == DROP) or | ||
(y + 1 < n and grid[x][y + 1] == DROP) | ||
): | ||
ans.append(count(x, y) - 1) | ||
else: | ||
ans.append(0) | ||
else: | ||
ans.append(0) | ||
|
||
return ans[::-1] | ||
__________________________________________________________________________________________________ | ||
|
||
sample 608 ms submission | ||
class Solution: | ||
""" | ||
T: O(m * n) | ||
S: O(m * n) | ||
""" | ||
def hitBricks(self, grid: List[List[int]], hits: List[List[int]]) -> List[int]: | ||
if not grid or not grid[0] or not hits: | ||
return [] | ||
|
||
m, n, k = len(grid), len(grid[0]), len(hits) | ||
EMPTY = 0 | ||
BRICK = 1 | ||
ERASURE = 2 | ||
DROPED = 3 | ||
|
||
def count(x, y): | ||
grid[x][y] = DROPED | ||
cnt = 1 | ||
|
||
if x > 0 and grid[x - 1][y] == BRICK: | ||
cnt += count(x - 1, y) | ||
if y > 0 and grid[x][y - 1] == BRICK: | ||
cnt += count(x, y - 1) | ||
if x + 1 < m and grid[x + 1][y] == BRICK: | ||
cnt += count(x + 1, y) | ||
if y + 1 < n and grid[x][y + 1] == BRICK: | ||
cnt += count(x, y + 1) | ||
|
||
return cnt | ||
|
||
for x, y in hits: | ||
if grid[x][y] == BRICK: | ||
grid[x][y] = ERASURE | ||
|
||
for y in range(n): | ||
if grid[0][y] == BRICK: | ||
count(0, y) | ||
|
||
ans = [0] * k | ||
|
||
for i in range(k - 1, -1, -1): | ||
x, y = hits[i] | ||
|
||
if grid[x][y] == ERASURE: | ||
grid[x][y] = BRICK | ||
|
||
if ( | ||
(x == 0) or | ||
(x > 0 and grid[x - 1][y] == DROPED) or | ||
(y > 0 and grid[x][y - 1] == DROPED) or | ||
(x + 1 < m and grid[x + 1][y] == DROPED) or | ||
(y + 1 < n and grid[x][y + 1] == DROPED) | ||
): | ||
ans[i] = count(x, y) - 1 | ||
|
||
return ans | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 24 ms submission | ||
class Solution: | ||
def uniqueMorseRepresentations(self, words: List[str]) -> int: | ||
morse_codes = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | ||
alphabet = {k: v for v, k in enumerate([letter for letter in "abcdefghijklmnopqrstuvwxyz"])} | ||
transformations = [] | ||
|
||
for word in words: | ||
transformation = "" | ||
|
||
for char in word: | ||
transformation += morse_codes[alphabet[char]] | ||
|
||
transformations.append(transformation) | ||
|
||
return len(list(set(transformations))) | ||
__________________________________________________________________________________________________ | ||
sample 12936 kb submission | ||
#!/user/bin/env python3 | ||
|
||
|
||
class Solution: | ||
table = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] | ||
|
||
def encode_c(self, c): | ||
return self.table[ord(c) - ord('a')] | ||
|
||
def encode(self, word): | ||
return ''.join(map(self.encode_c, word)) | ||
|
||
def uniqueMorseRepresentations(self, words: List[str]) -> int: | ||
results = set(self.encode(word) for word in words) | ||
return len(results) | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,84 @@ | ||
__________________________________________________________________________________________________ | ||
sample 40 ms submission | ||
class Solution: | ||
def splitArraySameAverage(self, A: List[int]) -> bool: | ||
L, total = len(A), sum(A) | ||
A.sort() | ||
|
||
'''个数不会超过数组总个数的一半 + 1''' | ||
for i in range(1, L // 2 + 1): | ||
''' | ||
i个数 x sum(A) == i个数之和 x len(A) | ||
所以 sum(A) x i ➗ len(A) == i个数之和 必然是整数 | ||
''' | ||
if total * i % L: | ||
continue | ||
|
||
|
||
''' 从数组A中,找到k个数之和等于target的可能性''' | ||
k, target, index = i, total * i // L, 0 | ||
|
||
if self.dfs(A, k, target, index): | ||
return True | ||
|
||
return False | ||
|
||
|
||
''' | ||
从数组A中,找到k个数之和等于target的可能性 | ||
combination sum II 或者 k sum那两道题的简易版 | ||
''' | ||
def dfs(self, A, k, target, index): | ||
if not k and not target: | ||
return True | ||
|
||
if not k or target < 0: | ||
return False | ||
|
||
for i in range(index, len(A)): | ||
if i > index and A[i] == A[i -1]: | ||
continue | ||
|
||
if target < A[i] * k or target > A[-1] * k: | ||
break | ||
|
||
if self.dfs(A, k - 1, target - A[i], i + 1): | ||
return True | ||
|
||
return False | ||
__________________________________________________________________________________________________ | ||
|
||
sample 44 ms submission | ||
class Solution: | ||
def splitArraySameAverage(self, A: List[int]) -> bool: | ||
A.sort() | ||
|
||
L, total = len(A), sum(A) | ||
|
||
for k in range(1, L // 2 + 1): | ||
if total * k % L: | ||
continue | ||
|
||
start_index, target = 0, total * k // L | ||
if self.dfs(A, k, start_index, target): | ||
return True | ||
|
||
return False | ||
|
||
|
||
def dfs(self, A, k, start_index, target): | ||
if k == 0 and target == 0: | ||
return True | ||
|
||
if k == 0 or target <= 0: | ||
return False | ||
|
||
if start_index < len(A) and target < k * A[start_index] or target > k * A[-1]: | ||
return False | ||
|
||
for i in range(start_index, len(A)): | ||
if i > start_index and A[i] == A[i - 1]: | ||
continue | ||
|
||
if self.dfs(A, k - 1, i + 1, target - A[i]): | ||
return True | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 24 ms submission | ||
class Solution: | ||
def numberOfLines(self, widths: List[int], S: str) -> List[int]: | ||
|
||
letters = 'abcdefghijklmnopqrstuvwxyz' | ||
keys = list(letters) | ||
print(keys) | ||
|
||
letter_width = dict(zip(keys, widths)) | ||
|
||
ptr = 0 | ||
lines = 1 | ||
for i in range(len(S)): | ||
if ptr + letter_width[S[i]] > 100: | ||
lines += 1 | ||
ptr = letter_width[S[i]] | ||
else: | ||
ptr += letter_width[S[i]] | ||
|
||
return [lines, ptr] | ||
__________________________________________________________________________________________________ | ||
sample 13000 kb submission | ||
class Solution: | ||
def numberOfLines(self, widths, S): | ||
lines, width = 1, 0 | ||
for c in S: | ||
w = widths[ord(c) - ord('a')] | ||
width += w | ||
if width > 100: | ||
lines += 1 | ||
width = w | ||
|
||
return lines, width | ||
__________________________________________________________________________________________________ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
__________________________________________________________________________________________________ | ||
sample 68 ms submission | ||
class Solution: | ||
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: | ||
row_maxes = [max(row) for row in grid] | ||
col_maxes = [max(col) for col in zip(*grid)] | ||
|
||
return sum(min(row_maxes[r], col_maxes[c]) - val | ||
for r, row in enumerate(grid) | ||
for c, val in enumerate(row)) | ||
__________________________________________________________________________________________________ | ||
|
||
sample 13112 kb submission | ||
class Solution: | ||
def maxIncreaseKeepingSkyline(self, grid: List[List[int]]) -> int: | ||
row, col = list(map(max, grid)), list(map(max, zip(*grid))) | ||
return sum(min(i, j) for i in row for j in col) - sum(map(sum, grid)) | ||
__________________________________________________________________________________________________ |
Oops, something went wrong.