Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions climbing-stairs/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# idea: DP
# I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP.
# If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
Comment on lines +1 to +3
Copy link
Contributor

@samcho0608 samcho0608 Nov 16, 2025

Choose a reason for hiding this comment

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

my rule of thumb is checking if current value(aka value at index) can be determined using the previous.

for instance, both #230 and #264 are both similar in that desired value at index can be determined using previous values.

  • Climbing Stairs #230 :
    • f(i) = # of possible combinations to reach i
    • can take 1 or 2 steps means that f(i) can be reached using:
      • f(i-1)—taking 1 step—and
      • f(i-2)—taking 2 steps—
    • thus, f(i) = f(i-1) + f(i-2)
  • House Robber #264 :
    • f(i) = max possible amount that can be robbed to reach i
    • cannot rob adjacent houses means that f(i) can be calculated using the max between:
      • f(i-1)—choosing not to rob house[i]—and
      • f(i-2)—choosing to rob house[i]
    • f(i) = max(f(i-1), f(i-2)) + house[i]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your detailed explanation! it was really helpful:)
I haven’t solved that many problems yet, so this might be why I have one more question: isn’t it easy to miss that a value can be determined using previous values when the step size is larger? For instance, dp[6] could be obtained from dp[1] and dp[5].

Copy link
Contributor

@samcho0608 samcho0608 Nov 20, 2025

Choose a reason for hiding this comment

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

@ppxyn1

That's why you gotta focus on the current affected by previous part (kinda like Induction proof from discrete math courses)

Begin with smaller n's and see if there is a pattern.

Take #230 with max step size up to 4 as an example(6 is too big for a written explanation of patterns).

  1. n=0: There's only one way (0)
  2. n=1: There's only one way (0->1)
  3. n=2: 2 because (0 -> 2 or 0 -> 1 -> 2)
    • f(0): 0 -> 2
    • f(1): 0 -> 1 -> 2
    • Already kinda seeing the pattern since we're seeing f(2) = f(0) + f(1)
  4. n=3: 4
    • f(0): 0 -> 3
    • f(1): 0 -> 1 -> 3
    • f(2):
      • 0 -> 2 -> 3
      • 0 -> 1 -> 2 -> 3
  5. n=4: 8
    • f(0): 0 -> 4
    • f(1): 0 -> 1 -> 4
    • f(2):
      • 0 -> 2 -> 4
      • 0 -> 1 -> 2 -> 4
    • f(3):
      • 0 -> 3 -> 4
      • 0 -> 1 -> 3 -> 4
      • 0 -> 2 -> 3 -> 4
      • 0 -> 1 -> 2 -> 3 -> 4

So the pattern is:
f(n) = f(n-1) + f(n-2) + .... f(n-k), where k is the step size

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Appreciate it!


class Solution:
def climbStairs(self, n: int) -> int:
if n <= 2:
return n
dp = [0] * (n+1)
dp[2], dp[3] = 2, 3

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

return dp[n]
Comment on lines +7 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

you can simplify it a bit

Suggested change
if n <= 2:
return n
dp = [0] * (n+1)
dp[2], dp[3] = 2, 3
#for i in range(4, n): error when n=4
for i in range(4, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
dp = [0] * (n+1)
dp[0], dp[1] = 1, 1
for i in range(2, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]





27 changes: 27 additions & 0 deletions valid-anagram/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#idea: dictionary
from collections import Counter

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_dic = Counter(sorted(s))
t_dic = Counter(sorted(t))
print(s_dic, t_dic)
Copy link
Contributor

Choose a reason for hiding this comment

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

probably better to remove print statement to reduce IO time

Suggested change
print(s_dic, t_dic)

return s_dic==t_dic



# Trial and Error
'''
When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored.
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_dic = Counter(s)
t_dic = Counter(t)
print(s_dic, t_dic)
return sorted(s_dic)==sorted(t_dic)
'''