862-Shortest-Subarray-With-Sum-At-Least-K
https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
n = len(A)
ans = n + 1
q = deque()
prefix = [0] + list(itertools.accumulate(A))
for i in range(n + 1 …
Read More
866-Prime-Palindrome
https://leetcode.com/problems/prime-palindrome
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def primePalindrome(self, N: int) -> int:
def getPalindromes(n: int) -> int:
length = n // 2
for i in range(10**(length - 1), 10**length):
s = str(i)
for j in …
Read More
867-Transpose-Matrix
https://leetcode.com/problems/transpose-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def transpose(self, A: List[List[int]]) -> List[List[int]]:
ans = [[0] * len(A) for _ in range(len(A[0]))]
for i in range(len(A)):
for j …
Read More
868-Binary-Gap
https://leetcode.com/problems/binary-gap
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def binaryGap(self, n: int) -> int:
ans = 0
d = -32 # Distance between any two 1's, initialized to a reasonable small value
while n:
if n & 1:
ans = max …
Read More
869-Reordered-Power-Of-2
https://leetcode.com/problems/reordered-power-of-2
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
count = Counter(str(N))
return any(Counter(str(1 << i)) == count for i in range(30))
new Solution().reorderedPowerOf2()
Score: 5
Read More
87-Scramble-String
https://leetcode.com/problems/scramble-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isScramble(self, s1: str, s2: str) -> bool:
if s1 == s2:
return True
if len(s1) != len(s2):
return False
if Counter(s1) != Counter(s2):
return False
for i …
Read More
870-Advantage-Shuffle
https://leetcode.com/problems/advantage-shuffle
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from sortedcontainers import SortedList
class Solution:
def advantageCount(self, A: List[int], B: List[int]) -> List[int]:
sl = SortedList(A)
for i, b in enumerate(B):
index = 0 if sl[-1 …
Read More
871-Minimum-Number-Of-Refueling-Stops
https://leetcode.com/problems/minimum-number-of-refueling-stops
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minRefuelStops(self, target: int, startFuel: int, stations: List[List[int]]) -> int:
# dp[i] := farthest position we can reach w / i refuels
dp = [startFuel] + [0] * len(stations)
for i …
Read More
872-Leaf-Similar-Trees
https://leetcode.com/problems/leaf-similar-trees
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
def dfs(root: Optional[TreeNode]) -> None:
if not root:
return
if not root.left and not root.right:
yield …
Read More
873-Length-Of-Longest-Fibonacci-Subsequence
https://leetcode.com/problems/length-of-longest-fibonacci-subsequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def lenLongestFibSubseq(self, A: List[int]) -> int:
n = len(A)
ans = 0
numToIndex = {a: i for i, a in enumerate(A)}
dp = [[2] * n for _ in range(n …
Read More