Q****131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.
A palindrome string is a string that reads the same backward as forward.
Example 1:
Input: s = "aab"
Output: [["a","a","b"],["aa","b"]]
Example 2:
Input: s = "a"
Output: [["a"]]
解法及注释
class Solution {
public List<List<String>> partition(String s) {
if(s == null || s.length() == 0)
return Collections.EMPTY_LIST;
int len = s.length();
boolean[][] dp = new boolean[len][len];
List<List<String>> res = new ArrayList();
dfs(res, s, 0, new ArrayList(), dp);
return res;
}
private void dfs(List<List<String>> res, String s, int start, List<String> currList, boolean[][] dp) {
if(start >= s.length())
res.add(new ArrayList(currList));
for(int end = start; end < s.length(); end++) {
if(s.charAt(start) == s.charAt(end) && (end - start <= 2
|| dp[start+1][end-1])) {
dp[start][end] = true;
currList.add(s.substring(start, end + 1)); //add from start to end (inclusive)
dfs(res, s, end + 1, currList, dp);
currList.remove(currList.size() - 1);
}
}
}
}
Q198. 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 system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers representing the amount of money of each house, determine 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:
0 <= nums.length <= 1000 <= nums[i] <= 400
解法及注释
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
int len = nums.length;
int[] dp = new int[len];
dp[0] = nums[0];
dp[1] = Math.max(nums[0], nums[1]);
for(int i = 2; i < len; i++) {
dp[i] = Math.max(dp[i - 1], nums[i] + dp[i -2]);
}
return dp[len - 1];
}
}
Q****213. House Robber II
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given a list of non-negative integers 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 = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:
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 3:
Input: nums = [0]
Output: 0
Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1000
解法及注释
class Solution {
public int rob(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
if(nums.length == 1)
return nums[0];
if(nums.length == 2)
return Math.max(nums[0], nums[1]);
int len = nums.length;
//eventually compare [0, len - 2] and [1, len - 1]
return Math.max(findMax(nums, 0, len - 2), findMax(nums, 1, len - 1));
}
private int findMax(int[] nums, int start, int end) {
//use two parameters to store the max value;
int prev = nums[start];
int curr = Math.max(prev, nums[start + 1]);
int temp;
for(int i = start + 2; i <= end; i++) {
temp = curr;
curr = Math.max(curr, nums[i] + prev);
prev = temp;
}
return curr;
}
}
Q****264. Ugly Number II
Write a program to find the n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
Example:
Input: n = 10
Output: 12
Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.
Note:
1is typically treated as an ugly number.ndoes not exceed 1690.
解法及注释 (python)
class Solution(object):
def nthUglyNumber(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 0
if n == 1:
return 1
h = 1
d2, d3, d5 = [],[],[]
for i in range(n-1):
d2.append(2 * h)
d3.append(3 * h)
d5.append(5 * h)
h = min(d2[0],d3[0],d5[0])
if h == d2[0]:
d2.pop(0)
if h == d3[0]:
d3.pop(0)
if h == d5[0]:
d5.pop(0)
return h
Q****312. Burst Balloons
You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.
If you burst the ith balloon, you will get nums[i - 1] * nums[i] * nums[i + 1] coins. If i - 1 or i + 1 goes out of bounds of the array, then treat it as if there is a balloon with a 1 painted on it.
Return the maximum coins you can collect by bursting the balloons wisely.
Example 1:
Input: nums = [3,1,5,8]
Output: 167
Explanation:
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
Example 2:
Input: nums = [1,5]
Output: 10
Constraints:
n == nums.length1 <= n <= 5000 <= nums[i] <= 100
解法及注释
class Solution {
public int maxCoins(int[] nums) {
int len = nums.length, index = 1, l = len + 2;
//try to build an array from index -1 to len (inclusive)
int nNums[] = new int[len + 2];
//initialize
for(int num : nums)
if(num > 0)
nNums[index++] = num;
nNums[0] = 1;
nNums[index++] = 1;
int dp[][] = new int[len + 2][len + 2];
//i stands for length of sub array
for(int i = 2; i < len + 2; i++) {
for(int left = 0; left < l - i; left++) {
int right = left + i;
for(int j = left + 1; j< right; j++) {
dp[left][right] = Math.max(dp[left][right], nNums[left] * nNums[j] * nNums[right] + dp[j][right] + dp[left][j]);
}
}
}
return dp[0][l - 1];
}
}
Q****322. Coin Change
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 <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
解法及注释
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 j = 0; j < coins.length; j++) {
if(coins[j] <= i)
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
Q****338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example 1:
Input: 2
Output: [0,1,1]
Example 2:
Input: 5
Output: [0,1,1,2,1,2]
Follow up:
- It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
- Space complexity should be O(n).
- Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
解法及注释
class Solution {
public int[] countBits(int num) {
int dp[] = new int[num + 1];
for(int i = 1; i <= num; i++) {
int k = (int) (Math.log(i)/Math.log(2));
dp[i] = 1 + dp[i - (int)Math.pow(2, k)];
}
return dp;
}
}
Q****343. Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.
Example 1:
Input: 2
Output: 1
Explanation: 2 = 1 + 1, 1 × 1 = 1.
Example 2:
Input: 10
Output: 36
Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.
Note: You may assume that n is not less than 2 and not larger than 58.
解法及注释
public class Solution {
public int integerBreak(int n) {
//handle extreme cases
if(n<2)
return -1;
//handle special cases
if(n==2)
return 1;
if(n==3)
return 2;
if(n==4)
return 4;
if(n==5)
return 6;
if(n==6)
return 9;
int res=1;
//handle normal cases
if(n%3 == 0){
while(n>=3){
res *= 3;
n -= 3;
}
}else if(n%3==1){
while(n>4){
res *= 3;
n-=3;
}
res *= 4;
}else {
while(n>2){
res *= 3;
n -= 3;
}
res *= 2;
}
return res;
}
}
Q****392. Is Subsequence
Given two strings s and t, check if s is a subsequence of t.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
Constraints:
0 <= s.length <= 1000 <= t.length <= 104sandtconsist only of lowercase English letters.
解法及注释
class Solution {
public boolean isSubsequence(String s, String t) {
for(int i = 0; i < s.length(); i++) {
int idx = t.indexOf(s.charAt(i));
if(idx != -1)
t = t.substring(idx + 1);
else
return false;
}
return true;
}
//ignore below dynamic programming approach
private boolean isSubsequence(String s, String t, int idx) {
if(s == null && t == null)
return true;
if(s == null || t == null || s.length() == 0 || t.length() == 0)
return false;
if(s.charAt(idx) == t.charAt(idx))
return isSubsequence(s.substring(idx + 1), t.substring(idx + 1), idx + 1);
else
return isSubsequence(s, t.substring(idx + 1), idx + 1);
}
}
Q****413. Arithmetic Slices
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
For example, these are arithmetic sequences:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
The following sequence is not arithmetic.
1, 1, 2, 5, 7
A zero-indexed array A consisting of N numbers is given. A slice of that array is any pair of integers (P, Q) such that 0 <= P < Q < N.
A slice (P, Q) of the array A is called arithmetic if the sequence:
A[P], A[P + 1], ..., A[Q - 1], A[Q] is arithmetic. In particular, this means that P + 1 < Q.
The function should return the number of arithmetic slices in the array A.
Example:
A = [1, 2, 3, 4]
return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.
解法及注释
class Solution {
int total = 0;
public int numberOfArithmeticSlices(int[] A) {
calc(A, A.length -1);
return total;
}
private int calc(int[] a, int i) {
if(i < 2)
return 0;
int result = 0;
if(a[i] + a[i - 2] == 2*a[i - 1]) {
result = 1 + calc(a, i - 1);
total += result;
} else
calc(a, i - 1);
return result;
}
}
Q****446. Arithmetic Slices II - Subsequence
A sequence of numbers is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
For example, these are arithmetic sequences:
1, 3, 5, 7, 9
7, 7, 7, 7
3, -1, -5, -9
The following sequence is not arithmetic.
1, 1, 2, 5, 7
A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, ..., Pk) such that 0 ≤ P0 < P1 < ... < Pk < N.
A subsequence slice (P0, P1, ..., Pk) of array A is called arithmetic if the sequence A[P0], A[P1], ..., A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.
The function should return the number of arithmetic subsequence slices in the array A.
The input contains N integers. Every integer is in the range of -231 and 231-1 and 0 ≤ N ≤ 1000. The output is guaranteed to be less than 231-1.
Example:
Input: [2, 4, 6, 8, 10]
Output: 7
Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]
解法及注释
class Solution {
public int numberOfArithmeticSlices(int[] A) {
int len = A.length;
long res = 0;
//the map arrays stores the starting point of each element
//for each map, it stores diff/count pair
Map<Integer, Integer>[] count = new Map[len];
for(int i = 0; i < len; i++) {
count[i] = new HashMap();
for(int j = 0; j < i; j++) {
long delta = (long)A[i] - (long)A[j];
if(delta < Integer.MIN_VALUE || delta > Integer.MAX_VALUE)
continue;
int diff = (int)delta;
int sum = count[j].getOrDefault(diff, 0);
int original = count[i].getOrDefault(diff, 0);
count[i].put(diff, original + sum + 1);
res += sum;
}
}
return (int)res;
}
}
Q****464. Can I Win
In the "100 game" two players take turns adding, to a running total, any integer from 1 to 10. The player who first causes the running total to reach or exceed 100 wins.
What if we change the game so that players cannot re-use integers?
For example, two players might take turns drawing from a common pool of numbers from 1 to 15 without replacement until they reach a total >= 100.
Given two integers maxChoosableInteger and desiredTotal, return true if the first player to move can force a win, otherwise return false. Assume both players play optimally.
Example 1:
Input: maxChoosableInteger = 10, desiredTotal = 11
Output: false
Explanation:
No matter which integer the first player choose, the first player will lose.
The first player can choose an integer from 1 up to 10.
If the first player choose 1, the second player can only choose integers from 2 up to 10.
The second player will win by choosing 10 and get a total = 11, which is >= desiredTotal.
Same with other integers chosen by the first player, the second player will always win.
Example 2:
Input: maxChoosableInteger = 10, desiredTotal = 0
Output: true
Example 3:
Input: maxChoosableInteger = 10, desiredTotal = 1
Output: true
Constraints:
1 <= maxChoosableInteger <= 200 <= desiredTotal <= 300
解法及注释
class Solution {
Map<Integer, Boolean> cache = new HashMap<>();
public boolean canIWin(int maxChoosableInteger, int desiredTotal) {
if (desiredTotal <= 0)
return true;
if (maxChoosableInteger * (maxChoosableInteger+1)/2 < desiredTotal)
return false;
return win(maxChoosableInteger, desiredTotal, 0);
}
boolean win(int max, int val, int state) {
if (val <= 0)
return false;
if (cache.containsKey(state))
return cache.get(state);
boolean ans = false;
for (int i = 0; i < max; i++)
if ((1 << i & state) == 0) {
if (!win(max, val-i-1, 1 << i | state)) {
ans = true;
break;
}
}
cache.put(state, ans);
return ans;
}
}
Q****486. Predict the Winner
Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.
Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.
Example 1:
Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.
Example 2:
Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.
Constraints:
- 1 <= length of the array <= 20.
- Any scores in the given array are non-negative integers and will not exceed 10,000,000.
- If the scores of both players are equal, then player 1 is still the winner.
解法及注释
class Solution {
public boolean PredictTheWinner(int[] nums) {
return getWinner(nums, 0, nums.length - 1) >=0;
}
//use recursion
private int getWinner(int[] nums, int left, int right) {
if(left == right)
return nums[left];
return Math.max(nums[left] - getWinner(nums, left + 1, right),
nums[right] - getWinner(nums, left, right - 1));
}
}
Q****494. Target Sum
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
Find out how many ways to assign symbols to make sum of integers equal to target S.
Example 1:
Input: nums is [1, 1, 1, 1, 1], S is 3.
Output: 5
Explanation:
-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3
There are 5 ways to assign symbols to make the sum of nums be target 3.
Constraints:
- The length of the given array is positive and will not exceed 20.
- The sum of elements in the given array will not exceed 1000.
- Your output answer is guaranteed to be fitted in a 32-bit integer.
解法及注释
class Solution {
public int findTargetSumWays(int[] nums, int S) {
//dp is arrays stores all possible ways for a given sum
//here index is sum, value is sum
int[] dp = new int[2001];
//use 1000 as separator
dp[nums[0] + 1000] = 1;
dp[-nums[0] + 1000] += 1;
for(int i = 1; i < nums.length; i++) {
int[] next = new int[2001];
for(int sum = -1000; sum <= 1000; sum++) {
//the sum of ways has to be larger than 0
if(dp[sum + 1000] > 0) {
next[sum + nums[i] + 1000] += dp[sum + 1000];
next[sum - nums[i] + 1000] += dp[sum + 1000];
}
}
dp = next;
}
return S > 1000 ? 0 : dp[S + 1000];
}
}
Q****516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
Example 1:
Input:
"bbbab"
Output:
4
One possible longest palindromic subsequence is "bbbb".
Example 2:
Input:
"cbbd"
Output:
2
One possible longest palindromic subsequence is "bb".
Constraints:
1 <= s.length <= 1000sconsists only of lowercase English letters.
****解法及注释
class Solution {
public int longestPalindromeSubseq(String s) {
//pre-check
if(s == null || s.length() == 0)
return 0;
int n = s.length();
//result array
int[][] res = new int[n][n];
for(int len = 1; len <= n; len++) {
for(int i = 0; i + len - 1 < n; i++) {
int j = i + len -1;
if(len == 1)
res[i][j] = 1;
else if(s.charAt(i) == s.charAt(j))
res[i][j] = res[i+1][j-1] + 2;
else
res[i][j] = Math.max(res[i+1][j], res[i][j-1]);
}
}
return res[0][n-1];
}
}
Q****647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example 1:
Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Note:
- The input string length won't exceed 1000.
****解法及注释
class Solution {
public int countSubstrings(String s) {
//pre-check
if(s == null || s.length() == 0)
return 0;
//start with center
int n = s.length(), ans = 0;
for(int center = 0 ; center <= 2 * n - 1; ++center) {
int left = center / 2;
int right = left + center % 2;
while(left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
left--;
right++;
ans++;
}
}
return ans;
}
}
Q****517. Super Washing Machines
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 <-- 5 => 1 1 4
2nd move: 1 <-- 1 <-- 4 => 2 1 3
3rd move: 2 1 <-- 3 => 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 <-- 3 0 => 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
- The range of n is [1, 10000].
- The range of dresses number in a super washing machine is [0, 1e5].
****解法及注释
class Solution {
public int findMinMoves(int[] machines) {
if(machines == null || machines.length == 0)
return 0;
int sum = 0;
for(int i : machines)
sum += i;
if(sum % machines.length != 0)
return -1;
int avg = sum / machines.length;
int max = 0, curr = 0, prev = 0, tot = 0;
for(int i = 0; i < machines.length; i++) {
curr += machines[i] - avg;
if(curr >= 0 && prev >= 0)
tot = curr + prev;
else
tot = Math.max(Math.abs(curr), Math.abs(prev));
max = Math.max(max, tot);
prev = -curr;
}
return max;
}
}
Q****718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays.
Example 1:
Input:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
Output: 3
Explanation:
The repeated subarray with maximum length is [3, 2, 1].
Note:
- 1 <= len(A), len(B) <= 1000
- 0 <= A[i], B[i] < 100
****解法及注释
class Solution {
public int findLength(int[] A, int[] B) {
//pre-check
if(A.length == 0 || A == null || B.length == 0 || B == null)
return 0;
return getAnsByDP(A,B);
}
private int getAnsByDP(int[] A, int[] B) {
int res = 0;
//initialize the two dimentional array
int[][] matrix = new int[A.length + 1][B.length + 1];
for(int i = A.length -1; i >= 0; --i) {
for(int j = B.length -1; j >= 0; --j) {
if(A[i] == B[j]) {
matrix[i][j] = matrix[i+1][j+1] + 1;
if(res < matrix[i][j])
res = matrix[i][j];
}
}
}
return res;
}
}
Q****746. Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.
Example 1:
Input: cost = [10, 15, 20]
Output: 15
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
Example 2:
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
Output: 6
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
Note:
costwill have a length in the range[2, 1000].- Every
cost[i]will be an integer in the range[0, 999].
****解法及注释
class Solution {
public int minCostClimbingStairs(int[] cost) {
if(cost == null || cost.length == 0)
return 0;
int f1 = 0, f2 = 0;
for(int i = cost.length - 1; i >= 0; i--) {
int f0 = cost[i] + Math.min(f1, f2);
f2 = f1;
f1 = f0;
}
return Math.min(f1, f2);
}
}