Skip to content

Adding the solutions#1985

Open
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master
Open

Adding the solutions#1985
subbu4061 wants to merge 1 commit intosuper30admin:masterfrom
subbu4061:master

Conversation

@subbu4061
Copy link

No description provided.

@super30admin
Copy link
Owner

For the coin change problem:

  • Solution 1 (DP) is well-implemented. However, the condition for returning -1 is a bit arbitrary. It would be better to define a constant for the initial value (e.g., INF = Integer.MAX_VALUE - 5) and then compare matrix[amount] to INF. Also, note that the inner loop could be optimized by starting from the coin value instead of from 1 and then checking condition. But your current approach is acceptable.

  • Solution 2 (Recursion) is a good attempt at a recursive solution, but without memoization it is not efficient for large inputs. You should consider adding memoization to avoid repeated calculations. For example, you could use a 2D memo array with dimensions [coins.length][amount+1] to store the result for state (idx, amount). This would reduce the time complexity to O(coins.length * amount), which is acceptable.

Also, in Solution 2, you subtract 10 from Integer.MAX_VALUE to avoid overflow when adding 1. But it would be better to use a constant for the invalid value.

Overall, you demonstrated understanding of both DP and recursive approaches. However, for the recursive solution, you should always consider memoization to avoid exponential time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants