518. Coin Change 2 (Segmentation Fault...)

class Solution { public: void changeHelper(int amount, vector<int> coins, vector<int>::iterator begin, int & currSum, int & count){ // a helper function. // currSum is how much we already have // count is how many types of coin combinations we already have // iterator begin is to indicate where in the coins can we still choose from. Use this to avoid duplications if (currSum > amount) return; else if (currSum == amount){ // found a solution! count++; return; } // now choose explore unchoose for (auto it = begin; it != coins.end(); it++){ currSum += *it; changeHelper(amount, coins, it, currSum, count); currSum -= *it; } } int change(int amount, vector<int>& coins) { int currSum = 0; int count = 0; vector<int>::iterator begin = coins.begin(); changeHelper(amount, coins, begin, currSum, count); return count; } };
There seems to be an infinite loop... Need to check why...

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.