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 ...
随机推荐
- [luogu4133 BJOI2012] 最多的方案 (计数dp)
题目描述 第二关和很出名的斐波那契数列有关,地球上的OIer都知道:F1=1, F2=2, Fi = Fi-1 + Fi-2,每一项都可以称为斐波那契数.现在给一个正整数N,它可以写成一些斐波那契数的 ...
- mkl安装与使用
mkl安装教程 1.下载 首先到该网站下载压缩包,需要注册 2.安装 首先解压缩 tar -zxvf l_mkl_2019.0.117.tgz 进入目录进行安装 cd l_mkl_2019.0.117 ...
- JavaScript 的对象继承方式,有几种写法?
JavaScript 的对象继承方式,有几种写法? 一.对象冒充 其原理如下:构造函数使用 this 关键字给所有属性和方法赋值(即采用类声明的构造函数方式).因为构造函数只是一个函数,所以可使 Pa ...
- 数字签名技术与https
1,非对称加密技术 非对称加密算法需要两个密钥,公开密钥(publickey)和私有密钥(privatekey):公钥和私钥是成对出现的. 非对称加密例子:B想把一段信息传给A,步骤:1)A把公钥传给 ...
- Linux下MySql数据库常用操作
1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...
- node-webkit 主页面和 iframe 页通讯
<html lang="en-US"> <head> <title>Hello World!</title> <style&g ...
- JAVA设计模式之【状态模式】
状态模式 水.固态.气态.液态 账户.正常状态.透支状态.受限状态 状态模式中,用一个状态类来分散冗长的条件语句,让系统有灵活性和可扩展性 状态模式用于解决系统中复杂对象的状态转换以及不同状态下行为的 ...
- Pandas与Matplotlib
Pandas与Matplotlib基础 pandas是Python中开源的,高性能的用于数据分析的库.其中包含了很多可用的数据结构及功能,各种结构支持相互转换,并且支持读取.保存数据.结合matplo ...
- CxImage的使用及基本用法
基本定义:CxImage类库是一个优秀的图像操作类库.它可以快捷地存取.显示.转换各种图像. 下载地址:http://www.codeproject.com/KB/graphics/cximage.a ...
- 重温前端基础之-js排序算法
javascript数组常用的方法: push():返回值,数组新的长度 pop():返回值,被删除的数组末尾元素 shift():返回值,被删除的数组首部元素 unshift():返回值,数组新的长 ...