`
testforvln
  • 浏览: 18949 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • superich2008: 1、去重可以用Set集合2、在排序后,相邻2个元素如果相同可以 ...
    4Sum

Best Time to Buy and Sell Stock I & II

 
阅读更多
啊 第一次直接过small和big测试 好爽!虽然主要是以前知道这个题目吧= =
以前一直想不清这个题目的算法,今天终于搞明白了。思路很简单:就是用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;
    }
}

囧 写了好几次都没过= = 主要原因是最后一天结束的时候,如果是上涨的话,按照原来算法就会不管,继续等以后的交易,可是以后就没有交易了,这样只能把最后一天单独拿出来考虑。囧,单独考虑的时候又出了一些错误,唉唉唉,还是太弱了


分享到:
评论

相关推荐

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

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

    lrucacheleetcode-LeetCode:CppSourceCode的LeetCode解决方案

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

    javalruleetcode-leetcode-java:力码笔记

    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:leetcode摘要

    股票收益leetcode LeetCode 股票问题 Best Time to Buy and Sell Stock ...一次交易,找最大收益 ...i ...i) ...i-low) ...i-buy1) #找第一次交易最大收益 buy2 = min(buy2, i-prof1) #找用第一次收益购买的股票仍

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

    Andy619-Zhu#CS-Notes-chu#63. 股票的最大利润1

    63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必

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

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

    股票买卖最佳时机leetcode-best-time-to-buy-and-sell-stock:让我们弄清楚什么时候买一些股票!这不应该在现

    所以让我们假设我们有一个数组,其中第i个项目是给定股票在第i天的价格。 现在,由于您刚刚开始了解股票,您每天只能完成一笔交易(买一卖一股股票)。 开发一个算法来找到最大的利润。 笔记: 在购买股票之前,您不...

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 ...题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

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

    leetcode浇花-LCSolutions:我的力扣解决方案

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

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

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

    LeetCode:LeetCode Java解决方案

    公众号 coding 笔记、点滴记录,以后的文章也会...最近花时间分门别类整理了 LeetCode 的前 500 题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:

    LeetCode:Leetcode-解决方案

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

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

    Best Time to Buy and Sell Stock with Transaction Fee 0713. 乘积小于K的子数组 0695. 岛屿的最大面积 0674. 最长连续递增序列 0673. 最长递增子序列的个数 0567. 字符串的排列 0563. Binary Tree Tilt 0547. 朋友...

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

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

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

    了解加法过程122.Best Time to Buy and Sell Stock II -&gt; 动态规划 -&gt; 27.Remove Element -&gt; 两个指针,前后,交换268.Missing Number -&gt; 数字之和 [0,1,2,...,n] 是一个常数,所以可以计算出缺失的那个169.Majority...

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

    leetcode 答案 LeetCode-Trip LeetCode刷题代码,大佬勿入。 为一年后的研究生找工作准备 ...Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]

    股票买卖最佳时机leetcode-UCI_Project3_TheAvengers:UCI_Project3_TheAvengers

    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题解

    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_

Global site tag (gtag.js) - Google Analytics