diff --git a/CoinChange.kt b/CoinChange.kt new file mode 100644 index 00000000..333472bc --- /dev/null +++ b/CoinChange.kt @@ -0,0 +1,31 @@ +// In this problem of Coin Change, we use DP with bottom up approach. We create a dp array where each index represents the mininum coins needed to make that amount. +// We initialize dp[0] to 0 and all other indices to a large value. We then iterate through each coin and for each coin, we iterate through all amounts from 1 to the target amount. +// If the coin denomination is greater than the current amount, we use the previous value. Otherwise, we take the minimum of the current value and 1 plus the value at the index +// Time complexity is O(m*n) where m is number of coins and n is the amount. Space complexity is O(n) for the dp array. + +class Solution { + fun coinChange(coins: IntArray, amount: Int): Int { + val n = amount + val m = coins.size + + val dp = IntArray(n + 1) + + dp[0] = 0 + for(j in 1..n){ + dp[j] = Int.MAX_VALUE - 10 + } + for(i in 1 .. m) { + for (j in 1 .. n) { + //check if coin denomination > the current amount use the one from abobe, j is amoount and i is the idx on the matrix + if(j < coins[i - 1]) { + dp[j] = dp[j] + } else { + dp[j] = Math.min(dp[j], 1 + dp[j - coins[i -1]]) + } + } + } + val result = dp[n] + if (result >= Int.MAX_VALUE - 10) return -1 + return result + } +} \ No newline at end of file diff --git a/HouseRobber.kt b/HouseRobber.kt new file mode 100644 index 00000000..02a9aa9f --- /dev/null +++ b/HouseRobber.kt @@ -0,0 +1,17 @@ +// Chose to solve this problem using DP. This is bottom up approach where we keep track of two variables prev and curr. We iterate through the array and each step we update the curr to be max of curr and nums value plus prev. +// Time complexity is O(n) and space complexity is O(1) + +class Solution { + fun rob(nums: IntArray): Int { + if(nums.size < 2) return nums[0] + var prev = nums[0] + var curr = Math.max(nums[0], nums[1]) + for (i in 2..nums.size - 1) { + var temp = curr + curr = Math.max(curr, nums[i]+ prev) + prev = temp + } + + return curr + } +} \ No newline at end of file