啊 第一次直接过small和big测试 好爽!虽然主要是以前知道这个题目吧= =
以前一直想不清这个题目的算法,今天终于搞明白了。思路很简单:就是用i遍历第2到最后一天,看第i天卖最大的profit是多少。当然,这就需要维护一个前i项最小值的变量,很简单啦。这里还有一个问题,就是如果最大profit都小于0,那不如不卖。就像期货界里大家说的那样,你不交易,就已经超越80%的人了 = =
恩 代码就是这样。。。
然后手痒做了后面的II,发现概念有点类似于<modeling maximum profit with C++>这本书里的potential profit概念,只是<modeling>这本书里可以卖空,这道题只可以买进而已
思路应该比较简单,识别所有的上升波段即可。
囧 写了好几次都没过= = 主要原因是最后一天结束的时候,如果是上涨的话,按照原来算法就会不管,继续等以后的交易,可是以后就没有交易了,这样只能把最后一天单独拿出来考虑。囧,单独考虑的时候又出了一些错误,唉唉唉,还是太弱了
以前一直想不清这个题目的算法,今天终于搞明白了。思路很简单:就是用i遍历第2到最后一天,看第i天卖最大的profit是多少。当然,这就需要维护一个前i项最小值的变量,很简单啦。这里还有一个问题,就是如果最大profit都小于0,那不如不卖。就像期货界里大家说的那样,你不交易,就已经超越80%的人了 = =
恩 代码就是这样。。。
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function int length = prices.length; int max; if (length > 1) max = prices[1] - prices[0]; else max = 0; if (max < 0) max = 0; int min = (length > 0 ? prices[0]: 0); for (int i = 1; i < length; ++i){ if (prices[i] < min){ min = prices[i]; continue; } if (prices[i] - min > max) max = prices[i] - min; } return max; } }
然后手痒做了后面的II,发现概念有点类似于<modeling maximum profit with C++>这本书里的potential profit概念,只是<modeling>这本书里可以卖空,这道题只可以买进而已
思路应该比较简单,识别所有的上升波段即可。
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function int trend = 0; int length = prices.length; if (length < 2) return 0; int min = prices[0]; int profit = 0; for (int i = 1; i < length - 1; ++i){ if (prices[i] > prices[i-1]){ if (trend != 1){ trend = 1; min = prices[i-1]; } } else{ if (trend == 1){ profit += prices[i-1] - min; trend = -1; } } } if (prices[length-1] > prices[length-2]){ if (trend != 1) profit += prices[length-1] - prices[length-2]; else profit += prices[length-1] - min; } else if (trend == 1) profit += prices[length-2] - min; return profit; } }
囧 写了好几次都没过= = 主要原因是最后一天结束的时候,如果是上涨的话,按照原来算法就会不管,继续等以后的交易,可是以后就没有交易了,这样只能把最后一天单独拿出来考虑。囧,单独考虑的时候又出了一些错误,唉唉唉,还是太弱了
发表评论
-
Insert Interval
2012-11-11 01:33 543各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 582唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1093Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 589唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 670动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 714自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 693public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 748不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 605全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 609一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Balanced Binary Tree
2012-11-01 23:38 609/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 586这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 600public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 734本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
leetcode题目:Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖股票的最佳时机Ⅳ Best Time to Buy and ...
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring ...121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time ...II
股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍
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 ...
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 Invert Binary Tree #237 ...
所以让我们假设我们有一个数组,其中第i个项目是给定股票在第i天的价格。 现在,由于您刚刚开始了解股票,您每天只能完成一笔交易(买一卖一股股票)。 开发一个算法来找到最大的利润。 笔记: 在购买股票之前,您不...
leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 ...题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
Best Time to Buy and Sell Stock II 6 GROUP ANAGRAMS 7 COUNTING ELEMENTS 日 问题描述 问题和解决方案链接 Git 解决方案页面 8 Middle of the Linked List 9 Backspace String Compare 10 Min Stack 11 Diameter ...
Time to Buy and Sell Stock #0125 - Valid Palindrome #0136 - Single Number #0167 - Two Sum - Input Array is sorted #0189 - Rotate Array #0217 - Contains Duplicate #0242 - Valid Anagram #0243 - Shortest...
Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic ...
公众号 coding 笔记、点滴记录,以后的文章也会...最近花时间分门别类整理了 LeetCode 的前 500 题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
Best Time to Buy and Sell Stock [121]3. Climbing Stairs [70]4. Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. Palindromic Substrings [647]9. ...
Best Time to Buy and Sell Stock with Transaction Fee 0713. 乘积小于K的子数组 0695. 岛屿的最大面积 0674. 最长连续递增序列 0673. 最长递增子序列的个数 0567. 字符串的排列 0563. Binary Tree Tilt 0547. 朋友...
leetcode leetcode The optimum C++ solutions for the leetcode 这是我自己写的leetcode的题解,目前已经完成了70%左右,后续部分会很快更新 这里放上来的代码都能保证是最优解(复杂度最优) 当然有些题目因为测试...
了解加法过程122.Best Time to Buy and Sell Stock II -> 动态规划 -> 27.Remove Element -> 两个指针,前后,交换268.Missing Number -> 数字之和 [0,1,2,...,n] 是一个常数,所以可以计算出缺失的那个169.Majority...
leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 ...Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]
Stock 提取应用程序接口。 用户的故事 Investing can be a daunting task for those who are new to the game. I want to be able to predict a stocks price over the next week so that I know when is the best ...
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_