LeetCode(42)-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.
思路:
- 题意给定一个数组prices,每一个值prices[i]是第i天的股价,要求只能买入和卖出一次,求出最大的获益
- 首先买入一定在卖出之前,所以要同步更新最小股价,然后最大利润,用一个变量代替最小的股价,一个值来代替最大利润,注意最大利润是负数的情况
-
代码:
public class Solution {
public int maxProfit(int[] prices) {
if(prices.length <= 1){
return 0;
}
int minPrice = prices[0];
int maxProfit = prices[1] - prices[0];
for(int i = 2; i < prices.length;i++){
minPrice = Math.min(minPrice,prices[i-1]);
maxProfit = Math.max(maxProfit,prices[i]-minPrice);
}
if(maxProfit < 0){
return 0;
}else{
return maxProfit;
}
}
}
LeetCode(42)-Best Time to Buy and Sell Stock(卖股票)的更多相关文章
- [LeetCode] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
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 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 ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 309. 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
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
随机推荐
- 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态
一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...
- 【一天一道LeetCode】#102. Binary Tree Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- Dynamics CRM2013 ScLib::AccessCheckEx failed
今天在系统中做某一操作的时候报如下截图错误,把错误日志下载下来,根据AccessRights这:ReadAccess一提示确定是对某一实体没有读的权限. 那怎样知道是哪个实体呢,再看上面错误日志中给出 ...
- Java 8新特性探究(四)深入解析日期和时间-JSR310
众所周知,日期是商业逻辑计算一个关键的部分,任何企业应用程序都需要处理时间问题.应用程序需要知道当前的时间点和下一个时间点,有时它们还必须计算这两个时间点之间的路径.但java之前的日期做法太令人恶心 ...
- Jquery之Bind方法参数传递与接收的三种方法
方法一. function GetCode(event) { alert(event.data.foo); } $(document).ready(function() { $("#s ...
- android自定义组件的简易实现
写这篇博客是为了复习之前在慕课上面有幸看到的自定义组件的实现,原理很简单,有三个步骤, 为自定义的组件做好声明:封装成具体的可以使用的组件类,并利用接口回调机制为其注册监听函数:想使用正常的组件的方式 ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
- 【一天一道LeetCode】#51. N-Queens
一天一道LeetCode系列 (一)题目 The n-queens puzzle is the problem of placing n queens on an n×n chessboard suc ...
- 教你自己写Android第三方库
其实Android studio的出现很大程度上方便了我们Android开发人员,今天我们说说怎么构建我们自己的库. 依次按File->New Moudle->android Librar ...
- saiku中文维度,补充说明
saiku在筛选中文维度 会出现浏览器白屏 停止响应的现象,经过跟踪源代码,分析原来在linux 操作系统中 数据库读取的中文和界面选取的编码是不一致的 解决方法, classes\saiku-dat ...