47-Permutations-Ii
https://leetcode.com/problems/permutations-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
ans = []
used = [False] * len(nums)
def dfs(path: List[int]) -> None:
if len(path) == len(nums):
ans.append(path.copy …
Read More
472-Concatenated-Words
https://leetcode.com/problems/concatenated-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findAllConcatenatedWordsInADict(self, words: List[str]) -> List[str]:
wordSet = set(words)
@functools.lru_cache(None)
def isConcat(word: str) -> bool:
for i in range(1, len(word)):
prefix = word[:i …
Read More
473-Matchsticks-To-Square
https://leetcode.com/problems/matchsticks-to-square
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def makesquare(self, matchsticks: List[int]) -> bool:
if len(matchsticks) < 4:
return False
perimeter = sum(matchsticks)
if perimeter % 4 != 0:
return False
A = sorted(matchsticks)[::-1]
def dfs(selected …
Read More
474-Ones-And-Zeroes
https://leetcode.com/problems/ones-and-zeroes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
# dp[i][j] := max size of the subset given i 0's and j 1's are available …
Read More
478-Generate-Random-Point-In-A-Circle
https://leetcode.com/problems/generate-random-point-in-a-circle
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self, radius: float, x_center: float, y_center: float):
self.radius = radius
self.x_center = x_center
self.y_center = y_center
def randPoint(self) -> List[float]:
length = sqrt(random.uniform(0, 1 …
Read More
479-Largest-Palindrome-Product
https://leetcode.com/problems/largest-palindrome-product
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestPalindrome(self, n: int) -> int:
if n == 1:
return 9
kMod = 1337
upper = pow(10, n) - 1
lower = pow(10, n - 1) - 1
for i in range(upper …
Read More
48-Rotate-Image
https://leetcode.com/problems/rotate-image
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
matrix.reverse()
for i in range(len(matrix)):
for j in range(i + 1, len(matrix)):
matrix[i][j], matrix[j …
Read More
483-Smallest-Good-Base
https://leetcode.com/problems/smallest-good-base
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestGoodBase(self, n: str) -> str:
n = int(n)
for m in range(int(math.log(n, 2)), 1, -1):
k = int(n**m**-1)
if (k**(m + 1 …
Read More
484-Find-Permutation
https://leetcode.com/problems/find-permutation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findPermutation(self, s: str) -> List[int]:
ans = []
stack = []
for i, c in enumerate(s):
stack.append(i + 1)
if c == 'I':
while stack: # Consume all decreasings
ans.append …
Read More
485-Max-Consecutive-Ones
https://leetcode.com/problems/max-consecutive-ones
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = 0
summ = 0
for num in nums:
if num == 0:
summ = 0
else:
summ += num
ans = max(ans, summ)
return ans …
Read More