389-Find-The-Difference
https://leetcode.com/problems/find-the-difference
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
count = Counter(s)
for i, c in enumerate(t):
count[c] -= 1
if count[c] == -1:
return c
new Solution().findTheDifference …
Read More
39-Combination-Sum
https://leetcode.com/problems/combination-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
ans = []
def dfs(s: int, target: int, path: List[int]) -> None:
if target < 0:
return
if target == 0 …
Read More
394-Decode-String
https://leetcode.com/problems/decode-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def decodeString(self, s: str) -> str:
stack = [] # (prevStr, repeatCount)
currStr = ''
currNum = 0
for c in s:
if c.isdigit():
currNum = currNum * 10 + int(c)
else:
if c == '[':
stack.append …
Read More
396-Rotate-Function
https://leetcode.com/problems/rotate-function
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
f = sum(i * num for i, num in enumerate(nums))
ans = f
summ = sum(nums)
for a in reversed(nums):
f += summ …
Read More
397-Integer-Replacement
https://leetcode.com/problems/integer-replacement
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def integerReplacement(self, n: int) -> int:
ans = 0
while n > 1:
if (n & 1) == 0:
n >>= 1
elif n == 3 or ((n >> 1) & 1) == 0:
n -= 1
else:
n …
Read More
399-Evaluate-Division
https://leetcode.com/problems/evaluate-division
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
ans = []
# graph[A][B] := A / B
graph = defaultdict(dict)
for (A, B), value …
Read More
4-Median-Of-Two-Sorted-Arrays
https://leetcode.com/problems/median-of-two-sorted-arrays
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
n1 = len(nums1)
n2 = len(nums2)
if n1 > n2:
return self.findMedianSortedArrays(nums2, nums1)
l = 0
r = n1
while …
Read More
40-Combination-Sum-Ii
https://leetcode.com/problems/combination-sum-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
ans = []
def dfs(s: int, target: int, path: List[int]) -> None:
if target < 0:
return
if target == 0 …
Read More
403-Frog-Jump
https://leetcode.com/problems/frog-jump
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canCross(self, stones: List[int]) -> bool:
n = len(stones)
# dp[i][j] := True if a frog can make a size j jump to stones[i]
dp = [[False] * (n …
Read More
409-Longest-Palindrome
https://leetcode.com/problems/longest-palindrome
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestPalindrome(self, s: str) -> int:
ans = 0
count = Counter(s)
for c in count.values():
ans += c if c % 2 == 0 else c - 1
hasOddCount = any(c % 2 …
Read More