Java for LeetCode 121 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.
解题思路:
本题目是《算法导论》 4.1 节 给出的股票问题的原题,可以转化为最大子数组的问题,书本上给出的是分治的做法,练习4.1-5给出了线性时间的算法。
Java for LeetCode 053 Maximum Subarray实现了相应的解法,如果不转化为最大子数组问题,解法如下:
一次遍历,每次找到最小的Buy点即可,JAVA实现如下:
public int maxProfit(int[] prices) {
int buy = 0;
int profit = 0;
for (int i = 0; i < prices.length; ++i) {
if (prices[buy] > prices[i])
buy = i;
profit = Math.max(profit, prices[i] - prices[buy]);
}
return profit;
}
Java for LeetCode 121 Best Time to Buy and Sell Stock的更多相关文章
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 121. 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 ...
- 30. leetcode 121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- leetcode 121. 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 ...
- LeetCode 121. 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 ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 122 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 ...
- Python [Leetcode 121]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 ...
随机推荐
- Android Retrofit使用教程(二)
上一篇文章讲述了Retrofit的简单使用,这次我们学习一下Retrofit的各种HTTP请求. Retrofit基础 在Retrofit中使用注解的方式来区分请求类型.比如@GET("&q ...
- 【spring cloud】子模块启动报错com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect
spring cloud子模块启动报错 Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanic ...
- novell.directory.ldap获取邮箱活动目录
在windows系统上可以使用下列方法来查找所有的员工邮箱和员工组: StringDictionary ReturnArray = new StringDictionary(); Dictionary ...
- 穿透内网防线,USB自动渗透手法总结
USB(Universal Serial Bus)原意是指通用串行总线,是一个外部总线标准,用于规范电脑与外部设备的连接和通讯,这套标准在1994年底由英特尔.康柏.IBM.Microsoft等多家公 ...
- 单选复选框的js代码取值
单选框 复选框选中后的js代码处理 <script type="text/javascript"> function check(){ document.getElem ...
- 关于使用Axure RP进行原型开发的一些心得体会
Axure RP(Axure Rapid Prototyping)是一款高速实现.准确表达.带有交互效果且易于上手的原型设计工具. 本人在曾參与某系统需求分析时開始接触Axure RP,初步掌握了一定 ...
- hdu1198Farm Irrigation(dfs找联通)
题目链接: 啊哈哈,选我选我 思路是:首先依据图像抽象出联通关系.. 首先确定每一种图形的联通关系.用01值表示不连通与不连通... 然后从第1个图形进行dfs搜索.假设碰到两快田地能够联通的话那么标 ...
- UNP学习笔记(第十六章 非阻塞I/O)
套接字的默认状态时阻塞的 可能阻塞的套接字调用可分为以下4类: 1.输入操作,包括read.readv.recv.recvfrom和recvmsg. 2.输入操作,包括write.writev.sen ...
- mongodb:monogo和php整合
1.到如下网址,下载php扩展包,找一个最新stable版的.
- 高阶函数:filter()
Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是 ...