Not a combination-- wrong question wording


  • 0
    L

    There is a problem with question. It asks for combinations, but actually includes permutations.

    For example, 1,3 and 3,1 are same combinations. The following code treats these as combination.

    So the question should be rephrased either to permutuation or the answers should be corrected.

    class Solution {
    public:
        int combinationSum4(vector<int>& nums, int target) {
            
            vector<int> cur;
            return comb(nums,cur, 0, target, 0);
        }
        
        int comb(vector<int>& nums, vector<int> cur,  int cursum, int target, int sp)
        {
            if(cursum  == target)
            {
                for( int i=0;i < cur.size();i++)
                    cout << cur[i];
                cout<<endl;
                return 1;
            }
                
            else if( sp == nums.size() || cursum >target)
                return 0;
            else
            {
                cur.push_back(nums[sp]);
                int v= comb (nums, cur,  nums[sp]+cursum , target, sp );
                cur.erase(cur.end()-1);
                
                v += comb(nums , cur, cursum, target, sp+1 );
                
                
                //v+= comb (nums,  nums[sp]+cursum , target, sp+1 );
                
                
                return v;
            }
            
        }
        
    };
    

  • 0

    It's not really permutations, either, is it?

    I had noticed this as well, but I think sometimes you'll just have to allow non-math definitions disagreeing with certain math definitions. In this case, imagine I have a combination lock with four digits and the opening combination is 7-3-9-2. Do you think it'll also open for combination 2-7-3-9? Both are different combinations of the digits 2, 3, 7 and 9.

    Moral of the story, mathematics doesn't absolutely own the word.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.