博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
110_leetcode_Best Time to Buy and sell Stock II
阅读量:6937 次
发布时间:2019-06-27

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

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

1:注意特殊情况;2:找到数组相邻的凹点和凸点;3:两者的差值是当前的最大值。4:在查找凸凹值的时候注意边界

int maxProfit(vector
&prices) { if(prices.size() <= 1) { return 0; } int maxValue = 0; int start = 0; int end = 0; int size = (int)prices.size(); while(start < size) { while(start < size - 1 && prices[start] >= prices[start + 1]) { start++; } end = start + 1; while(end < size - 1 && prices[end] <= prices[end + 1]) { end++; } if(end == size) { break; } else { maxValue += prices[end] - prices[start]; } start = end + 1; } return maxValue; }

转载地址:http://rrpjl.baihongyu.com/

你可能感兴趣的文章
3G路由器配置
查看>>
MySQL 常用SQL优化手段
查看>>
redis的持久化方式:RDB和AOF
查看>>
史上最全的css hack(ie6-9,firefox,chrome,opera,safari)
查看>>
监控磁盘
查看>>
关于mongodb的journal日志工作方式分析
查看>>
50种方法优化SQL Server
查看>>
Memcached
查看>>
httpd一之关于http工作模式的基本说明
查看>>
一天一句小贴士:早日告别试用期
查看>>
使用zabbix 2.4 监控mysql----自带模板
查看>>
Linux下多线程程序设计实验
查看>>
求二进制数中1的个数
查看>>
Cisco Pix 515e防火墙 配置×××实例--注释
查看>>
傲鹏ERP系统的一个BUG修正
查看>>
ubuntu12.04 “flash 设置” 无法点击
查看>>
BT下载怎样保护硬盘
查看>>
Docker Guide: Deploying Ghost Blog with MySQL
查看>>
exchange2010卸载后,再安装报错。
查看>>
root密码忘记的解决方法
查看>>