371-Sum-Of-Two-Integers
https://leetcode.com/problems/sum-of-two-integers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getSum(self, a: int, b: int) -> int:
mask = 0xFFFFFFFF
kMax = 2000
while b:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
return a if a < kMax else …
Read More
372-Super-Pow
https://leetcode.com/problems/super-pow
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def superPow(self, a: int, b: List[int]) -> int:
def powMod(x: int, y: int) -> int:
pow = 1
for _ in range(y):
pow = (pow * x) % k
return pow …
Read More
375-Guess-Number-Higher-Or-Lower-Ii
https://leetcode.com/problems/guess-number-higher-or-lower-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getMoneyAmount(self, n: int) -> int:
# Dp(i, j) := min money you need to guarantee a win of picking i..j
@functools.lru_cache(None)
def dp(i: int, j …
Read More
377-Combination-Sum-Iv
https://leetcode.com/problems/combination-sum-iv
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def combinationSum4(self, nums: List[int], target: int) -> int:
dp = [1] + [-1] * target
def dfs(target: int) -> int:
if target < 0:
return 0
if dp[target] != -1:
return dp …
Read More
378-Kth-Smallest-Element-In-A-Sorted-Matrix
https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
minHeap = [] # (matrix[i][j], i, j)
i = 0
while i < k and i < len(matrix):
heapq.heappush(minHeap, (matrix …
Read More
38-Count-And-Say
https://leetcode.com/problems/count-and-say
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countAndSay(self, n: int) -> str:
ans = '1'
for _ in range(n - 1):
nxt = ''
i = 0
while i < len(ans):
count = 1
while i + 1 < len(ans) and …
Read More
383-Ransom-Note
https://leetcode.com/problems/ransom-note
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
count1 = Counter(ransomNote)
count2 = Counter(magazine)
return all(count1[c] <= count2[c] for c in string.ascii_lowercase)
new Solution().canConstruct()
Score …
Read More
384-Shuffle-An-Array
https://leetcode.com/problems/shuffle-an-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
return self.nums
def shuffle …
Read More
385-Mini-Parser
https://leetcode.com/problems/mini-parser
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def deserialize(self, s: str) -> NestedInteger:
if s[0] != '[':
return NestedInteger(int(s))
stack = []
for i, c in enumerate(s):
if c == '[':
stack.append(NestedInteger())
start = i + 1
elif …
Read More
387-First-Unique-Character-In-A-String
https://leetcode.com/problems/first-unique-character-in-a-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def firstUniqChar(self, s: str) -> int:
count = Counter(s)
for i, c in enumerate(s):
if count[c] == 1:
return i
return -1
new Solution().firstUniqChar()
Score: 5
Read More