121. Best Time to Buy and Sell Stock——Leetcode
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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Note that you cannot sell a stock before you buy one.
Example 1:
Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Not 7-1 = 6, as selling price needs to be larger than buying price.
Example 2:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
题目大意:给定一组股票的价格,买卖一次,求最大的收益。
思路:扫一遍,记录最小值,当前价格减去最小值就是当前可获得的最大收益,从这些可能的收益值中取最大的。
- public int solution(int[] arr) {
- if(arr == null || arr.length == 0) {
- return 0;
- }
- int[] dp = new int[arr.length];
- int min = arr[0];
- for(int i = 1;i<arr.length;i++) {
- dp[i] = Math.max(dp[i-1], arr[i] - min);
- if (arr[i] < min) {
- min = arr[i];
- }
- }
- return dp[arr.length - 1];
- }
121. Best Time to Buy and Sell Stock——Leetcode的更多相关文章
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 121. Best Time to Buy and Sell Stock【easy】
121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...
- Best Time to Buy and Sell Stock - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Best Time to Buy and Sell Stock - LeetCode 注意点 在卖出之前必须要先购入 解法 解法一:遍历一遍,随时记录当前 ...
- 121. Best Time to Buy and Sell Stock@python
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 ...
- 【刷题-LeetCode】121 Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 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 ...
随机推荐
- 前端技巧-w3c
1.使用全等===比较符 if (zeroAsAString === 0) { // 判断为false }在和null进行比较的时候,允许使用 == 比较符 2.使用 .parseInt() 的时候, ...
- python迭代器、生成器、列表推倒式
创建迭代器: iter( ): 创建迭代器 next( ): 返回迭代器的下一个element(元素) 实例题: >>> list = [1,2,3,4] >>> ...
- listview适配器中的控件的点击事件并传值
@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto ...
- CI模板中php脚本的使用
今天偶然发现,在CI的模板中能够直接使用CI自带的函数,并且可以直接调用controller里面的属性.案例: 控制器: public function test(){ $this->a = ' ...
- siteserver学习笔记
1.安装 安装前的准备工作 参考https://docs.siteserver.cn/getting-started/#/how-to-install-siteserver-cms官网的文档写的很详细 ...
- VUE中给template组件加背景
<template> <div class="index_background" > </div> </template> < ...
- 使用Advanced Installer进行二次打包
使用Advanced Installer进行二次打包 在上一篇使用InstallerShield打包VS程序中,我已经叙述过,为什么要进行二次打包的问题,在此我就不再赘述.本次长枪直入,说一说如何使用 ...
- 微信公众号自动回复_Java
先声明一下,这是一个maven工程pom文件需要的依赖: <dependency> <groupId>dom4j</groupId> <artifactId& ...
- 基于springmvc开发注解式ip拦截器
一.注解类 @Documented @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) ...
- Django auth组件拓展 关联外部信息---------------------------- Profile 模式
https://docs.djangoproject.com/en/2.1/topics/auth/customizing/ 官方文档. 网上的get_profile 方法不好用太假了 可能我没用明白 ...