110_leetcode_Best Time to Buy and sell Stock II
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了110_leetcode_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).
1:注意特殊情况;2:找到数组相邻的凹点和凸点;3:两者的差值是当前的最大值。4:在查找凸凹值的时候注意边界
int maxProfit(vector<int> &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; }
以上是关于110_leetcode_Best Time to Buy and sell Stock II的主要内容,如果未能解决你的问题,请参考以下文章
Nginx 高并发下报错 connect() failed (110: Connection timed out) while connecting to upstream
______more time ,the scientists will be able to work out a good solution to the problem
24.leetcode121_best_time_to_buy_and_sell_stock
121. Best Time to Buy and Sell Stock
leetcode第一刷_Best Time to Buy and Sell Stock
Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令时问题