[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. 动态规划的基本思想 动态规划背后的基本思想非常简单.大致上,若要解一个给 ...
随机推荐
- C++设计模式——抽象工厂模式
问题描述 之前讲到了C++设计模式——工厂方法模式,我们可能会想到,后期产品会越来越多了,建立的工厂也会越来越多,工厂进行了增长,工厂变的凌乱而难于管理:由于工厂方法模式创建的对象都是继承于Produ ...
- ASP.NET MVC和jQuery DataTable整合
本文包含代码示例说明如何jQuery插件开发者可以集成到ASP.NET MVC应用程序. 下载源代码- 87.4 KB Introduction The jQuery DataTables plug- ...
- js记录重复字母的个数
var str = "hello"; var obj = {}; for (var i = 0; i < str.length; i++) { var v = str.cha ...
- Postman编程
Postman常用Api Postman像jmeter一样提供前置处理脚本和后置处理脚本.脚本主要使用JavaScript语法,并内置提供了一些js代码库,提供了一些内置对象和方法. 参考:https ...
- wamp的安装--亲测有用
一.修改默认密码进入之后 use mysql;1.update user set password=PASSWORD('自己的数据库密码') where user='root';2.flush pri ...
- Spark:一个独立应用
[TOC] Spark:一个独立应用 关于构建 Java和Scala 在Java和Scala中,只需要给你的应用添加一个对于spark-core的Maven依赖. Python 在Python中,可以 ...
- localStorage , sessionStorage ,cookie 使用介绍
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IO的构造方法
package com.file; /* File的构造方法: File(String pathname); File(File parent,String child); File(String p ...
- iterator的实现原理
1.iterator遍历linkedlist集合 Iterator li = list.iterator(); while(li.hasNext()){ System.out.println(li.n ...
- webapi 发布接口报405错误(angularjs2.0)
参考链接:http://www.cnblogs.com/shenbin/p/5680976.html web访问接口报405错误,以前的jQuery访问方式访问接口没有问题. 但是换成angularj ...