LeetCode【122】Best Time to Buy and Sell Stock II

来源:互联网 发布:淘宝小商品拍摄技巧 编辑:程序博客网 时间:2024/06/05 17:34
/** * 题意:和121题不同的地方是可以多次买卖任意次,然后求多次买卖后最大利润 * * @auther Dennis * @date 2017/12/13 * 思想:贪心思想,最大的收益方法就是尽可能多的低入高抛,只要明天比今天价格高,就今天买,明天卖 */public class BestTimetoBuyandSellStockII {    public static int maxProfit(int[] prices) {        int maxProfit = 0;        for (int i = 1; i < prices.length; i++) {            if(prices[i]> prices[i-1])                maxProfit += prices[i]-prices[i-1];        }        return maxProfit;    }    public static void main(String[] args) {        int[] prices = new int[]{7,1,4,3,6};        System.out.println(maxProfit(prices));    }}
原创粉丝点击