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 ...
随机推荐
- ScheduledExecutorService和timer的异同
先来个传统的Timer的例子: package com.jerry.concurrency; import java.text.ParseException; import java.text.Sim ...
- 直接内存访问(DMA)
1. 什么是DMA 直接内存访问是一种硬件机制,它允许外围设备和主内存之间直接传输它们的I/O数据,而不需要系统处理器的参与.使用这种机制可以大大提高与设备通信的吞吐量. 2. DMA数据传输 有 ...
- 由源代码编译SpriteBuilder最新版本1.5.0搭配最新的Cocos2D 3.4.9
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 大家知道SpriteBuilder版本停留在1.4.9已经很久 ...
- UNIX网络编程——sockatmark函数
每当收到一个带外数据时,就有一个与之关联的带外标记.这是发送进程发送带外字节时该字节在发送端普通数据流中的位置.在从套接字读入期间,接收进程通过调用sockatmark函数确定是否处于带外标记. #i ...
- Android初级教程之内容提供者获取联系人信息
内容提供折详细理论知识请参考之前的博文:http://blog.csdn.net/qq_32059827/article/details/51646513 这里新建了三个联系人信息,通过查看系统联系人 ...
- MyEclipse9安装Checkstyle5.5插件(图解)
①首先下载Eclipse Checkstyle Plug-in 官方首页:http://sourceforge.net/projects/eclipse-cs/files/ 最新版为: ...
- nginx root、alias、location指令使用方法
一.nginx root指令 1. Nginx配置 相关配置如下图: 通过配置root目录到"/wwwroot/html/"位置 在用虚拟主机方法,主机名称是test,需要大家配置 ...
- polaris: 一个用go实现的支持restful的web框架
介绍 polaris是一个用go实现的支持restful的web框架,主要参考tornado进行设计. 虽然在go里面搭建一个http server非常的简单,这里强烈推荐gorilla,但并没有很好 ...
- OpenCV stereo matching BM 算法
一直找不到opencv stereo matching的根据和原理出处,下面这个文章贴了个链接,有时间看看: Basically OpenCV provides 2 methods to calcul ...
- java容易混淆的15个知识点
java知识点不多,但是有一些经常会被我们忽略 1.java是强类型的语言,数组也是对象,一旦确定数组的类型,里面就只能存放一个类型的数据. 2.新建的对象都被存放到堆上,如果没有引用,会很快垃圾回收 ...