博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetCode-Best Time to Buy and Sell Stock II
阅读量:4623 次
发布时间:2019-06-09

本文共 806 字,大约阅读时间需要 2 分钟。

Description:

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

My Solution:

class Solution {    public int maxProfit(int[] prices) {        int bestPrice = 0;        int len = prices.length;        for(int i = 0;i < len - 1;i++){            if(prices[i + 1] >= prices[i]){                bestPrice += prices[i + 1] - prices[i];            }        }        return bestPrice;    }}

总结:

注意股价走向,股价上升就买进,第二天卖出。

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/kevincong/p/7881337.html

你可能感兴趣的文章
洛谷1144 最短路计数
查看>>
BZOJ 1207: [HNOI2004]打鼹鼠
查看>>
堆排序
查看>>
android下网络通信流程
查看>>
Spring+shiro session与线程池的坑
查看>>
Python基础学习笔记02之list
查看>>
jquery实现拖拽的效果
查看>>
JS 获取图片标签和所有的图片中的src的正则表达式
查看>>
jQuery:1.5.5.2,京东导航(当前默认设置)
查看>>
ASP.NET中 DetailsView(详细视图)的使用前台绑定
查看>>
我又情不自禁了——立方网的又一次加速度
查看>>
如何屏蔽国内IP访问我们的网站的一些方法!
查看>>
起与伏
查看>>
2.网络编程-udp
查看>>
GitHub笔记(三)——分支管理和多人协作
查看>>
Shell.xaml
查看>>
connection reset by peer, socket write error问题排查
查看>>
【luogu P4113 [HEOI2012]采花】 假题解
查看>>
第N次从零开始学Java笔记及思考_第一部分_基本语法(三)
查看>>
python里的Join函数
查看>>