You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.