Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/incrementaltest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
def two_sum(nums, target):
seen = {}

for i, num in enumerate(nums):
complement = target - num

if complement in seen:
return [seen[complement], i]

seen[num] = num
Comment thread
mchun2288 marked this conversation as resolved.

return []


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


def reverse_linked_list(head):
prev = None
current = head

while current:
next_node = current.next
current.next = prev
prev = current
current = next_node

return current


def is_valid_parentheses(s):
stack = []
mapping = {')': '(', '}': '{', ']': '['}

for char in s:
if char in mapping:
top = stack.pop() if stack else '#'
if mapping[char] != top:
return False
else:
stack.append(char)

return True


def climb_stairs(n):
if n <= 1:
return n

dp = [0] * (n + 1)
dp[0] = 1
dp[1] = 1

for i in range(2, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]

return dp[n - 1]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 Code Bugs

The function returns dp[n - 1] instead of dp[n], causing an off-by-one error — for example, climb_stairs(2) returns 1 instead of the correct 2.

Details

📖 Explanation: The dp table is computed correctly up to index n, but the final return uses n-1, which gives the answer for n-1 stairs instead of n stairs.

Suggested change
return dp[n - 1]
return dp[n]

Uses AI. Verify results. Give Feedback

Loading