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 ...
随机推荐
- SQL查询最近三个月的数据(查询最近几天,几年等等)
定义和用法 :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId OrderPayD ...
- Spring集成Hessian
一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...
- HDOJ-1019 Least Common Multiple
http://acm.hdu.edu.cn/showproblem.php?pid=1019 题意:给出n个数,求它们的最小公倍数 对于n个数,它们的最小公倍数等于[前n-1个数的最小公倍数和第n个数 ...
- BindService总结
一.整体工程图 二.activity_bind.xml <?xml version="1.0" encoding="utf-8"?> <Lin ...
- php用apc实现的临界区 解决并发,资源互斥同步访问
在面对线程或进程的互斥同步的控制问题时,常用的解决办法是:临界区,互斥锁,信号量 临界区保证在某一时刻只有一个线程能够访问到所需资源的方法. 任何时候,只能至多有一个线程处于临界区中.如果多个线程要求 ...
- jquery 自动跳出列表
先上效果图:当鼠标经过相亲会自动弹出取最新的10条数据
- 在Unity3d编辑器中加入菜单以及菜单项
在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...
- 如何在编译内核时添加缺少的固件(随着intel wireless 5100 AGN的 iwlwifi 案例)
我不知道你在笔记本使用 Linux 在内核编译无线wifi 不能用.我的书"关联 Y450"一个足够的旧书,随着无线网卡: $ lspci | grep Wireless 06:0 ...
- Tomcat 内存与优化篇
Tomcat 内存与优化一.Tomcat 运行环境介绍 1.Tomcat 本身无法直接在计算机上运行,需要依赖硬件基础上的操作系统和Java虚拟机: 2.Java 程序启动时JVM都会分配一个初始内存 ...
- Android WebView 软键盘挡住输入框
解决方法一: 在所在的Activity中加入 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RES ...