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

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

 

附上代码:

1 class Solution { 2 public: 3     int maxProfit(vector
&prices) { 4 unsigned int len = prices.size(); 5 if (len == 0) return 0; 6 // ans: holds the total profit that has been earned 7 int ans = 0; 8 for (int i = 0; i < len; ) { 9 // buy: holds price of the ith day where 10 // i is the maximum index satisfing11 // prices[i-1] > prices[i] and prices[i] <= prices[i+1]12 int buy = prices[i++];13 while (i < len && prices[i] < buy) {14 buy = prices[i++];15 }16 if (i >= len) return ans;17 // sell: holds price of the ith day where18 // i is the maximum index satisfing19 // prices[i-1] < prices[i] and prices[i] >= prices[i+1]20 int sell = prices[i++];21 while (i < len && prices[i] > sell) {22 sell = prices[i++];23 }24 ans += (sell - buy);25 }26 27 return ans;28 }29 };

 

转载于:https://www.cnblogs.com/Stomach-ache/p/3754200.html

你可能感兴趣的文章
Cannot find module `express`
查看>>
20051206: 早退
查看>>
凸包问题的描述(Graham法)
查看>>
JavaScript严格模式总结
查看>>
07-图
查看>>
20145127《java程序设计》第四周学习总结
查看>>
idea中,使用facets添加完web后,项目已变为web项目,但web.xml中内容经常变为红色,并报错,如何解决?...
查看>>
nginx支持https
查看>>
查看进程信息
查看>>
转载:DIV+CSS有可能遇到的问题
查看>>
protocol buffer
查看>>
web常用模块测试用例
查看>>
【转】分布式数据层 TDDL 来自:阿里巴巴
查看>>
swing常用布局
查看>>
#学习笔记#e2e学习使用(二)
查看>>
LeetCode 222.完全二叉树的节点个数(C++)
查看>>
20180307-Xen、KVM、VMware、hyper-v等虚拟化技术的比较
查看>>
在C#中??和?分别是什么意思?
查看>>
APP 开发,代码写的真烂
查看>>
适合0基础的web开发系列教程-html5新的表单元素
查看>>