Best Time to Buy and Sell Stock 解答
Question
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.
Solution
Key to the problem is to record money that we buy the stock.
For each element prices[i], we need to compare it with current buy-in price "buyIn":
If prices[i] > buyIn, we calculate the profit and compare it with current profit.
If prices[i] < buyIn, we set prices[i] as new buyIn.
Time complexity O(n), space cost O(1)
public class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length < 2)
return 0;
int result = Integer.MIN_VALUE;
int buyIn = prices[0], length = prices.length;
for (int i = 1; i < length; i++) {
if (prices[i] > buyIn)
result = Math.max(result, prices[i] - buyIn);
else
buyIn = prices[i];
}
if (result < 0)
result = 0;
return result;
}
}
Best Time to Buy and Sell Stock 解答的更多相关文章
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 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 al ...
- Best Time to Buy and Sell Stock系列
I题 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- Best Time to Buy and Sell Stock I II III IV
一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
随机推荐
- Poj2761-Feed the dogs(伸展树求名次)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- 【POJ3299】Humidex(简单的数学推导)
公式题中已经给出,直接求解即可. #include <iostream> #include <cstdlib> #include <cstdio> #include ...
- 十分钟搭建自己的hadoop2/CDH4集群
版本及准备 我部署的是hadoop-2.0.0-cdh4.2.0.tar.gz,下载地址为http://archive.cloudera.com/cdh4/cdh/4/hadoop-2.0.0-cdh ...
- mysql 基础技术
一.树状结构 参考http://www.cnblogs.com/kingteach/archive/2011/07/05/2098046.html )) begin declare lev int; ...
- 编码规范(HTML)
code { font-family: "PT Mono", Menlo, "Courier New", monospace; padding: 2px 4px ...
- Js获取Gridview中Dropdownlist选中状态
在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...
- json返回数据拼接HTML
<div class="box-lists"> </div> $.ajax({ url: 'AjaxPage/AjaxHandler.ashx', typ ...
- java final 和instanceof 关键字
/* final class A { public final void eat(){ System.out.println("测试"); } } */ class A { } c ...
- 一个项目涉及到的50个Sql语句(整理版)
/* 标题:一个项目涉及到的50个Sql语句(整理版) 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句. */ --1.学生表Student(S,Sname,Sage,Sse ...
- 自定义UIView动画效果
最普通动画: //开始动画 [UIView beginAnimations:nil context:nil]; //设定动画持续时间 [UIView setAnimationDuration:]; / ...