Sunday, January 3, 2016

LeetCode [322] Coin Change

 322. Coin Change

Medium

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.

 

Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
Output: 0

Example 4:

Input: coins = [1], amount = 1
Output: 1

Example 5:

Input: coins = [1], amount = 2
Output: 2

 

Constraints:

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 231 - 1
  • 0 <= amount <= 104

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        dp[0] = 0;
        for (int i = 1; i <= amount; ++i) {
            dp[i] = -1;
            for (int c : coins) {
                if (i >= c && dp[i - c] >= 0) {
                    if (dp[i] == -1)
                        dp[i] = dp[i - c] + 1;
                    else
                        dp[i] = Math.min(dp[i], dp[i - c] + 1);
                }
            }
       //     System.out.println(Arrays.toString(dp));
        }
        return dp[amount];
    }
}

Wednesday, December 23, 2015

LeetCode [320] Generalized Abbreviation

Ref
[1] https://leetcode.com/problems/generalized-abbreviation/

LeetCode [321] Create Maximum Number

Ref
[1] https://leetcode.com/problems/create-maximum-number/
OJ
[2] https://leetcode.com/discuss/75756/share-my-greedy-solution

Sunday, December 20, 2015

MJ [50] Connected Components

Ref
[1] http://www.mitbbs.com/article_t/JobHunting/33111815.html

Saturday, December 19, 2015

LeetCode [319] Bulb Switcher

Ref
[1] https://leetcode.com/problems/bulb-switcher/

Friday, December 18, 2015

Survey [1] Cycle Detection


Method 1: Union-Find (Detect Cycle in a an Undirected Graph) [1]



Method 2: DFS (Detect Cycle in a an Directed Graph) [2]

Method 3: Coloring (Detect Cycle in a an Directed Graph) [3]

 Ref
[1] http://www.geeksforgeeks.org/union-find/
[2] http://www.geeksforgeeks.org/detect-cycle-in-a-graph/
[3] http://www.cs.cornell.edu/courses/cs2112/2012sp/lectures/lec24/lec24-12sp.html

Wednesday, December 16, 2015

MJ [49] Upsampling

Question:

you have an img data as an array, output the data for upsampling. For example, 
[1, 2, 3, 4, 5, 6] as width 3(2 rows) ==> upsample 2 times would be [1 1 2 2 3 3 1 1 2 2 3 3 4 4 5 5 6 6 4 4 5 5 6 6]
================
================
Ref
[1] http://www.mitbbs.com/article_t/JobHunting/33110511.html