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 ...
随机推荐
- 8:30+1.5小时,返回时间格式的 php函数
一个实用的自定义函数 /** * 传入8:30格式的开始时间,和小数形式的小时长度,返回结束时间 * @param [type] $start [description] 8:30 * @param ...
- Git学习总结(8)——Git和SVN之间的基本区别
GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等.如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征.所以,这篇文章的主要目的就是 ...
- HDOJ 1753 大明A+B
JAVA大数.... 大明A+B Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- easyui datagrid 动态加入、移除editor
使用easyui 行编辑的时候完毕编辑的功能比較简单,可是假设要依据一个框的值动态改变别的值或者编辑的时候禁用某个框的时候就比較麻烦了. 比方像以下这样:加入行的时候每一个值都是手动输入,改动的时候第 ...
- U盘无法格式化的恢复
昨天装Ubuntu的系统可能把U盘搞崩溃了.然后今早起来U盘无法识别,格式化也不行,用Windows的磁盘管理工具格式化说是:Windows无法格式化U盘. 曾经没遇到这样的情况,所以百度了一下,试了 ...
- swift学习第四章
// Playground - noun: a place where people can play import UIKit var age=100 //假设年龄不大于50的就会在这里断言,和之前 ...
- Ubuntu16.04下Mongodb官网卸载部署步骤(图文详解)(博主推荐)
不多说,直接上干货! 前期博客 Ubuntu16.04下Mongodb官网安装部署步骤(图文详解)(博主推荐) https://docs.mongodb.com/manual/tutorial/ins ...
- IDEA模板设置
/** * @className: $CLASSNAME$ * @author: liuyachao * @date: $DATE$ $TIME$ */ ================= ...
- Servlet基础(一)
JavaEE:企业级开发技术 <一.基础概念>j2ee:jdk1.1--1.4 ----->> j2ee1.1 1.2 javaee:jdk--5,6,7 ...
- oracle11g-rac安装部署
网上11g rac安装文档大多不详细,今天来一个详细的! 纯操作文档,不喜勿喷!! 环境: 系统:rhel5.5 oracle:11.2.03 双网卡 共享磁盘 ip地址和主机名规划: #节点1 主机 ...