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
19-Renko-Charts
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
# !pip install stocktrends
print(pyu.ps2("stocktrends yfinance pandas matplotlib"))
stocktrends==0.1.5
yfinance==0.2.51
pandas …
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
191-Number-Of-1-Bits
https://leetcode.com/problems/number-of-1-bits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
for i in range(32):
if (n >> i) & 1:
ans += 1
return ans
new Solution().hammingWeight()
Score: 5
Read More
198-House-Robber
https://leetcode.com/problems/house-robber
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0
if len(nums) == 1:
return nums[0]
# dp[i]: = max money of robbing nums[0..i]
dp …
Read More
199-Binary-Tree-Right-Side-View
https://leetcode.com/problems/binary-tree-right-side-view
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
if not root:
return []
ans = []
q = deque([root])
while q:
size = len(q)
for i in range(size):
root = q.popleft …
Read More
2-Add-Two-Numbers
https://leetcode.com/problems/add-two-numbers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
curr = dummy
carry = 0
while carry or l1 or l2:
if l1:
carry += l1.val
l1 = l1.next …
Read More
20-Fibonacci-Retracement
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
print(pyu.ps2("yfinance pandas matplotlib"))
yfinance==0.2.51
pandas==2.2.3
matplotlib==3.9.3
Read More