172-Factorial-Trailing-Zeroes
https://leetcode.com/problems/factorial-trailing-zeroes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def trailingZeroes(self, n: int) -> int:
return 0 if n == 0 else n // 5 + self.trailingZeroes(n // 5)
new Solution().trailingZeroes()
Score: 5
Read More
174-Dungeon-Game
https://leetcode.com/problems/dungeon-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
m = len(dungeon)
n = len(dungeon[0])
dp = [math.inf] * (n + 1)
dp[n - 1] = 1
for i in reversed(range …
Read More
179-Largest-Number
https://leetcode.com/problems/largest-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class LargerStrKey(str):
def __lt__(x: str, y: str) -> bool:
return x + y > y + x
class Solution:
def largestNumber(self, nums: List[int]) -> str:
return ''.join(sorted(map(str, nums), key …
Read More
18-4Sum
https://leetcode.com/problems/4sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def fourSum(self, nums: List[int], target: int):
ans = []
def nSum(l: int, r: int, target: int, n: int, path: List[int], ans: List[List[int]]) -> None:
if r …
Read More
186-Reverse-Words-In-A-String-Ii
https://leetcode.com/problems/reverse-words-in-a-string-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseWords(self, s: List[str]) -> None:
def reverse(l: int, r: int) -> None:
while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1 …
Read More
187-Repeated-Dna-Sequences
https://leetcode.com/problems/repeated-dna-sequences
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findRepeatedDnaSequences(self, s: str) -> List[str]:
ans = set()
seen = set()
for i in range(len(s) - 9):
seq = s[i:i + 10]
if seq in seen:
ans.add …
Read More
188-Best-Time-To-Buy-And-Sell-Stock-Iv
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxProfit(self, k: int, prices: List[int]) -> int:
if k >= len(prices) // 2:
sell = 0
hold = -math.inf
for price in prices:
sell = max(sell, hold + price)
hold …
Read More
189-Rotate-Array
https://leetcode.com/problems/rotate-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
k %= len(nums)
self.reverse(nums, 0, len(nums) - 1)
self.reverse(nums, 0, k - 1)
self.reverse(nums, k …
Read More
19-Remove-Nth-Node-From-End-Of-List
https://leetcode.com/problems/remove-nth-node-from-end-of-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
slow = head
fast = head
for _ in range(n):
fast = fast.next
if not fast:
return head.next
while fast.next …
Read More
190-Reverse-Bits
https://leetcode.com/problems/reverse-bits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseBits(self, n: int) -> int:
ans = 0
for i in range(32):
if n >> i & 1:
ans |= 1 << 31 - i
return ans
new Solution().reverseBits()
Score: 5
Read More