Array
DP
Description:
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.
先说明题目意思,刚开始我看了很久也没搞明白,去Discuss上才看明白了。就是说,Input
数组表示这段时间每天的货物价格,你以某个价格买入,之后再以那天的价格卖出,获取利益。例如:
Input: [7,1,5,3,6,4]
在价格为1时买入,价格为6时卖出,可获利润为5
Input: [7,6,4,3,1]
一直再降价,所以不买该货物,利润为0
Input: [2,4,1]
第一天买,第二天卖,可获利为2
Input: [3,2,6,5,0,3]
第二天买,第三天卖出,获利为4,比价格为0时买入,价格为3时卖出获利更多
my Solution:
public class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int profit = 0;
if (n == 0) {
return profit;
}
else {
int min = prices[0];
int max = prices[0];
for (int i = 1; i < n; i++) {
if (min >= prices[i]) {
min = prices[i];
max = min;
}
if (max <= prices[i]) {
max = prices[i];
}
profit = Math.max(profit, max-min);
}
return profit;
}
}
}
我的方法非常常规,这题最好的方法就是用动态规划的思想来解。
Best Solution:
public class Solution {
public int maxProfit(int[] prices) {
int maxCur = 0;
int maxSoFar = 0;
for (int i = 1; i < prices.length; i++) {
maxCur = Math.max(0, maxCur + prices[i] - prices[i-1]);
maxSoFar = Math.max(maxCur, maxSoFar);
}
return maxSoFar;
}
}
我对动态规划的理解还不够深刻,还需要多学习,多做这方面的题,加油!
LeetCode & Q121-Best Time to Buy and Sell Stock-Easy的更多相关文章
-
Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
-
[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] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 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] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 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] 309. 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
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
-
[Leetcode Week6]Best Time to Buy and Sell Stock
Best Time to Buy and Sell Stock 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/best-time-to-buy-and ...
-
[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
-
【leetcode】Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
随机推荐
-
java基础 泛型
泛型的存在,是为了使用不确定的类型. 为什么有泛型? 1. 为了提高安全 2. 提高代码的重用率 (自动 装箱,拆箱功能) 一切好处看代码: package test1; import java.la ...
-
淘宝(阿里百川)手机客户端开发日记第一篇 android 主框架搭建(一)
android 主框架搭建(一) 1.开发环境:Android Studio 相继点击下一步,直接项目建立完毕(如下图) 图片看的效果如果很小,请放大您的浏览器显示百分比 转载请注明http://w ...
-
boost构造,解析json
void asynDBCenter::isGetActorInfoEx(void* on_process, const char* arg) { std::stringstream ros(arg); ...
-
Android---3种方式限制EditView输入字数(转载)
方法一:利用TextWatcher editText.addTextChangedListener(new TextWatcher() { private CharSequence temp; pri ...
-
duilib入门之贴图描述、类html文本描述、动态换肤、Dll插件、资源打包
转载自duilib入门文档 贴图描述: Duilib的表现力丰富很大程度上得益于贴图描述的简单强大.Duilib的贴图描述分为简单模式和复杂模式两种. 简单模式使用文件名做为贴图描述内容,在这种方式下 ...
-
Linux学习笔记之Linux添加/删除用户和用户组
本文总结了Linux添加或者删除用户和用户组时常用的一些命令和参数. 1.建用户: adduser phpq //新建phpq用户 passwd phpq //给phpq用户设置密码 2.建工作组 g ...
-
nginx 支持的命令行参数
Command-line parameters 命令行参数 nginx supports the following command-line parameters: nginx支持以下命令行参数 - ...
-
linux后台执行命令:&;和nohup
当我们在终端或控制台工作时,可能不希望由于运行一个作业而占住了屏幕,因为可能还有更重要的事情要做,比如阅读电子邮件.对于密集访问磁盘的进程,我们更希望它能够在每天的非负荷高峰时间段运行(例如凌晨).为 ...
-
angular组件之间的通讯
组件通讯,意在不同的指令和组件之间共享信息.如何在两个多个组件之间共享信息呢. 最近在项目上,组件跟组件之间可能是父子关系,兄弟关系,爷孙关系都有.....我也找找了很多关于组件之间通讯的方法,不同的 ...
-
java ASM动态生成类
BeanTest2.java import java.io.FileOutputStream; import org.objectweb.asm.AnnotationVisitor; import o ...