动态规划
来源:互联网 发布:淘宝儿童玩具大全 编辑:程序博客网 时间:2024/06/05 19:53
动态规划题目:Say you have an array for which the i-th element is the price of a given stock on day i.Design an algorithm and implement it to nd the maximum pro t. You may complete at most two transactions.Note: You may not engage in multiple transactions at the same time (ie,you must sell the stock before you buy again).
代码:
import java.io.ObjectInputStream.GetField;public class Max_Rock_Profit {static int Rock_Price[] = { 2, 5, 8, 15, 4, 28, 20, 33 };static int N = Rock_Price.length;public static int Max_Profit(int a[]) {int max = Integer.MIN_VALUE;int Left_min = Integer.MAX_VALUE;int[] Left_Max = new int[N];int[] Right_Max = new int[N];int diff;for (int i = 0; i < N; i++) {if (a[i] < Left_min)Left_min = a[i];diff = a[i] - Left_min;if (diff > max)max = diff;Left_Max[i] = max;}max = Integer.MIN_VALUE;int Max_Right = Integer.MIN_VALUE;for (int j = N - 1; j >= 0; j--) {if (a[j] > Max_Right)Max_Right = a[j];diff = Max_Right - a[j];if (diff > max)max = diff;Right_Max[j] = max;}max = Integer.MIN_VALUE;for (int k = 0; k < N - 1; k++) {int m = Left_Max[k] + Right_Max[k + 1];if (m > max)max = m;}return max;}public static void main(String[] args) { System.out.println(Max_Profit(Rock_Price));}}
0 0
- 动态规划!!!动态规划!!!
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 动态规划
- 雾天和水下图像增强仿真
- sql小结
- Week3-1Semantic similarity:Synonymy and other Semantic Relations
- php snmp counter64 无法显示
- Name for argument type [java.lang.String] not available, and parameter name information not found in
- 动态规划
- LightOJ 1002 Country Roads
- 安装操作系统centos6.5手动分区以及设置网络连通性
- git 的学习
- Android-SeekBar(拖动进度条)
- ubuntu14.10,解决按照最新版Gnome 15.10后,经典Gnome桌面字体问题!
- angular学习资源
- 当你的Windows系统运行速度减慢时
- CC Arithmetic Progressions (FFT + 分块处理)