-
-
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
9 changed files
with
374 additions
and
14 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,40 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 24 ms submission | ||
class Solution: | ||
def readBinaryWatch(self, num: int) -> List[str]: | ||
out = [] | ||
for i in range(12): | ||
for j in range(60): | ||
if bin(j).count('1') == num - bin(i).count('1'): | ||
out.append("{}:{:02d}".format(i, j)) | ||
return out | ||
__________________________________________________________________________________________________ | ||
|
||
sample 12940 kb submission | ||
class Solution: | ||
def readBinaryWatch(self, num: int) -> List[str]: | ||
|
||
ans = [] | ||
|
||
def bitCount( n ): | ||
# compute the number of 1s in the binary representation of n | ||
|
||
count = 0 | ||
while n > 0: | ||
if n%2 == 1: | ||
count += 1 | ||
n = n >> 1 | ||
|
||
return count | ||
|
||
def convert(H, M): | ||
# convert to string time 0<= H < 12 | ||
return str(H) + ":" + ( "0" if M < 10 else "" ) + str(M) | ||
|
||
|
||
for H in range(0, 12): | ||
for M in range(0, 60): | ||
if bitCount(H) + bitCount(M) == num: | ||
ans.append( convert(H, M) ) | ||
|
||
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,42 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 32 ms submission | ||
# | ||
# @lc app=leetcode id=402 lang=python3 | ||
# | ||
# [402] Remove K Digits | ||
# | ||
class Solution: | ||
def removeKdigits(self, num: str, k: int) -> str: | ||
out = [] | ||
for c in num: | ||
while k > 0 and out and out[-1] > c: | ||
out.pop() | ||
k -= 1 | ||
out.append(c) | ||
return ''.join(out[:-k or None]).lstrip('0') or '0' | ||
__________________________________________________________________________________________________ | ||
|
||
sample 13060 kb submission | ||
class Solution: | ||
def removeKdigits(self, num: str, k: int) -> str: | ||
if k >= len(num): | ||
return '0' | ||
|
||
changed = False | ||
while k > 0: | ||
for i in range(len(num) - 1): | ||
if num[i] > num[i+1]: | ||
num = num[:i] + num[i+1:] | ||
changed = True | ||
break | ||
|
||
if not changed: | ||
num = num[:-1] | ||
k -= 1 | ||
changed = False | ||
|
||
i = 0 | ||
while i < len(num) - 1 and num[i] == '0': | ||
i += 1 | ||
|
||
return num[i:] | ||
__________________________________________________________________________________________________ |
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,43 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 72 ms submission | ||
from collections import deque | ||
class Solution: | ||
def canCross(self, s: List[int]) -> bool: | ||
stones = set(s) | ||
step = 1 | ||
for i in range(len(s) - 1): | ||
if s[i + 1] - s[i] > step: return False | ||
step += 1 | ||
def helper(start, end, step): | ||
if start == end: return True | ||
if start not in stones: return False | ||
if helper(start + step + 1, end, step + 1): return True | ||
if helper(start + step, end, step): return True | ||
if step > 1 and helper(start + step - 1, end, step - 1): return True | ||
return False | ||
return helper(1, s[-1], 1) | ||
__________________________________________________________________________________________________ | ||
|
||
sample 13376 kb submission | ||
class Solution: | ||
def canCross(self, stones: List[int]) -> bool: | ||
if (len(stones) - 1 > stones[-1] | ||
or stones[-1] > (len(stones) - 1)*(len(stones))/2): | ||
return False | ||
|
||
last_stone = stones[-1] # keep the value of the last stone | ||
stones = set(stones) # convert the stones positions into set, to have O(1) check using "in" | ||
jumps_stack = [(1, 1)] # we are at the second stone with k = 1 | ||
|
||
while jumps_stack: | ||
current_stone = jumps_stack.pop() # pop the last element from stack | ||
pos = current_stone[0] | ||
k = current_stone[1] | ||
for dk in [-1,0,1]: | ||
candidate_stone = pos + k + dk | ||
if candidate_stone in stones: | ||
if pos + k + dk == last_stone: # if we have reached the last stone | ||
return True | ||
if k + dk > 0: # avoid situations when k = 1 and dk = -1, since sum is 0 | ||
jumps_stack.append((candidate_stone, k + dk)) | ||
return False | ||
__________________________________________________________________________________________________ |
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,51 @@ | ||
__________________________________________________________________________________________________ | ||
sample 20 ms submission | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution: | ||
def sumOfLeftLeaves(self, root): | ||
self.sum = 0 | ||
def dfs(node): | ||
if not node: | ||
return | ||
if node.left: | ||
if not node.left.left and not node.left.right: | ||
self.sum += node.left.val | ||
else: | ||
dfs(node.left) | ||
if node.right: | ||
dfs(node.right) | ||
return | ||
dfs(root) | ||
return self.sum | ||
__________________________________________________________________________________________________ | ||
sample 13296 kb submission | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution: | ||
def sumOfLeftLeaves(self, root: TreeNode) -> int: | ||
if not root: | ||
return 0 | ||
ans = 0 | ||
stack = [(root,False)] | ||
while stack: | ||
cur, side = stack.pop() | ||
if not cur.left and not cur.right and side: | ||
ans += cur.val | ||
continue | ||
if cur.left: | ||
stack.append((cur.left,True)) | ||
if cur.right: | ||
stack.append((cur.right,False)) | ||
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,33 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 16 ms submission | ||
class Solution: | ||
def toHex(self, num: int) -> str: | ||
if num == 0: return '0' | ||
x = num.to_bytes(4, byteorder='big', signed=True).hex() | ||
count = 0 | ||
for i in x: | ||
if i == '0': | ||
count += 1 | ||
else: | ||
break | ||
return x[count:] | ||
__________________________________________________________________________________________________ | ||
|
||
sample 13152 kb submission | ||
class Solution: | ||
def toHex(self, num: int) -> str: | ||
ans = '' | ||
if num < 0: | ||
num = 0x100000000 + num | ||
while num > 0: | ||
digit = num % 16 | ||
if digit < 10: | ||
ans = chr(ord('0') + digit) + ans | ||
else: | ||
ans = chr(ord('a') + digit - 10) + ans | ||
num //= 16 | ||
|
||
if len(ans) == 0: | ||
return '0' | ||
else: | ||
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,23 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 96 ms submission | ||
class Solution: | ||
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: | ||
people.sort(key=lambda p: (-p[0], p[1])) | ||
|
||
queue = [] | ||
for person in people: | ||
queue.insert(person[1], person) | ||
|
||
return queue | ||
__________________________________________________________________________________________________ | ||
sample 13332 kb submission | ||
from collections import deque | ||
class Solution: | ||
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]: | ||
order = deque() | ||
people.sort(key = lambda x: (-x[0], x[1])) | ||
for pair in people: | ||
order.insert(pair[1], pair) | ||
|
||
return list(order) | ||
__________________________________________________________________________________________________ |
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,73 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 164 ms submission | ||
class Solution: | ||
def trapRainWater(self, heightMap: List[List[int]]) -> int: | ||
if not heightMap or not heightMap[0]: | ||
return 0 | ||
m = len(heightMap) | ||
n = len(heightMap[0]) | ||
|
||
heap = [] | ||
seen = [[False] * n for _ in range(m)] | ||
|
||
# Puts the edges on the heap | ||
for i in range(m): | ||
heap.append( (heightMap[i][0], i, 0) ) | ||
heap.append( (heightMap[i][n - 1], i, n - 1) ) | ||
seen[i][0] = True | ||
seen[i][n - 1] = True | ||
for j in range(n): | ||
heap.append( (heightMap[0][j], 0, j) ) | ||
heap.append( (heightMap[m - 1][j], m - 1, j) ) | ||
seen[0][j] = True | ||
seen[m - 1][j] = True | ||
heapq.heapify(heap) | ||
|
||
area = 0 | ||
while heap: | ||
h, i, j = heapq.heappop(heap) | ||
if h > heightMap[i][j]: | ||
area += h - heightMap[i][j] | ||
|
||
if i > 0 and not seen[i - 1][j]: | ||
heapq.heappush(heap, (max(h, heightMap[i - 1][j]), i - 1, j)) | ||
seen[i - 1][j] = True | ||
if i + 1 < m and not seen[i + 1][j]: | ||
heapq.heappush(heap, (max(h, heightMap[i + 1][j]), i + 1, j)) | ||
seen[i + 1][j] = True | ||
if j > 0 and not seen[i][j - 1]: | ||
heapq.heappush(heap, (max(h, heightMap[i][j - 1]), i, j - 1)) | ||
seen[i][j - 1] = True | ||
if j + 1 < n and not seen[i][j + 1]: | ||
heapq.heappush(heap, (max(h, heightMap[i][j + 1]), i, j + 1)) | ||
seen[i][j + 1] = True | ||
|
||
return area | ||
__________________________________________________________________________________________________ | ||
|
||
sample 14168 kb submission | ||
class Solution: | ||
def trapRainWater(self, heightMap): | ||
""" | ||
:type heightMap: List[List[int]] | ||
:rtype: int | ||
""" | ||
if not any(heightMap): | ||
return 0 | ||
m, n = len(heightMap), len(heightMap[0]) | ||
boundaries = [] | ||
for i, row in enumerate(heightMap): | ||
for j, val in enumerate(row): | ||
if i in (0, m - 1) or j in (0, n - 1): | ||
boundaries.append((val, i, j)) | ||
heightMap[i][j] = -1 | ||
heapq.heapify(boundaries) | ||
ans = 0 | ||
while boundaries: | ||
height, i, j = heapq.heappop(boundaries) | ||
for newi, newj in (i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1): | ||
if 0 <= newi < m and 0 <= newj < n and heightMap[newi][newj] != -1: | ||
ans += max(0, height - heightMap[newi][newj]) | ||
heapq.heappush(boundaries, (max(height, heightMap[newi][newj]), newi, newj)) | ||
heightMap[newi][newj] = -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,45 @@ | ||
__________________________________________________________________________________________________ | ||
|
||
sample 24 ms submission | ||
class Solution: | ||
def longestPalindrome(self, s: str) -> int: | ||
|
||
counter = {} | ||
for c in s: | ||
if c in counter: | ||
counter[c] += 1 | ||
else: | ||
counter[c] = 1 | ||
|
||
has_isolated = False | ||
|
||
l = 0 | ||
for _, v in counter.items(): | ||
if v % 2 == 1: | ||
has_isolated = True | ||
l += v - 1 | ||
else: | ||
l += v | ||
|
||
if has_isolated: | ||
l += 1 | ||
return l | ||
__________________________________________________________________________________________________ | ||
sample 13004 kb submission | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
def longestPalindrome(self, s: str) -> int: | ||
d = defaultdict(int) | ||
for c in s: | ||
d[c] += 1 | ||
|
||
count = 0 | ||
for v in d.values(): | ||
n = v // 2 * 2 | ||
count += n | ||
|
||
if len(s) > count: | ||
count += 1 | ||
|
||
return count | ||
__________________________________________________________________________________________________ |
Oops, something went wrong.