Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. Array

Possible Sum

You have a collection of coins, and you know the values of the coins and the quantity of each type of coin in it. You want to know how many distinct sums you can make from non-empty groupings of these coins.

Example

For coins = [10, 50, 100] and quantity = [1, 2, 1], the output should be solution(coins, quantity) = 9.

Here are all the possible sums:

  • 50 = 50;

  • 10 + 50 = 60;

  • 50 + 100 = 150;

  • 10 + 50 + 100 = 160;

  • 50 + 50 = 100;

  • 10 + 50 + 50 = 110;

  • 50 + 50 + 100 = 200;

  • 10 + 50 + 50 + 100 = 210;

  • 10 = 10;

  • 100 = 100;

  • 10 + 100 = 110.

As you can see, there are 9 distinct sums that can be created from non-empty groupings of your coins.

Idea

HashSet, traversal

Iterate through each coin and corresponding quantity, add resulting sum to HashSet

Make a duplicate of current sums for every coin index, so that each coin can be added to the sums without affecting the original set.

The set will automatically remove sum duplicates

Code

int solution(int[] coins, int[] quantity) {
    HashSet<Integer> sums = new HashSet<>();
    sums.add(0);
    for(int i = 0; i<coins.length; i++){
        List<Integer> currSum = new ArrayList<>(sums);
        for(Integer curr: currSum){
            for(int j = 1; j<=quantity[i]; j++){
                sums.add(curr+coins[i]*j);
            }
        }
    }
    return sums.size()-1;
}
PreviousNumbers At Most N Given Digit SetNextSwap Lex Order

Last updated 3 years ago