9-Palindrome-Number
https://leetcode.com/problems/palindrome-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0:
return False
rev = 0
y = x
while y:
rev = rev * 10 + y % 10
y //= 10
return rev == x
Read More
9-Pygal-1
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
!pip show pygal | grep "Version:"
import pygal
line_chart = pygal.Line()
line_chart.title = "Simple Line …
Read More
90-Subsets-Ii
https://leetcode.com/problems/subsets-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
ans = []
def dfs(s: int, path: List[int]) -> None:
ans.append(path)
if s == len(nums):
return
for i in …
Read More
902-Numbers-At-Most-N-Given-Digit-Set
https://leetcode.com/problems/numbers-at-most-n-given-digit-set
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def atMostNGivenDigitSet(self, D: List[str], N: int) -> int:
ans = 0
num = str(N)
for i in range(1, len(num)):
ans += len(D)**i
for i, c in …
Read More
904-Fruit-Into-Baskets
https://leetcode.com/problems/fruit-into-baskets
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def totalFruit(self, tree: List[int]) -> int:
ans = 0
count = defaultdict(int)
l = 0
for r, t in enumerate(tree):
count[t] += 1
while len(count) > 2:
count[tree …
Read More
905-Sort-Array-By-Parity
https://leetcode.com/problems/sort-array-by-parity
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortArrayByParity(self, A: List[int]) -> List[int]:
l = 0
r = len(A) - 1
while l < r:
if A[l] % 2 == 1 and A[r] % 2 == 0:
A[l …
Read More
906-Super-Palindromes
https://leetcode.com/problems/super-palindromes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def superpalindromesInRange(self, left: str, right: str) -> int:
def nextPalindrome(num: int) -> int:
s = str(num)
n = len(s)
half = s[0:(n + 1) // 2]
reversedHalf = half[:n // 2 …
Read More
907-Sum-Of-Subarray-Minimums
https://leetcode.com/problems/sum-of-subarray-minimums
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sumSubarrayMins(self, arr: List[int]) -> int:
kMod = 1_000_000_007
n = len(arr)
ans = 0
# prev[i] := index k s.t. arr[k] is the prev min in arr …
Read More
908-Smallest-Range-I
https://leetcode.com/problems/smallest-range-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestRangeI(self, A: List[int], K: int) -> int:
return max(0, max(A) - min(A) - 2 * K)
new Solution().smallestRangeI()
Score: 5
Read More
909-Snakes-And-Ladders
https://leetcode.com/problems/snakes-and-ladders
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n = len(board)
ans = 0
q = deque([1])
seen = set()
A = [0] * (1 + n * n) # 2D -> 1D
for i …
Read More