Data Structure and Algorithm - Day 10
-
new climb stairs
step: 1 2 3, two adjacent steps cannot be the same.
-
120. Triangle
Given a
triangle
array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index
i
on the current row, you may move to either indexi
or indexi + 1
on the next row.Example 1:
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] Output: 11 Explanation: The triangle looks like: 2 3 4 6 5 7 4 1 8 3 The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
Example 2:
Input: triangle = [[-10]] Output: -10
Constraints:
1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 104
Follow up: Could you do this using only
O(n)
extra space, wheren
is the total number of rows in the triangle?// division: problem(i,j) = min(sub(i+1,j),sub(i+1,j+1)) + a[i,j] // dp array: f[i,j] // dp equation: f[i][j] = Math.min(f[i+1][j], f[i+1][j+1]) + a[i][j] class Solution { public int minimumTotal(List<List<Integer>> triangle) { int n = triangle.size(); int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = triangle.get(n-1).get(i); } for (int i = n-2; i >= 0; i--) { for (int j = 0; j <= i; j++) { dp[j] = triangle.get(i).get(j) + Math.min(dp[j], dp[j+1]); } } return dp[0]; } } // memo dfs
-
152. Maximum Product Subarray
Given an integer array
nums
, find a contiguous non-empty subarray within the array that has the largest product, and return the product.It is guaranteed that the answer will fit in a 32-bit integer.
A subarray is a contiguous subsequence of the array.
Example 1:
Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6.
Example 2:
Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
Constraints:
1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
class Solution { public int maxProduct(int[] nums) { int res = nums[0]; int min = nums[0], max = nums[0]; for (int i = 1; i < nums.length; i++) { int t = min; min = Math.min(Math.min(nums[i], nums[i] * min), nums[i] * max); max = Math.max(Math.max(nums[i], nums[i] * max), nums[i] * t); res = Math.max(res, max); } return res; } }
-
322. Coin Change
You are given an integer array
coins
representing coins of different denominations and an integeramount
representing a total amount of money.Return 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
// recur over time class Solution { int res = Integer.MAX_VALUE; public int coinChange(int[] coins, int amount) { bfs(coins, amount, 0); return res == Integer.MAX_VALUE ? -1 : res; } private void bfs(int[] coins, int amount, int num) { if (amount < 0) return ; if (amount == 0) { res = Math.min(res, num); return ; } for (int coin : coins) { bfs(coins, amount - coin, num + 1); } } } // DP // subproblems: problem[i] = 1 + min(sub(i-coin1),sub(i-coin2),...) // dp array: f(n) = min(f(n-k), fpr k in [1,2,5]) + 1 // dp equation class Solution { public int coinChange(int[] coins, int amount) { int max = amount + 1; int[] dp = new int[amount + 1]; Arrays.fill(dp, max); dp[0] = 0; for (int i = 1; i <= amount; i++) { for (int coin : coins) { if (coin <= i) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } } } return dp[amount] > amount ? -1 : dp[amount]; } }
-
198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array
nums
representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.Example 1:
Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
Example 2:
Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 400
class Solution { public int rob(int[] nums) { if (nums.length == 1) return nums[0]; nums[1] = Math.max(nums[0], nums[1]); for (int i = 2; i < nums.length; i++) { nums[i] = Math.max(nums[i] + nums[i-2], nums[i-1]); } return nums[nums.length - 1]; } } class Solution { public int rob(int[] nums) { if (nums.length == 1) return nums[0]; int[][] dp = new int[nums.length][2]; dp[0][0] = 0; // not rob dp[0][1] = nums[0]; // rob for (int i = 1; i < nums.length; i++) { dp[i][0] = Math.max(dp[i-1][1], dp[i-1][0]); dp[i][1] = dp[i-1][0] + nums[i]; } return Math.max(dp[nums.length - 1][0], dp[nums.length - 1][1]); } }