题目描述
在股市的交易日中,假设最多可进行两次买卖(即买和卖的次数均小于等于2),规则是必须一笔成交后进行另一笔(即买-卖-买-卖的顺序进行)。给出一天中的股票变化序列,请写一个程序计算一天可以获得的最大收益。请采用实践复杂度低的方法实现。
给定价格序列prices及它的长度n,请返回最大收益。保证长度小于等于500。
测试样例:
[10,22,5,75,65,80],6
返回: 87
package questions;
import java.util.ArrayList;
import java.util.List;
public class StockTradingDay2 {
private int [] prices;//给定的价格序列;
private int num ;//价格序列的长度;
private int count;//一天可以买卖的次数
private List<Node> list;//根据价格生成的新集合
public void createNode() {
if(null == prices || prices.length ==0) {
System.out.println("初始化的价格数组出现错误!");
}
if(count<1 || count>num/2) {
System.out.println("初始化的买卖次数出现错误!");
}
list = new ArrayList<Node>();
for(int i =0;i<num-1;i++) {
for(int j = i+1;j<num;j++) {
if(prices[j]>prices[i]) {
Node node = new Node(i, j, prices[j]-prices[i]);
list.add(node);
}
}
}
}
public void computeProfits() {
if(list == null || list.size() ==0) {
System.out.println("今天不适合进行交易!");
}
int n = makeProfits(list);
System.out.println("最大利润为:"+n);
}
private int makeProfits(List<Node> tempList) {
int max = tempList.get(0).getValue();
List<Integer> list2 = new ArrayList<Integer>();
for(int i =0;i<tempList.size();i++) {
Node n = tempList.get(i);
list2.add(n.getValue());
if(n.getValue()>max) {
max = n.getValue();
}
//从list集合中挑出2个元素,并且元素之间不交叉
for(int j =0;j<tempList.size();j++) {
Node m = tempList.get(j);
if(i != j && n.getStart()<m.getStart() && n.getEnd()<m.getStart()) {
list2.add(n.getValue()+m.getValue());
}
}
}
for(Integer n : list2) {
if(n>max) {
max = n;
}
}
return max ;
}
public static void main(String[] args) {
int [] prices = new int [] {10,22,5,75,65,80};
StockTradingDay2 st = new StockTradingDay2(prices, 6, 2);
st.createNode();
st.computeProfits();
}
public StockTradingDay2(int[] prices, int num, int count) {
super();
this.prices = prices;
this.num = num;
this.count = count;
}
public int[] getPrices() {
return prices;
}
public void setPrices(int[] prices) {
this.prices = prices;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
class Node {
private int start;
private int end;
private int value;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node(int start, int end, int value) {
super();
this.start = start;
this.end = end;
this.value = value;
}
}
}
———————-分隔线—————–
进阶篇,不限定交易次数
package questions;
import java.util.ArrayList;
import java.util.List;
public class StockTradingDay3 {
private int [] prices;//给定的价格序列;
private int num ;//价格序列的长度;
private int count;//一天可以买卖的次数
private List<Node> list;//根据价格生成的新集合
//可以认为所有的合理的交易都是为正数的。
public void createNode() {
if(null == prices || prices.length ==0) {
System.out.println("初始化的价格数组出现错误!");
}
if(count<1 || count>num/2) {
System.out.println("初始化的买卖次数出现错误!");
}
list = new ArrayList<Node>();
for(int i =0;i<num-1;i++) {
for(int j = i+1;j<num;j++) {
if(prices[j]>prices[i]) {
Node node = new Node(i, j, prices[j]-prices[i]);
list.add(node);
}
}
}
}
public void computeProfits() {
if(list == null || list.size() ==0) {
System.out.println("今天不适合进行交易!");
}
int n = 1;
do {
list = makeProfits(list);
n++;
}while(n<count);
//获取最大的利润
int max = list.get(0).getValue();
for(Node nd : list) {
if(nd.getValue()>max) {
max = nd.getValue();
}
}
System.out.println("最大利润为:"+max);
}
private List<Node> makeProfits(List<Node> tempList) {
List<Node> newlist = new ArrayList<Node>();
//使用list是不确定元素个数
List<Integer> list2 = new ArrayList<Integer>();
for(int i =0;i<tempList.size();i++) {
Node n = tempList.get(i);
list2.add(n.getValue());
newlist.add(n);
//从list集合中挑出2个元素,并且元素之间不交叉
for(int j =0;j<tempList.size();j++) {
Node m = tempList.get(j);
if(i != j && n.getStart()<m.getStart() && n.getEnd()<m.getStart()) {
//如果可以交易多次,是否可以构造新的元素 start = n.getStart()
// end = m.getEnd() value = n.getValue()+m.getValue();
//在此基础上,还是找两个元素
list2.add(n.getValue()+m.getValue());
Node node = new Node(n.getStart(), m.getEnd(), n.getValue()+m.getValue());
newlist.add(node);
}
}
}
return newlist;
}
public static void main(String[] args) {
int [] prices = new int [] {10,22,5,75,65,80};
StockTradingDay3 st = new StockTradingDay3(prices, 6, 3);
st.createNode();
st.computeProfits();
}
public StockTradingDay3(int[] prices, int num, int count) {
super();
this.prices = prices;
this.num = num;
this.count = count;
}
public int[] getPrices() {
return prices;
}
public void setPrices(int[] prices) {
this.prices = prices;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
class Node {
private int start;
private int end;
private int value;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node(int start, int end, int value) {
super();
this.start = start;
this.end = end;
this.value = value;
}
}
}