算法:股票交易日

題目描述
在股市的交易日中,假設最多可進行兩次買賣(即買和賣的次數均小於等於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;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章