zoukankan      html  css  js  c++  java
  • [leedcode 122] 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).

    public class Solution {
        public int maxProfit(int[] prices) {
           /* 题目:假设有一个数组,其中的第 i 个元素代表给定的第 i 天的股票价格。
    
            设计一个算法找出最大的利润。你可以完成多个交易(如,买一和卖一股票 多次)。但是,你不能同时进行多个交易(如,你必须在新买入之前卖出)。
    
            思路:可以理解成求任意多个二元序列之差 之和 的最大值,其中每对序列之差需为正数,对应的索引要减号之前的大于减号之后的。
            
            如果是递增的序列对,就买入再卖出,求递增的所有序列的差和*/
            int res=0;
            for(int i=0;i<prices.length-1;i++){
                if(prices[i+1]>prices[i]){
                    res+=prices[i+1]-prices[i];
                }
            }
            return res;
        }
    }
  • 相关阅读:
    CentOS7安装mysql
    strusts2的开发包
    CentOS7配置mavne国内镜像
    卸载linux自带的jdk
    Centos7安装nodejs
    redis启动方式
    bash学习笔记——Shell变量
    bash学习笔记——bash是什么
    教育管理系统——android家长客户端
    php入门学习——php与jsp对比
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4672122.html
Copyright © 2011-2022 走看看