[抄题]:

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.

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)

[一句话思路]:贪心算法。不要用数组i,j角标,直接用min, profit表示价格,更方便。

[画图]:

[一刷]:

min, profit都记得要初始化

[总结]:

[复杂度]:

n/1

[英文数据结构]:

Range Queries

[其他解法]:

[题目变变变]:

maximum subarray最大求和子数组

class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0 || prices == null) {
return 0;
} int min = Integer.MAX_VALUE;
int profit = 0;
for (int i = 0; i < prices.length; i++) {
min = min < prices[i] ? min : prices[i];
profit = (prices[i] - min) > profit ? prices[i] - min : profit;
} return profit;
}
}

[抄题]:

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).可以买卖无数次,但是必须先卖后买。

[一句话思路]:

股票profit或者数组sum只有一个时,用贪心算法,容易定义。

股票profit是很多利润的累加时,用数组i,j角标来叠加。因为只要上涨就要卖出,攫取利润。

[画图]:

[一刷]:

由于出现了prices[i + 1], 循环体的上界要减1,为for (int i = 0; i < prices.length - 1; i++)

[总结]:

[复杂度]:

[英文数据结构]:

[其他解法]:

[题目变变变]:

class Solution {
public int maxProfit(int[] prices) {
if (prices.length == 0 || prices == null) {
return 0;
} int diff = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] - prices[i] > 0) {
diff += prices[i + 1] - prices[i];
}
} return diff;
}
}

121. Best Time to Buy and Sell Stock买卖股票12的更多相关文章

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

  2. 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机

    网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...

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

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

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

  6. 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)

    You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...

  7. [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机

    Say you have an array for which the i th element is the price of a given stock on day i. If you were ...

  8. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

  9. 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 ...

随机推荐

  1. dubbo无法创建线程问题

    OutOfMemoryError: unable to create new native thread 决定当前用户程序能够创建多少线程由2个因素决定 1. 用户环境允许的线程数 cat /etc/ ...

  2. 1033 To Fill or Not to Fill (25 分)

    1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any other ...

  3. FiddlerCoreAPI 使用简介

    原文:https://blog.csdn.net/zhang116868/article/details/49406599 大名鼎鼎的Fiddler大家都知道,或者用过,Fiddler 开放了他的Fi ...

  4. 管理oracle 11g RAC 常用命令

    1).检查集群状态: [grid@rac02 ~]$ crsctl check cluster CRS-4537: Cluster Ready Services is online CRS-4529: ...

  5. Python之模块(二)

    1.subprocess模块 2.loggin模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日 ...

  6. Survival Coxph log-rank

    Difference between survdiff log-rank and coxph log-rank Ask Question 6 1 I'm using the survival pack ...

  7. 2018-2019-2 《网络对抗技术》Exp4 恶意代码分析 Week6 20165233

    Exp4 恶意代码分析 实验内容 一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. 使用wi ...

  8. UVA375

    题意: 已知等腰三角形的高H,底边长B,这时有一个内切圆C, 以内切圆C和长度为B对应的角的角平分线的交点做切线. 切线与角平分线相交,此时切线,和俩边又会出现一个小的等腰三角形,也有一个小的内切圆C ...

  9. OpenACC 书上的范例代码(Jacobi 迭代),part 2

    ▶ 使用Jacobi 迭代求泊松方程的数值解 ● 首次使用 OpenACC 进行加速,使用动态数组,去掉了误差控制 #include <stdio.h> #include <stdl ...

  10. Node NPM 的常用配置

    1,修改 npm 下载模块的 保存地址 <1>  进入 cmd 运行, 如下命令 npm config set prefix  "C:\Program File\NodeJs\p ...