最近看到网站上提到了leetcode网站,用来在线面试算法;就上去看了下,自己也解决了一题,蛮有意思的,偶尔做做算法练练脑。
题目:Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
大致的意思就是输入一个股票的股价数组,第i个元素代表第i天的股价,只允许买入卖出一次,问最大的利润是多少?
第一个反映和解决:
两层for循环遍历下即可,在浏览器中输入完成;提交"judge small"按钮,提示有非法字符;排除后,正确运行。代码如下:
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function //check the input if(prices==null||prices.length<=1){ return 0; } //proces int max = 0; int buyPoint = 0; int sellPoint = 1; for (int j=0;j<prices.length -1;j++){ buyPoint = j; for (int i=j+1;i<prices.length;i++){ sellPoint = i; if (prices[i]-prices[j]>max){ max = prices[i]-prices[j]; sellPoint = i; } } } // not buy if (max<0){ return 0; } return max; } }
但是当按"judge large"时,提示 运行超时:(
第二解决:
思考了下 o(N平方),能不能降到N(log N),花了大概5分钟思考和验证(算法知识已经遗忘了很多),居然找出了o(N)的算法:
1先找出股价最小值和股价最大值的位置,
1.1如果股价最小点的位置在股价最大值的位置之前,直接输出最大值和最小值的差,程序结束;
1.2 反之,三段式处理。
2,三段式处理:
2.1 从数组开始点到“股价最大值的位置”这一区间,只要找到这一段的最小值,然后和之前的最大值相减,就是这段可能的最大利润;P1
2.2 从"股价最小点的位置"到数组的结尾这一区间,只要找到这一段的最大值,冉舟和之前的最小值相减,就是这一段可能的最大李瑞:P3
2.3“股价最大值的位置”到"股价最小点的位置"这一区间,这段区间为止,递归解决;P2
编程好程序,再执行,还是提示超时。。。这个算法的的复杂度是O(N)啊,应该不可能有再快的算法了。突然想到java对递归支持不好,如果数据很长的话,可能StackOverFlow。
最终版本:
那就改成非递归的版本吧,改完再测试,果然ok了。
代码如下:
public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below // DO NOT write main() function //check the input if(prices==null||prices.length<=1){ return 0; } int start = 0; int end = prices.length-1; int max = 0; while (end - start >=1){ //find the smallest and biggest int smallest = prices[start]; int sPoint = start; int biggest = prices[start]; int bPoint = start; for (int i=start+1;i<=end;i++){ if (prices[i]<smallest){ smallest = prices[i]; sPoint = i; } if (prices[i]>biggest){ biggest = prices[i]; bPoint = i; } } if (sPoint<bPoint){ if (biggest-smallest>max){ max = biggest - smallest; } break; } // find the smallest in the interval int p1 = 0; int s1 = biggest; for (int i=start;i<bPoint;i++){ if (prices[i]<s1){ s1 = prices[i]; } } p1 = biggest -s1; if (p1>max){ max = p1; } // find the smallest in the third interval int p3 = 0; int b1 = smallest; for (int i=sPoint+1;i<=end;i++){ if (prices[i]>b1){ b1 = prices[i]; } } p3 = b1 - smallest; if (p3>max){ max = p3; } //find the smallest in the second interval start = bPoint+1; end = sPoint-1; } // not buy if (max<0){ return 0; } return max; } }
小结:
1)使用在线文本编辑器后,才知道Eclipse的好啊,一些无聊的错误在eclipse中根本就不会发生,而且如果有问题,直接输入异常,真的不行debug下就知道原因了;
2)leetcode网站的题目质量还是很高的;
3)这种方式的面试确实对动手能力要求很高(类ACM),而且能绕过一些麻烦的环境准备问题,对面试过程确实有帮助。
相关推荐
《最佳买卖股票时机II》是LeetCode中的一道经典编程题目,主要涉及动态规划和数组操作的知识点。在这个问题中,我们被赋予一个整数数组`prices`,表示某支股票每天的价格。任务是找到在允许进行多次交易的情况下,...
java java_leetcode题解之Best Time to Buy and Sell Stock with Cooldown
java java_leetcode题解之Best Time to Buy and Sell Stock IV.java
java java_leetcode题解之Best Time to Buy and Sell Stock III.java
java java_leetcode题解之Best Time to Buy and Sell Stock II.java
java java_leetcode题解之Best Time to Buy and Sell Stock I.java
leetcode LeetCode 解决题目的总数: 136/1753 微信公众号: 工程师Ruojhen 算法-动态规划 题号 名称 English Name 题解 53 最大子序和 Maximum Subarray 70 爬楼梯 Climbing Stairs 121 买卖股票的最佳时机 Best Time...
股票收益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) ...
python python_leetcode题解121_Best_Time_to_Buy_and_Sell_Stock
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 ...
python python_leetcode题解之123_Best_Time_to_Buy_and_Sell_Stock_III
python python_leetcode题解之122_Best_Time_to_Buy_and_Sell_Stock_II
javascript js_leetcode题解之121-best-time-to-buy-and-sell-stock.js
javascript js_leetcode题解之123-best-time-to-buy-and-sell-stock-iii.js
javascript js_leetcode题解之122-best-time-to-buy-and-sell-stock-ii.js
leetcode 分类 LeetCode LeetCode Java Solution 2018年5月31日 更新 最近刷了一遍 LeetCode,发现第一次的代码确实有些 low 很多代码并不简洁,本来1行的代码可以写成3行 思维混乱 没有按照题目类型分类 没有写结题...
公众号 coding 笔记、点滴记录,以后的文章也会...最近花时间分门别类整理了 LeetCode 的前 500 题,把相似的题目都放在了一起,比如Best Time to Buy and Sell Stock的6道题,这些题目放在一起刷效果更好。 简书博客:
63. 股票的最大利润题目链接Leetcode:121. Best Time to Buy and Sell Stock题目描述可以有一次买入和一次卖出,买入必
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 ...
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. ...