[LeetCode] Dp
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的更多相关文章
- 【leetcode dp】629. K Inverse Pairs Array
https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...
- 【leetcode dp】132. Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...
- 【leetcode dp】Dungeon Game
https://leetcode.com/problems/dungeon-game/description/ [题意] 给定m*n的地牢,王子初始位置在左上角,公主在右下角不动,王子要去救公主,每步 ...
- [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 ...
- [leetcode DP]72. Edit Distance
计算最少用多少不把word1变为word2, 思路:建立一个dp表,行为word1的长度,宽为word2的长度 1.边界条件,dp[i][0] = i,dp[0][j]=j 2.最优子问题,考虑已经知 ...
- [leetcode DP]91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [leetcode DP]64. Minimum Path Sum
一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...
- [leetcode DP]63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode dp专题
1. 动态规划的适用场景 动态规划常常适用于有重叠子问题和最优子结构性质的问题,动态规划方法所耗时间往往远少于朴素解法. 2. 动态规划的基本思想 动态规划背后的基本思想非常简单.大致上,若要解一个给 ...
随机推荐
- HDU-1754-I Hate It(线段树,简单,不过好像有点问题)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目不难,不过开始我犯了一个低级错误,输入n,m,m代表操作的数目,我没有写了,写代码的时候,就 ...
- AIX系统开启ftp服务
http://blog.itpub.net/28227905/viewspace-1060183/ 当然,首先网络工程师给你放开策略,开通ftp端口之后,你才能做下一步. [@more@] AIX开启 ...
- C实现类、继承、多态
首先考虑用C实现类 肯定是要使用struct的,类的数据成员放在struct里面: 而类有构造函数.析构函数,这两个函数必须在struct外面,构造函数要分配struct空间并初始化struct成员, ...
- xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance
http://blog.sina.com.cn/s/blog_4b6f8d150100nx3e.html http://blog.csdn.net/iaiti/article/details/4226 ...
- 基于basys2驱动LCDQC12864B的verilog设计图片显示
话不多说先上图 前言 在做这个实验的时候在网上找了许多资料,都是关于使用单片机驱动LCD显示,确实用单片机驱动是要简单不少,记得在FPGA学习交流群里问问题的时候,被前辈指教,说给我最好的指教便是别在 ...
- MATLAB绘制等高线和梯度场
clear;clc;close all [X,Y] = meshgrid(-:.:); % 产生网格数据X和Y Z = X.*exp(-X.^ - Y.^); % 计算网格点处曲面上的Z值 [DX,D ...
- 微端启动器LAUNCHER的制作之MFC版二(下载)
用了C#再回来用C++写真的有一种我已经不属于这个世界的感觉.C++的下载就没有C#那么方便了,我用的是libcurl.dll,官网上下载的源码自己cmake出来编译的,c++的库引用有debug和r ...
- 将图片保存成png 或者jpg格式
-(void)saveImage:(UIImage*)image{ NSString *pngPath = [NSHomeDirectory() stringByAppendingPathCo ...
- linux上安装Oracle 11g R2 标准版 64位
一.Oracle 安装前的准备 检查一下包,必须全部安装: binutils-2.20.51.0.2-5.43.el6.x86_64 compat-libstdc++-296-2.96-144.el6 ...
- [Linux] - Linux下安装jdk,tar方式
下载jdk的linux下版本,下载页面http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.ht ...