Skip to content

Conversation

@Luke-Manyamazi
Copy link

@Luke-Manyamazi Luke-Manyamazi commented Nov 10, 2025

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

  • Optimized fibonacci using memoization to reduce time complexity from O(2^n) to O(n)
  • Enhanced making_change with dynamic programming to handle large amounts efficiently
  • Added edge case handling for both algorithms

Questions

I do not have questions for this PR

@Luke-Manyamazi Luke-Manyamazi added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Nov 10, 2025
Comment on lines 24 to 46
cache_key = (total, tuple(coins))

# Check if we already computed this combination
if cache_key in change_cache:
return change_cache[cache_key]

# Base cases
if total == 0:
return 1 # One way to make 0: use no coins
if len(coins) == 0:
return 0 # No coins left but still need to make total > 0

ways = 0
for coin_index in range(len(coins)):
coin = coins[coin_index]
count_of_coin = 1
while coin * count_of_coin <= total:
total_from_coins = coin * count_of_coin
if total_from_coins == total:
ways += 1
else:
intermediate = ways_to_make_change_helper(total - total_from_coins, coins=coins[coin_index+1:])
ways += intermediate
Copy link

Choose a reason for hiding this comment

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

From line 45, we can figure out coins can only be one of the following 9 lists:

[200, 100, 50, 20, 10, 5, 2, 1]
[100, 50, 20, 10, 5, 2, 1]
[50, 20, 10, 5, 2, 1]
...
[1]
[]

We could further improve the performance if we can

  • avoid repeatedly creating the same sub-lists at line 45, and
  • create key as (total, a_value_identifiying_one_of_the_sublists) instead of as (total, tuple(coins))

I don't think this exercise expects trainees to optimize the code to this level. So change is optional.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants