设f[i]为窃贼在前 i 栋房子最多偷多少金币f[i] = max{ f[i-1],f[i-2]+A[i-1]}
初始条件:
f[0]=0(没有房子,偷0枚金币)
f[1]=A[0]只有一栋房子肯定偷
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publiclonghouseRoober(int[] A){ // use long int n = A.length; if (n == 0) return0; if (n == 1) return A[0]; long old = 0;// f[0] long now = A[0];// f[1] for (int i = 2; i <= n; i++) { // now = f[i-1] // old = f[i-2] long t = Math.max(now, old + A[i - 1]);// f[i] old = now; now = t; } return now; }
publicintcalc(int[] A){ int n = A.length; if(n==0)return0; if(n==1)return A[0]; int old =0; int now = A[0]; for(int i =2;i<=n;i++){ int t = Math.max(now,old+A[i-1]); old = now; now = t; } return now; } publiclonghouseRoober2(int[] nums){ // use long int n = nums.length; if (n == 0) return0; if (n == 1) return nums[0]; int[] A = newint[n-1]; int res = Integer.MIN_VALUE; int i; for(i=0;i<n-1;i++){ A[i]=nums[i]; } res = Math.max(res,calc(A)); for(i=0;i<n-1;i++){ A[i] = nums[i+1]; } res = Math.max(res,calc(A)); return res; }
Best Time To Buy And Sell StockI
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example
Given array [3,2,3,1,2], return 1.
1 2 3 4 5 6 7 8
publicintmaxProfit(int[] A){ int res = 0, mn = INT_MAX; for (int i = 0; i < A.length; ++i) { mn = Math.min(mn, A[i]); res = Math.max(res, A[i] - mn); } return res; }
Best Time To Buy And Sell StockII
Problem:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times ). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example
Given an example [2,1,2,0,1], return 2
1 2 3 4 5 6 7 8 9
intmaxProfit(vector<int> &prices){ int res = 0, n = prices.size(); for (int i = 0; i < n - 1; ++i) { if (prices[i] < prices[i + 1]) { res += prices[i + 1] - prices[i]; } } return res; }
Best Time To Buy And Sell StockIII
Problem
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
publicintmaxProfit(int K, int[] P){ int n = P.length; if (n == 0) return0; int i, j; if (K > n / 2) {//如果K>N/2,题目可以简化为第二种类型 II int sum = 0; for (i = 0; i < n - 1; i++) { if (P[i + 1] > P[i]) { sum += P[i + 1] - P[i]; } } return sum; } // int[][] f = new int[n + 1][2 * K + 1]; //使用滚动数组,所有i的改为now,i-1的为old int[][] f = newint[2][2 * K + 1]; int old=0,now=0;
f[now][1] = 0; for (i = 2; i <= 2 * K + 1; i++) { f[now][i] = Integer.MIN_VALUE; } for (i = 1; i <= n; i++) { // 阶段1、3、5、...、2*K + 1 for (j = 1; j <= 2 * K + 1; j += 2) { f[now][j] = f[old][j]; if (j > 1 && i >= 2 && f[old][j - 1] != Integer.MIN_VALUE) { f[now][j] = Math.max(f[now][j], f[old][j - 1] + P[i - 1] - P[i - 2]); } } // 阶段2、4...2*K for (j = 2; j <= 2 * K; j += 2) { f[now][j] = f[old][j - 1]; if (i >= 2 && f[old][j] != Integer.MIN_VALUE) { f[now][j] = Math.max(f[now][j], f[old][j] + P[i - 1] - P[i - 2]); } } } int res = Integer.MIN_VALUE; for (i = 1; i <= 2 * K + 1; i++) { res = Math.max(res, f[now][i]); } return res; }
序列+状态型动态规划小结
当思考动态规划最后一步时,这一步的选择依赖于前一步的某种状态
初始化时,f[0]代表前0个元素/前0天的情况(与坐标型动态规划区别)
计算式,f[i]代表前i个元素(即元素0~i-1)的某种性质
Russian Doll Envelopes
Problem:
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Example:
Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).