67-Add-Binary
https://leetcode.com/problems/add-binary
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addBinary(self, a: str, b: str) -> str:
s = []
carry = 0
i = len(a) - 1
j = len(b) - 1
while i >= 0 or j >= 0 or carry:
if i …
Read More
670-Maximum-Swap
https://leetcode.com/problems/maximum-swap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maximumSwap(self, num: int) -> int:
s = list(str(num))
dict = {c: i for i, c in enumerate(s)}
for i, c in enumerate(s):
for digit in reversed …
Read More
672-Bulb-Switcher-Ii
https://leetcode.com/problems/bulb-switcher-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def flipLights(self, n: int, m: int) -> int:
n = min(n, 3)
if m == 0:
return 1
if m == 1:
return [2, 3, 4][n - 1]
if m == 2 …
Read More
673-Number-Of-Longest-Increasing-Subsequence
https://leetcode.com/problems/number-of-longest-increasing-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
ans = 0
maxLength = 0
length = [1] * len(nums) # length[i] := LIS's length ending w/ nums[i]
count = [1] * len(nums) # count …
Read More
674-Longest-Continuous-Increasing-Subsequence
https://leetcode.com/problems/longest-continuous-increasing-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
ans = 0
j = 0
for i in range(len(nums)):
if i > 0 and nums[i] <= nums[i - 1]:
j = i
ans …
Read More
678-Valid-Parenthesis-String
https://leetcode.com/problems/valid-parenthesis-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkValidString(self, s: str) -> bool:
low = 0
high = 0
for c in s:
if c == '(':
low += 1
high += 1
elif c == ')':
if low > 0:
low -= 1
high -= 1 …
Read More
679-24-Game
https://leetcode.com/problems/24-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def judgePoint24(self, nums: List[int]) -> bool:
def generate(a: float, b: float) -> List[float]:
return [a * b,
math.inf if b == 0 else a / b,
math.inf if …
Read More
68-Text-Justification
https://leetcode.com/problems/text-justification
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
ans = []
row = []
rowLetters = 0
for word in words:
if rowLetters + len(word) + len(row) > maxWidth:
for i in range …
Read More
680-Valid-Palindrome-Ii
https://leetcode.com/problems/valid-palindrome-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def validPalindrome(self, s: str) -> bool:
def validPalindrome(l: int, r: int) -> bool:
return all(s[i] == s[r - i + l] for i in range(l, (l + r) // 2 …
Read More
681-Next-Closest-Time
https://leetcode.com/problems/next-closest-time
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextClosestTime(self, time: str) -> str:
ans = list(time)
digits = sorted(ans)
def nextClosest(digit: chr, limit: chr) -> chr:
next = bisect_right(digits, digit)
return digits[0] if next == 4 …
Read More