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

exp:

[2,1,4] output:3

[2,1,4,1,7] output:6

典型的dp题目.上代码了

public class Solution {
public int maxProfit(int[] prices) {
if(prices.length<2) return 0;
int n = prices.length;
int[] ret = new int[n];
ret[0] = 0;
int buy = prices[0];
int max = 0;
for(int i=1;i<n;i++){
if(prices[i]<=buy){
buy = prices[i];
ret[i] = ret[i-1];
}
else {
if(prices[i]-buy > max){
ret[i] = prices[i] - buy;
max = ret[i];
}else{
ret[i]=ret[i-1];
}
}
}
return ret[n-1];
}
}

[LeetCode] Dp的更多相关文章

  1. 【leetcode dp】629. K Inverse Pairs Array

    https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...

  2. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  3. 【leetcode dp】Dungeon Game

    https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...

  4. [Leetcode] DP -- Target Sum

    You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symb ...

  5. [leetcode DP]72. Edit Distance

    计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...

  6. [leetcode DP]91. Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. [leetcode DP]64. Minimum Path Sum

    一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...

  8. [leetcode DP]63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LeetCode dp专题

    1. 动态规划的适用场景 动态规划常常适用于有重叠子问题和最优子结构性质的问题,动态规划方法所耗时间往往远少于朴素解法. 2. 动态规划的基本思想 动态规划背后的基本思想非常简单.大致上,若要解一个给 ...

随机推荐

  1. js框架Modernizr是什么东西? 他是前端开发HTML5和CSS3的强有力前端js检测类库

    最近在研究modernizr的前端框架,发现这个Modernir对前端写页面非常友好,并且能够很快的建立起适应任何设备的html页面哦.在这里分享下基础教程,让大伙对modernizr是什么?做什么用 ...

  2. spring 里面的StringUtils,先放这儿,有时间研究吧

    /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Vers ...

  3. The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

    skip-grant-tables下 GRANT ALL PRIVILEGES ON *.* TO helei IDENTIFIED BY 'MANAGER' WITH GRANT OPTION; 执 ...

  4. 集群环境下JSP中获取客户端IP地址的方法

    String ip = request.getHeader("X-Forwarded-For");if (ip == null || ip.length() == 0 || &qu ...

  5. HDU2063(二分图最大匹配)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 2017qq红包雨最强攻略,

    这个只支持苹果手机,而且要有苹果电脑,只有苹果手机是不行的. QQ红包规则:只要你到达指定的位置,就可以领取附近的红包,一般也就几毛,还有几分的,当然也不排除有更高的,只不过我是没遇到... 那么既然 ...

  7. yii2 中布局文件的 设置方法

    网页主题应用的属性: [yii\base\Application::layout|layout 该属性指定渲染 视图 默认使用的布局名字,默认值为 'main' 对应布局路径下的 main.php 文 ...

  8. seq语句随笔

    1.UNION在进行表链接后会筛选掉重复的记录,所以在表链接后会对所产生的结果集进行排序运算,删除重复的记录再返回结果. 2.UNION ALL只是简单的将两个结果合并后就返回.这样,如果返回的两个结 ...

  9. bzoj3238--后缀自动机

    显然只需求LCP(i,j)就可以了. 将s反转,然后插入后缀自动机.由于后缀自动机的link指针构成了一棵后缀树,而字符串又反转过,所以两个结点的LCP就是LCA. 树形DP,求出以每个结点为LCA的 ...

  10. 使用vlmcsd自建KMS服务~一句命令激活windows/office

    服务作用:在线激活windows和office 适用对象:VOL版本的windows和office 适用版本:截止到win10和office2016的所有版本 服务时间:24H,偶尔更新维护 优点:在 ...