`
huntfor
  • 浏览: 195564 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Best Time to Buy and Sell StockII

 
阅读更多

新博文:[leetcode]Best Time to Buy and Sell Stock II

http://oj.leetcode.com/problems/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).

 其实这道题相比于第一题http://huntfor.iteye.com/blog/2051366来讲,想法反而简单了

题中大意为,可以进行多次买入卖出,求最大收益,显然,我们只要把每一个增区间的极大值与极小值的差求出来,再将这些差值全部加起来就是了,因此现在题目的重点就变成了如何判断哪些点是极值点,我用的是比较偷懒的办法,维护三个指针,pre,post分别指向前后节点。通过比较前后节点的值与当前节点的值,就可以判断当前节点是否是极值点。

 

    public int maxProfit(int[] prices) {
		int pre = 0;
		int post = 1;
		int buy = 0;//买入点
		int profit = 0;//收益
		if(prices == null || prices.length == 1 || (prices.length == 2 && prices[0] >= prices[1])){
			return 0;
		}
		for(int tem = 0; tem < prices.length ; tem++,pre = tem - 1,post = tem + 1){
			if(post < prices.length && prices[tem] <= prices[pre] && prices[tem] < prices[post]){
				buy = prices[tem];
			}
			if( ( post < prices.length && prices[tem] > prices[pre] && prices[tem] >= prices[post]) || (post == prices.length && prices[tem] > prices[pre])){
				profit += prices[tem] - buy;
			}
		}
		return profit;
	
    }

 

 

 

 

分享到:
评论

相关推荐

    leetcode题目:Best Time to Buy and Sell Stock II

    leetcode题目:Best Time to Buy and Sell Stock II

    javalruleetcode-leetcode-java:力码笔记

    leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted ...

    股票收益leetcode-leetcode:leetcode摘要

    股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock (Easy) 一次交易,找最大收益 for i in prices: low = min(low, i) profit = max(profit, i-low) Best Time to Buy and Sell Stock II (Easy) ...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    cpp-算法精粹

    Best Time to Buy and Sell Stock II Longest Substring Without Repeating Characters Container With Most Water Patching Array 动态规划 Triangle Maximum Subarray Maximum Product Subarray Longest ...

    leetcode卡-leetcode:利特码解决方案

    leetcode卡 leetcode exercises 3-5 solutions everyday. fighting~ TODO array Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted ...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...

    leetcode小岛出水口-leetcode:leetcode训练

    leetcode小岛出水口 leetcode training one problem on file 为了方便文档管理,项目逐渐文档化,以单个文件作为集合最小集。 go语言不能完全适合写算法题 后续会尽可能加入rust或者c的题解 清单 0965. Univalued ...

    圆和矩形是否重叠leetcode-leetcode_solutions:leetcode_solutions

    圆和椭圆重叠leetcode ——#158 尖端 关心特殊情况 从不同的方向思考 简单的 大批 1.Two Sum -&gt; 使用哈希表避免遍历列表448.查找数组中消失的所有数字-&gt; 1.建立缓冲区来计算数字| 2.使用数组作为带符号的缓冲区118....

    leetcode-leetcode:leetcode的最佳C++解决方案

    leetcode leetcode The optimum C++ solutions for the leetcode 这是我自己写的leetcode的题解,目前已经完成了70%左右,后续部分会很快更新 这里放上来的代码都能保证是最优解(复杂度最优) 当然有些题目因为测试...

    LeetCode最全代码

    462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 ... Best_Time_To_Buy_And_Sell_StockII Java 简单的 125 Valid_Palindrome Java 简单的 136 单号 Java 简单的 137 Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 目标是BAT的算法岗哈哈哈哈哈 争取做到每日一更 嗯…… 19.10.22:鸽了这么久,我又回来了……主要在实验室天天没啥事,过于...

    leetcode中文版-leetcode:leetcode

    leetcode中文版车鸟 一组用python编写的算法。 种类 ...best_time_to_buy_and_sell_stock_II binary_tree_inorder_traversal binary_tree_level_order_traversal binary_tree_level_order_traversal_I

    gasstationleetcode-LeetCode_Practice:我的LeetCode练习从2020年开始

    加油站 leetcode 力扣_实践 标签: leetcode 我的 LeetCode 练习从 2020 年开始 ...Leetcode ...80_Remove_Duplicates_From_Sorted_Array_II ...299_Bulls_and_Cows ...122_Best_Time_to_Buy_and_Sell_Stock_

    leetcode中文版-cabbird:一组用python编写的算法

    leetcode中文版车鸟 一组用python编写的算法。 种类 ...best_time_to_buy_and_sell_stock_II binary_tree_inorder_traversal binary_tree_level_order_traversal binary_tree_level_order_traversal_I

    leetcode数组去重-algorithm::memo:解决算法问题

    Best-Time-to-Buy-and-Sell-Stock-II | | +- 위의 구조 동일 • • • 문제 폴더명 네이밍 : 문제 이름으로 쓰되 띄어쓰기는 '-'으로 연결 # 自动文件夹创建脚本 您是否为创建和重命名文件夹而烦恼? 试试这个! 把...

    收集面试中提出的一些重要问题。-C/C++开发

    buy-and-sell-stock-ii/ https://leetcode.com/problems/maximum-subarray/ https://leetcode.com/problems/first -missing-positive / https://leetcode.com/problems/container-with-most-water/ htt

Global site tag (gtag.js) - Google Analytics