class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp = new int[nums.length];
int max = 0;
for (int i = 0; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i]<=nums[j]){
continue;
}
dp[i] = Math.max(dp[j]+1,dp[i]);
}
max = Math.max(max,dp[i]);
}
return max;
}
}
class Solution {
public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
if ( amount==0 ){
return 0;
}
if (amount < coins[0] ){
return -1;
}
int[] dp = new int[amount+1];
Arrays.fill(dp,-1);
int i = 1;
while (i <= amount){
for (int coin : coins) {
if (i < coin){
break;
}
if (i == coin){
dp[i] = 1;
break;
}else {
if (dp[i-coin] == -1){
continue;
}
if (dp[i] == -1){
dp[i] = dp[i-coin]+1;
}else {
dp[i] = Math.min(dp[i],dp[i-coin]+1);
}
}
}
i++;
}
return dp[amount];
}
}
class Solution {
public int numWays(int n, int k) {
if (n==1){
return k;
}
int[][] dp = new int[2][2];
dp[0][0] = k;
dp[0][1] = k * ( k - 1 );
int count = 2;
int cur = 1;
int before = 0;
while (count < n){
dp[cur][0] = dp[before][1];
dp[cur][1] = (dp[before][0] + dp[before][1]) * ( k - 1 );
count++;
if (cur == 1){
cur = 0;
before = 1;
}else {
cur = 1;
before = 0;
}
}
return dp[before][0]+dp[before][1];
}
}
dp[i][2] = dp[i-2][2] * ( k - 1 ) + dp[i-1][2] * ( k - 1 )
所以
class Solution {
public int numWays(int n, int k) {
if (n==1){
return k;
}
int[] dp = new int[n];
dp[0] = k;
dp[1] = k * k;
int idx = 2;
while (idx<n){
dp[idx] = (dp[idx-2] + dp[idx-1]) * ( k - 1);
idx++;
}
return dp[--idx];
}
}