【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

  1. 云栖社区>
  2. 博客>
  3. 正文

【LeetCode从零单排】No121 Best Time to Buy and Sell Stock

傲海 2015-04-01 09:16:00 浏览535
展开阅读全文

题目

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.

又是时间复杂度,离真相就差一行代码。。。还是没想出来,看了discuss才知道答案。


代码

public class Solution {
    public int maxProfit(int[] prices) {

        if (pr

网友评论

登录后评论
0/500
评论
傲海
+ 关注