leetcode 121. 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.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 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 In this case, no transaction is done, i.e. max profit = 0.
求最佳买入卖出时的收益。
暴力法,超时。
public class Solution {
public int maxProfit(int[] prices) {int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
for( int i = 0;i<len;i++){
for( int j = i+1;j<len;j++){
if( prices[j]>prices[i])
result = Math.max(result,prices[j]-prices[i]);
} }
return result;
}
}
所以思考一下这道题的原理,其实就设定一个指针就好了
设定刚开始的数字是买入价格:
1、如果高于买入价格,那么就和result相比,取较大的。
2、如果是低于价格,那么更新买入价格。
public class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
if( len < 2 )
return 0;
int result = 0;
int buy = prices[0];
for( int i = 1;i<len;i++){
if( prices[i] > buy )
{
result = Math.max(result,prices[i]-buy);
}else
buy = prices[i];
} return result; }
}
leetcode 121. Best Time to Buy and Sell Stock ----- java的更多相关文章
- 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- [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 ...
- Java for 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 (买卖股票的最好时机)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Python [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 ...
- [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 最佳股票售卖时(动态规划,数组,模拟)
题目描述 已知一个数组,第i个元素表示第i天股票的价格,你只能进行一次交易(买卖各一次),设计算法找出最大收益 测试样例 Input: [7, 1, 5, 3, 6, 4] Output: 5 最大收 ...
- LeetCode 121. Best Time to Buy and Sell Stock (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
随机推荐
- POJ 1741 树的点分治
题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include & ...
- 凭借K2 SmartObject框架,在SharePoint中集成数据
随着SharePoint 2013的发布,Microsoft已提供Business Connectivity Services(BCS)增强功能以及外部列表功能,确保您可以更简单地在SharePoin ...
- OpenLayers简单介绍以及简单实例
OpenLayers是一个强大的JavaScript包,可以从它的官网免费下载.OpenLayers包含了很多强大的网页地图展示与操作功能,并且能够将不同源的图层展示在同一张地图中,支持各种第三方的地 ...
- Mac运行exe的几种方法,欢迎补充!
1. 用wine直接运行exe.安装wine后有个放exe的文件夹,双击后会自动包装运行.看起来挺方便的,就怕暂用资源比较大: http://www.youtube.com/watch?v=eYISV ...
- JVM-内存分配与回收策略
简单介绍一下Java技术体系下的Java虚拟机内存分配与回收策略. 1.对象优先在Eden分配 大多数情况下,对象在新生代Eden区中分分配.当Eden区已没有足够空间进行分配时,虚拟机将发起一次 ...
- Javascript 基础(一)
一.Js命名规范(变量/函数) (1)使用大小写字母,数字,_ ,$ 可以命名 (2)不能以数字打头 (3)不能使用js的关键字/保留字 (4)区分大小写 (5)单行注释 //多行注释 二.js的数据 ...
- HDU 1114 Piggy-Bank (poj1384)
储钱罐 [题目描述] 今年的ACM比赛在xxx国举行,为了比赛的顺利进行,必须提前预算好需要的费用,以及费用的来源.费用的主要来源是只进不出的金钱.其实原理很简单,每当ACM成员有任何一点小钱,他们就 ...
- 2016年3月AV评测
- hadoop 中对Vlong 和 Vint的压缩方法
hadoop 中对java的基本类型进行了writeable的封装,并且所有这些writeable都是继承自WritableComparable的,都是可比较的:并且,它们都有对应的get() 和 s ...
- golang函数调用计时
package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...