• Best Time to Buy and Sell Stock II


    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).

    思路很简单,就是把所有相邻2天增量相加即可,代码如下:

    public class Solution {
        public int maxProfit(int[] prices) {
            int size = prices.length;
            int profit = 0;
            for(int i=0;i<size-1;i++) {
                if(prices[i+1]>prices[i]) {
                    profit = profit+prices[i+1]-prices[i];
                }
            }
            return profit;
        }
    }
  • 相关阅读:
    包 (package)
    Object类
    异常
    接口
    抽象类
    多态(经典案例)
    三大特性:(经典代码)
    对象创建的过程(重点理解)
    final关键字
    cocos2dx工程中接入支付宝sdk
  • 原文地址:https://www.cnblogs.com/mrpod2g/p/4318820.html
Copyright © 2020-2023  润新知