From ca2e8d7df9a873a931234ab209e995a6ebf9693a Mon Sep 17 00:00:00 2001 From: Eason Yan Date: Wed, 16 Aug 2017 18:30:00 -0700 Subject: [PATCH 1/4] edit 279 --- .../java/com/fishercoder/solutions/_279.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fishercoder/solutions/_279.java b/src/main/java/com/fishercoder/solutions/_279.java index 4abfe0ab1d..380ab6c76b 100644 --- a/src/main/java/com/fishercoder/solutions/_279.java +++ b/src/main/java/com/fishercoder/solutions/_279.java @@ -1,5 +1,5 @@ package com.fishercoder.solutions; - +import java.util.*; /** * Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. @@ -17,4 +17,22 @@ public int numSquares(int n) { return result; } + //DP solution + public int numSquaresDP(int n) { + int[] dp = new int[n + 1]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + dp[1] = 1; + for(int i = 1; i <= n; i++) { + int min = Integer.MAX_VALUE; + int j = 1; + while(i -j*j >= 0) { + min = Math.min(min, dp[i - j*j] + 1); + j++; + } + dp[i] = min; + } + return dp[n]; + } + } From 8316b4c090718640342260393ce0f720684ec84c Mon Sep 17 00:00:00 2001 From: Eason Yan Date: Thu, 17 Aug 2017 14:20:17 -0700 Subject: [PATCH 2/4] edit 322 --- .../java/com/fishercoder/solutions/_322.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fishercoder/solutions/_322.java b/src/main/java/com/fishercoder/solutions/_322.java index 1458dc26f3..33d4b3776f 100644 --- a/src/main/java/com/fishercoder/solutions/_322.java +++ b/src/main/java/com/fishercoder/solutions/_322.java @@ -1,5 +1,5 @@ package com.fishercoder.solutions; - +import java.util.*; /** * 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. @@ -41,4 +41,21 @@ private int helper(int[] coins, int remaining, int[] count) { } + //dp solution + public int coinChangeDP(int[] coins, int amount) { + int max = amount + 1; + int [] dp = new int[max]; + Arrays.fill(dp, max);// initial the dp array with amount + 1 which is not valid case. + dp[0] = 0;//initial first amount 0 = 0; + for(int i = 1; i <= amount; i++) { + for(int j = 0; j < coins.length; j++) { + if(coins[j] <= i) { + dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);//the dp[coins[j]] will ba a valid case, then if dp[i - coins[j]] is valid + // then we update dp[i], otherwise dp[i] = max; + } + } + } + return dp[amount] > amount ? -1 : dp[amount]; + } + } From 9258968264146c30b9e161456ce486193ac766f0 Mon Sep 17 00:00:00 2001 From: Eason Yan Date: Thu, 17 Aug 2017 14:26:41 -0700 Subject: [PATCH 3/4] conflit --- src/main/java/com/fishercoder/solutions/_279.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/main/java/com/fishercoder/solutions/_279.java b/src/main/java/com/fishercoder/solutions/_279.java index 441c5f2005..ce8e0f2964 100644 --- a/src/main/java/com/fishercoder/solutions/_279.java +++ b/src/main/java/com/fishercoder/solutions/_279.java @@ -23,19 +23,11 @@ public int numSquaresDP(int n) { Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; dp[1] = 1; -<<<<<<< HEAD - for(int i = 1; i <= n; i++) { - int min = Integer.MAX_VALUE; - int j = 1; - while(i -j*j >= 0) { - min = Math.min(min, dp[i - j*j] + 1); -======= for (int i = 1; i <= n; i++) { int min = Integer.MAX_VALUE; int j = 1; while (i - j * j >= 0) { min = Math.min(min, dp[i - j * j] + 1); ->>>>>>> f71782f55e700a624b0b1b3cadd83390c37ed16c j++; } dp[i] = min; From 99b27a2f148606a27309902717f87ea331ad48a4 Mon Sep 17 00:00:00 2001 From: Eason Yan Date: Sat, 19 Aug 2017 16:51:17 -0700 Subject: [PATCH 4/4] 378 add BS solution --- .../java/com/fishercoder/solutions/_378.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/main/java/com/fishercoder/solutions/_378.java b/src/main/java/com/fishercoder/solutions/_378.java index ef837bf014..be9dd0c79a 100644 --- a/src/main/java/com/fishercoder/solutions/_378.java +++ b/src/main/java/com/fishercoder/solutions/_378.java @@ -37,4 +37,29 @@ public int kthSmallest(int[][] matrix, int k) { } //TODO: use heap and binary search to do it. + + //Binary Search : The idea is to pick a mid number than compare it with the elements in each row, we start form + // end of row util we find the element is less than the mid, the left side element is all less than mid; keep tracking elements + // that less than mid and compare with k, then update the k. + public int kthSmallestBS(int[][] matrix, int k) { + int row = matrix.length - 1, col = matrix[0].length - 1; + int lo = matrix[0][0]; + int hi = matrix[row][col] ; + while(lo < hi) { + int mid = lo + (hi - lo)/2; + int count = 0, j = col; + for(int i= 0; i <= row; i++) { + while(j >=0 && matrix[i][j] > mid) { + j--; + } + count += (j + 1); + } + if(count < k) { + lo = mid + 1; + } else { + hi = mid; + } + } + return lo; + } }