锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #Runtime: 177 ms (Beats 26.93%)
3 #Memory: 17.4 MB (Beats 96.45%)
4
5 class Solution(object):
6 def productExceptSelf(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[int]
10 """
11 t = 1
12 ans = []
13 for i in xrange(len(nums)):
14 ans.append(t)
15 t *= nums[i]
16 t = 1
17 for i in xrange(len(nums) - 1, -1, -1):
18 ans[i] *= t
19 t *= nums[i]
20 return ans
21
]]>
2
3 class Solution(object):
4 def sortedSquares(self, nums):
5 """
6 :type nums: List[int]
7 :rtype: List[int]
8 """
9 l = 0
10 r = len(nums) - 1
11 ans = []
12 while l <= r:
13 if nums[l] * nums[l] > nums[r] * nums[r]:
14 ans.append(nums[l] * nums[l])
15 l += 1
16 else:
17 ans.append(nums[r] * nums[r])
18 r -= 1
19 return ans[::-1]
]]>
2 #Runtime: 998 ms (Beats 89.58%)
3 #Memory: 23.6 MB (Beats 98.27%)
4
5 class Solution(object):
6 def dailyTemperatures(self, temperatures):
7 """
8 :type temperatures: List[int]
9 :rtype: List[int]
10 """
11 ans = [0] * len(temperatures)
12 stk = []
13 for i, t in enumerate(temperatures):
14 if not stk:
15 stk.append(i)
16 else:
17 while stk and temperatures[stk[-1]] < t:
18 idx = stk.pop()
19 ans[idx] = i - idx
20 stk.append(i)
21 return ans
]]>
2 #Runtime: 9219 ms (Beats 7.69%)
3 #Memory: 12.6 MB (Beats 100%)
4
5 class Solution(object):
6 def numSubmatrixSumTarget(self, matrix, target):
7 """
8 :type matrix: List[List[int]]
9 :type target: int
10 :rtype: int
11 """
12 ans = 0
13 n, m = len(matrix), len(matrix[0])
14 pre_sum = [[0] * (m + 1) for _ in xrange(n + 1)]
15 for i in xrange(1, n + 1):
16 for j in xrange(1, m + 1):
17 pre_sum[i][j] = pre_sum[i - 1][j] + pre_sum[i][j - 1] + matrix[i - 1][j - 1] - pre_sum[i - 1][j - 1]
18 for x in xrange(0, i):
19 for y in xrange(0, j):
20 if target == pre_sum[i][j] + pre_sum[x][y] - pre_sum[x][j] - pre_sum[i][y]:
21 ans += 1
22 return ans
]]>
#Runtime: 118 ms (Beats 39.13%)
#Memory: 11.7 MB (Beats 100%)
class Solution(object):
def findPaths(self, m, n, maxMove, startRow, startColumn):
"""
:type m: int
:type n: int
:type maxMove: int
:type startRow: int
:type startColumn: int
:rtype: int
"""
MOD = 10**9+7
dp = [[0] * n for _ in xrange(m)]
dp[startRow][startColumn] = 1
cnt = 0
for _ in xrange(1, maxMove + 1):
t = [[0] * n for _ in xrange(m)]
for i in xrange(m):
for j in xrange(n):
if i == m - 1:
cnt = (cnt + dp[i][j]) % MOD
if j == n - 1:
cnt = (cnt + dp[i][j]) % MOD
if i == 0:
cnt = (cnt + dp[i][j]) % MOD
if j == 0:
cnt = (cnt + dp[i][j]) % MOD
t[i][j] = ((dp[i - 1][j] if i > 0 else 0) % MOD + (dp[i + 1][j] if i < m - 1 else 0) % MOD + (dp[i][j - 1] if j > 0 else 0) % MOD + (dp[i][j + 1] if j < n - 1 else 0) % MOD) % MOD
dp = t
return cnt
]]>
2 #Runtime: 911 ms (Beats 96.43%)
3 #Memory: 136.7 MB (Beats 10.71%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def pseudoPalindromicPaths (self, root, cnt = 0):
13 """
14 :type root: TreeNode
15 :rtype: int
16 """
17 if not root:
18 return 0
19 cnt ^= 1 << (root.val - 1)
20 if root.left is None and root.right is None:
21 return 1 if cnt & (cnt - 1) == 0 else 0
22 return self.pseudoPalindromicPaths(root.left, cnt) + self.pseudoPalindromicPaths(root.right, cnt)
23
]]>
#Runtime: 97 ms (Beats 50%)
#Memory: 53.4 MB (Beats 8.49%)
class Solution(object):
def maxLength(self, arr):
"""
:type arr: List[str]
:rtype: int
"""
candidate = []
for s in arr:
u = set(s)
if len(s) == len(u):
candidate.append(u)
ans = [set()]
maxx = 0
for i in candidate:
for j in ans:
if not i & j:
ans.append(i | j)
maxx = max(maxx, len(i | j))
return maxx
]]>
2 #Runtime: 60 ms (Beats 13.89%)
3 #Memory: 13.3 MB (Beats 80.56%)
4
5 class Solution(object):
6 def findMatrix(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: List[List[int]]
10 """
11 ans = []
12 cnt = Counter(nums)
13 while cnt:
14 tp = []
15 used = []
16 for i, v in cnt.items():
17 tp.append(i)
18 cnt[i] -= 1
19 if cnt[i] == 0:
20 used.append(i)
21 for i in used:
22 cnt.pop(i)
23 ans.append(tp)
24 return ans
]]>
2 #Runtime: 230 ms (Beats 12.76%)
3 #Memory: 15 MB (Beats 6.38%)
4
5 class Solution(object):
6 def findContentChildren(self, g, s):
7 """
8 :type g: List[int]
9 :type s: List[int]
10 :rtype: int
11 """
12 g.sort()
13 s.sort()
14 p1, p2 = 0, 0
15 while p1 < len(g) and p2 < len(s):
16 if g[p1] <= s[p2]:
17 p1 += 1
18 p2 += 1
19 else:
20 p2 += 1
21 return p1
]]>
2 #Runtime: 67 ms (Beats 16.67%)
3 #Memory: 13.6 MB (Beats 83.33%)
4
5 class Solution(object):
6 def makeEqual(self, words):
7 """
8 :type words: List[str]
9 :rtype: bool
10 """
11 total = ''.join(words)
12 chars = set(total)
13 for ch in chars:
14 if total.count(ch) % len(words) != 0:
15 return False
16 return True
]]>
#Runtime: 812 ms (Beats 62.22%)
#Memory: 22.1 MB (Beats 88.89%)
class Solution(object):
def minCost(self, colors, neededTime):
"""
:type colors: str
:type neededTime: List[int]
:rtype: int
"""
ans = 0
for i in xrange(1, len(neededTime)):
if colors[i] == colors[i - 1]:
ans += min(neededTime[i], neededTime[i - 1])
neededTime[i] = max(neededTime[i - 1], neededTime[i])
return ans
]]>
2 #Runtime: 22 ms (Beats 17.34%)
3 #Memory: 13.4 MB (Beats 71.65%)
4
5 class Solution(object):
6 def numDecodings(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 dp = [0] * (len(s) + 1)
12 dp[0] = 1
13 for i in xrange(0, len(s)):
14 if i == 0:
15 if s[i] == '0':
16 return 0
17 dp[i + 1] = 1
18 else:
19 if 10 <= int(s[i - 1 : i + 1]) <= 26:
20 dp[i + 1] += dp[i - 1]
21 if 1 <= int(s[i]) <= 9:
22 dp[i + 1] += dp[i]
23 return dp[-1]
]]>
#Runtime: 25 ms (Beats 79.41%)
#Memory: 13.4 MB (Beats 88.24)
class Solution(object):
def minOperations(self, s):
"""
:type s: str
:rtype: int
"""
ans1, ans2 = 0, 0
for i in xrange(0, len(s)):
if i % 2:
if s[i] == '0':
ans1 += 1
else:
ans2 += 1
else:
if s[i] == '1':
ans1 += 1
else:
ans2 += 1
return min(ans1, ans2)
]]>
2 #Runtime: 16 ms (Beats 59.29%)
3 #Memory: 13.5 MB (Beats 76.11%)
4
5 class Solution(object):
6 def isPathCrossing(self, path):
7 """
8 :type path: str
9 :rtype: bool
10 """
11 x, y = 0, 0
12 cor = {(x, y)}
13 for ch in path:
14 if ch == 'N':
15 y += 1
16 elif ch == 'S':
17 y -= 1
18 elif ch == 'E':
19 x += 1
20 else:
21 x -= 1
22 if (x, y) in cor:
23 return True
24 cor.add((x, y))
25 return False
]]>
2 #Runtime: 13 ms (Beats 90.28%)
3 #Memory: 13.3 MB (Beats 56.94%)
4
5 class Solution(object):
6 def maxScore(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 ones = s[1:].count('1')
12 if s[0] == '0':
13 zeros = 1
14 else:
15 zeros = 0
16 ans = ones + zeros
17 for i in xrange(1, len(s) - 1):
18 if s[i] == '0':
19 zeros += 1
20 else:
21 ones -= 1
22 ans = max(ans, ones + zeros)
23 return ans
]]>
#Runtime: 20 ms (Beats 96.2%)
#Memory: 13.7 MB (Beats 69.85%)
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t):
return False
for i in set(s):
if s.count(i) != t.count(i):
return False
return True
]]>
2 #Runtime: 34 ms (Beats 59.12%)
3 #Memory: 13.6 MB (Beats 19.34%)
4
5 class Solution(object):
6 def destCity(self, paths):
7 """
8 :type paths: List[List[str]]
9 :rtype: str
10 """
11 st = set()
12 cities = set()
13 for x, y in paths:
14 st.add(x)
15 cities.add(x)
16 cities.add(y)
17 for x in cities:
18 if x not in st:
19 return x
]]>
2 #Runtime: 1162 ms (Beats 74.29%)
3 #Memory: 44.6 MB (Beats 80%)
4
5 class Solution(object):
6 def onesMinusZeros(self, grid):
7 """
8 :type grid: List[List[int]]
9 :rtype: List[List[int]]
10 """
11 n, m = len(grid), len(grid[0])
12 r = [0] * n
13 c = [0] * m
14 for i in xrange(n):
15 for j in xrange(m):
16 r[i] += grid[i][j]
17 c[j] += grid[i][j]
18 for i in xrange(n):
19 for j in xrange(m):
20 grid[i][j] = 2 * r[i] + 2 * c[j] - n - m
21 return grid
]]>
2 #Runtime: 121 ms (Beats 70.97%)
3 #Memory: 13.6 MB (Beats 35.48%)
4
5 class Solution(object):
6 def numSpecial(self, mat):
7 """
8 :type mat: List[List[int]]
9 :rtype: int
10 """
11 ans = 0
12 for i in xrange(len(mat)):
13 if mat[i].count(1) == 1:
14 x = mat[i].index(1)
15 col = [r[x] for r in mat]
16 if col.count(1) == 1:
17 ans += 1
18 return ans
]]>
2 #Runtime: 27 ms (Beats 98.59%)
3 #Memory: 15.3 MB (Beats 81.69%)
4
5 class Solution(object):
6 def largestOddNumber(self, num):
7 """
8 :type num: str
9 :rtype: str
10 """
11 p = -1
12 for i in xrange(len(num) - 1, -1, -1):
13 if (ord(num[i]) - ord('0')) % 2:
14 p = i
15 break
16 return num[0:p+1]
17
]]>
2 #Runtime: 44 ms (Beats 7.3%)
3 #Memory: 16.2 MB (Beats 35.94%)
4
5 # Definition for a binary tree node.
6 # class TreeNode(object):
7 # def __init__(self, val=0, left=None, right=None):
8 # self.val = val
9 # self.left = left
10 # self.right = right
11 class Solution(object):
12 def tree2str(self, root):
13 """
14 :type root: TreeNode
15 :rtype: str
16 """
17 self.ans = ""
18 def DFS(node):
19 self.ans += str(node.val)
20 if node.left or node.right:
21 self.ans += '('
22 if node.left:
23 DFS(node.left)
24 self.ans += ')'
25 if node.right:
26 self.ans += '('
27 DFS(node.right)
28 self.ans += ')'
29
30 if root:
31 DFS(root)
32 return self.ans
]]>
2 #Runtime: 17 ms (Beats 71.79%)
3 #Memory: 13.4 MB (Beats 46.15%)
4
5 class Solution(object):
6 def largestGoodInteger(self, num):
7 """
8 :type num: str
9 :rtype: str
10 """
11 x = '/'
12 t = 1
13 for i in xrange(1, len(num)):
14 if num[i] == num[i - 1]:
15 t += 1
16 if t >= 3:
17 x = max(x, num[i])
18 print(x)
19 else:
20 t = 1
21 if x >= '0':
22 return x * 3
23 return ""
]]>
2 #Runtime: 71 ms (Beats 71.98%)
3 #Memory: 14.4 MB (Beats 7.69%)
4
5 class Solution(object):
6 def countCharacters(self, words, chars):
7 """
8 :type words: List[str]
9 :type chars: str
10 :rtype: int
11 """
12 ans = ''
13 for w in words:
14 for ch in w:
15 if chars.count(ch) < w.count(ch):
16 break
17 else:
18 ans += w
19 return len(ans)
]]>
2 #Runtime: 1041 ms (Beats 11.18%)
3 #Memory: 44.8 MB (Beats 45.71%)
4
5 class Solution(object):
6 def largestSubmatrix(self, matrix):
7 """
8 :type matrix: List[List[int]]
9 :rtype: int
10 """
11 r, c = len(matrix), len(matrix[0])
12 ans = 0
13 for i in xrange(1, r):
14 for j in xrange(c):
15 matrix[i][j] += matrix[i - 1][j] if matrix[i][j] else 0
16 for i in xrange(r):
17 matrix[i].sort(reverse=True)
18 for j in xrange(c):
19 ans = max(ans, (j + 1) * matrix[i][j])
20 return ans
]]>
寰堝鐨勬濊礬鍙傝?-> https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/solutions/2273798/easy-to-understand-6-line-solution-with-explanation-o-n-time-o-1-space/?envType=daily-question&envId=2023-11-30
2 #Runtime: 17 ms (Beats 66.67%)
3 #Memory: 13.4 MB (Beats 33.33%)
4
5 class Solution(object):
6 def minimumOneBitOperations(self, n):
7 """
8 :type n: int
9 :rtype: int
10 """
11 binary = format(n, "b")
12 ans = 0
13 for i in xrange(1, len(binary) + 1):
14 if binary[-i] == "1":
15 ans = 2**i - 1 - ans
16 return ans
]]>
2 #Runtime: 724 ms (Beats 27.21%)
3 #Memory: 40.3 MB (Beats 41.50%)
4
5 class Solution(object):
6 def findDiagonalOrder(self, nums):
7 """
8 :type nums: List[List[int]]
9 :rtype: List[int]
10 """
11 ans = defaultdict(list)
12 for x in xrange(len(nums)):
13 for y in xrange(len(nums[x])):
14 ans[x + y].append(nums[x][y])
15 return [y for x in ans.keys() for y in reversed(ans[x])]
]]>
#Runtime: 586 ms (Beats 78.13%)
#Memory: 23 MB (Beats 43.75%)
class Solution(object):
def countNicePairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = [x - int(str(x)[::-1]) for x in nums]
ans = 0
for x in Counter(nums).values():
ans = (ans + (x - 1) * x // 2) % (10**9 + 7)
return ans
]]>
2 #Runtime: 900 ms (Beats 27%)
3 #Memory: 42.5 MB (Beats 81%)
4
5 class Solution(object):
6 def garbageCollection(self, garbage, travel):
7 """
8 :type garbage: List[str]
9 :type travel: List[int]
10 :rtype: int
11 """
12 ans = 0
13 last = [-1, -1, -1]
14 cnt = [0, 0, 0]
15 for i, st in enumerate(garbage):
16 for ch in st:
17 if ch == 'M':
18 last[0] = i
19 if ch == 'P':
20 last[1] = i
21 if ch == 'G':
22 last[2] = i
23 ans += len(st)
24 for i in xrange(len(travel)):
25 if last[0] >= i + 1:
26 ans += travel[i]
27 if last[1] >= i + 1:
28 ans += travel[i]
29 if last[2] >= i + 1:
30 ans += travel[i]
31 return ans
]]>
2 #Runtime: 768 ms (Beats 100%)
3 #Memory: 19.2 MB (Beats 66.67%)
4
5 class Solution(object):
6 def reductionOperations(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 nums.sort()
12 nt_sum = 0
13 pre = nums[-1]
14 nt = 1
15 ans = 0
16 for i in xrange(len(nums) - 2, -1, -1):
17 if nums[i] == pre:
18 nt += 1
19 else:
20 nt_sum += nt
21 ans += nt_sum
22 nt = 1
23 pre = nums[i]
24 return ans
]]>
2 #Runtime: 939 ms (Beats 68.83%)
3 #Memory: 25.7 MB (Beats 14.29%)
4
5 class Solution(object):
6 def minPairSum(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 nums.sort()
12 ans = 0
13 n = len(nums)
14 for i in xrange(n//2):
15 ans = max(ans, nums[i] + nums[n - i - 1])
16 return ans
]]>