Leetcode-Best Time to Buy and Sell Stock -java
题目:
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.
分析:
思路:贪心算法找出股票最低用来计算最大收益。同一时候保存眼下为止的最大收益,并依据产生的新的最大收益是否是最大来进行更新。
代码:
public class Solution {
public int maxProfit(int[] prices) {
int lowest = 0;
int maxProfit = 0;
if(prices.length > 0){
lowest = prices[0];
for(int i = 0; i < prices.length; i++){
if(lowest > prices[i]){
lowest = prices[i];
}
maxProfit = Math.max(maxProfit, prices[i] - lowest);
}
}
return maxProfit;
}
}
PS:
Math.max(val1, val2);用来比較val1和val2的大小
Leetcode-Best Time to Buy and Sell Stock -java的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [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 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 ...
- [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 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock with Transaction Fee 买股票的最佳时间含交易费
Your are given an array of integers prices, for which the i-th element is the price of a given stock ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
随机推荐
- Linux系统串口接收数据编
http://blog.csdn.net/bg2bkk/article/details/8668576 之前基于IBM deveplopworks社区的代码,做了串口初始化和发送的程序,今天在此基础上 ...
- Linux 操作系统启动流程
1.加载bios bios中包含的硬件CPU 内存 硬盘等相关信息 2.读取MBR 读取完bios信息之后,计算机会查找bios制定的硬盘MBR引导扇区,将其内容复制到 0x7c00 地址所在的物理内 ...
- python课堂知识的几点总结
1.执行python脚本的两种方式:利用python进入解释器和找到可执行文件1.py 2.位和字节:一个字节是8位,计算机运行时是以字节为单位,存储的时候是以位为单位 3.编码发展:ASCII只有0 ...
- JavaScript中的常用的数组操作方法
JavaScript中的常用的数组操作方法 一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2 ...
- linux 文件的权限说明
1.开头 d:表示问文件夹 ( directory ) -:表示普通二进制文件 ( 压缩包.文件 等等 ) l:表示软连接文件 ( link 硬链接或软链接 ) 2.权限 ( 3 个为一组 ) r:读 ...
- Jquery-ajax错误分析
当我把cshtml中的js代码移出到js文件中,将js代码作为文件引入cshtml时,出现了下面的这样的错误 网上的不少人说是通过在\(.ajax参数中加上async:true解决的,但\).ajax ...
- SQL SERVER-identity | @@identity | scope_identity
主键自增 IDENTITY(1,1),MS SQL Server 使用 IDENTITY 关键字来执行 auto-increment 任务. 在上面的实例中,IDENTITY 的开始值是 1,每条新记 ...
- ASP.NET-Razor常用方法
1.使用Scripts.Render()引入脚本 @sectionScrits{ @Scripts.Render("~/bundles/jquery") } 2.使用@Html.H ...
- 推断CPU 是小端存储(Little endian)还是大端存储(Big endian)模式
第一个版本号: //return true in big-endian machines bool check_big_endian1() { int a = 0; int *p = &a; ...
- Linux内核中进程上下文和中断上下文的理解
參考: http://www.embedu.org/Column/Column240.htm http://www.cnblogs.com/Anker/p/3269106.html 首先明白一个概念: ...