121. Best Time to Buy and Sell Stock

Easy

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 (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

Example 1:

Input: [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 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
Explanation: In this case, no transaction is done, i.e. max profit = 0.
package leetcode.easy;

public class BestTimeToBuyAndSellStock {
@org.junit.Test
public void test() {
int[] prices1 = { 7, 1, 5, 3, 6, 4 };
int[] prices2 = { 7, 6, 4, 3, 1 };
System.out.println(maxProfit1(prices1));
System.out.println(maxProfit1(prices2));
System.out.println(maxProfit2(prices1));
System.out.println(maxProfit2(prices2));
} public int maxProfit1(int[] prices) {
int maxprofit = 0;
for (int i = 0; i < prices.length - 1; i++) {
for (int j = i + 1; j < prices.length; j++) {
int profit = prices[j] - prices[i];
if (profit > maxprofit) {
maxprofit = profit;
}
}
}
return maxprofit;
} public int maxProfit2(int[] prices) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}

LeetCode_121. Best Time to Buy and Sell Stock的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [LintCode] 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 ...

  7. [LintCode] 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 ...

  8. LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)

    问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记

    123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...

随机推荐

  1. 1122 django中orm操作

    目录 1. 静态文件的配置 手动静态文件的访问资源 静态文件的动态绑定 2.request方法 2.1 请求方式 2.2 获取前端的请求方式 request.method 2.3 request方法 ...

  2. MyBatsi学习

    深入浅出Mybatis系列(一)---Mybatis入门 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇) 深入浅出Mybatis系列(三)---配置详解之properties与 ...

  3. Python 如何理解可更改元组中的可变序列

    在 Python 中,元组是不可变序列,那为什么当元组中的元素是可变序列时(如 list.dict)可进行增删的操作? 在定义一个元组时,Python内部会为元组中的每一个元素分配一个内存地址,当我们 ...

  4. DOM设置css样式

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. 55、servlet3.0-ServletContext注册三大组件

    55.servlet3.0-ServletContext注册三大组件 使用ServletContext 注册 Servlet.Filter.Listener 使用编码的方式,在项目启动的时候给 Ser ...

  6. Oracle 中MERGE语句的用法

    原文章出处(http://blog.csdn.net/lichkui/article/details/4306299) MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语 ...

  7. learning scala extractor object

    package com.aura.scala.day01 import scala.util.Random object extractorObject { def main(args: Array[ ...

  8. 关于item的prevvalue

    一个数据库大小监控项的更新周期是86400.一天,使用的最新值减去上次数值来显示变化值,一直就是10g-0 变化值为10g. 在更新时间为一天的时候prevvalue是0,不能显示上次的数值 当吧更新 ...

  9. CF455C Civilization

    嘟嘟嘟 水题一道,某谷又恶意评分. 合并无非是将两棵树的直径的中点连一块,记原来两棵树的直径为\(d_1, d_2\),那么新的树的直径就是\(max(d_1, d_2, \lceil \frac{d ...

  10. (转)SLOW READPROCESSOR;ERROR SLOW BLOCKRECEIVER错误日志分析

    1.总结 "Slow ReadProcessor" 和"Slow BlockReceiver"往往是因为集群负载比较高或者某些节点不健康导致的,本文主要是帮助你 ...