101-Symmetric-Tree
https://leetcode.com/problems/symmetric-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def isSymmetric(p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p or not q:
return p == q
return p.val == q …
Read More
1010-Pairs-Of-Songs-With-Total-Durations-Divisible-By-60
https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numPairsDivisibleBy60(self, time: List[int]) -> int:
ans = 0
count = [0] * 60
for t in time:
t %= 60
ans += count[(60 - t) % 60]
count[t] += 1
return ans
Read More
1011-Capacity-To-Ship-Packages-Within-D-Days
https://leetcode.com/problems/capacity-to-ship-packages-within-d-days
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shipWithinDays(self, weights: List[int], days: int) -> int:
l = max(weights)
r = sum(weights)
def shipDays(shipCapacity: int) -> int:
days = 1
capacity = 0
for weight in weights:
if …
Read More
1012-Numbers-With-Repeated-Digits
https://leetcode.com/problems/numbers-with-repeated-digits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numDupDigitsAtMostN(self, n: int) -> int:
return n - self._countSpecialNumbers(n)
def _countSpecialNumbers(self, n: int) -> int:
s = str(n)
digitSize = int(log10(n)) + 1
# Dp(i, j, k …
Read More
1013-Partition-Array-Into-Three-Parts-With-Equal-Sum
https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canThreePartsEqualSum(self, A: List[int]) -> bool:
summ = sum(A)
prefix = 0
parts = 1
for a in A:
prefix += a
if prefix == summ * parts // 3:
parts += 1
return summ …
Read More
1014-Best-Sightseeing-Pair
https://leetcode.com/problems/best-sightseeing-pair
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxScoreSightseeingPair(self, A: List[int]) -> int:
ans = 0
bestPrev = 0
for a in A:
ans = max(ans, a + bestPrev)
bestPrev = max(bestPrev, a) - 1
return ans
Read More
1015-Smallest-Integer-Divisible-By-K
https://leetcode.com/problems/smallest-integer-divisible-by-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def smallestRepunitDivByK(self, K: int) -> int:
if K % 10 not in {1, 3, 7, 9}:
return -1
seen = set()
N = 0
for length in range(1, K + 1):
N …
Read More
1016-Binary-String-With-Substrings-Representing-1-To-N
https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def queryString(self, S: str, N: int) -> bool:
if N > 1511:
return False
for i in range(N, N // 2, -1):
if format(i, 'b') not in S:
return …
Read More
1017-Convert-To-Base-2
https://leetcode.com/problems/convert-to-base-2
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def baseNeg2(self, N: int) -> str:
ans = ''
while N:
ans = str(N & 1) + ans
N = -(N >> 1)
return '0' if ans == '' else ans
new Solution().baseNeg2()
Score: 5
Read More
1018-Binary-Prefix-Divisible-By-5
https://leetcode.com/problems/binary-prefix-divisible-by-5
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def prefixesDivBy5(self, A: List[int]) -> List[bool]:
ans = []
num = 0
for a in A:
num = (num * 2 + a) % 5
ans.append(num % 5 == 0)
return ans
Read More