LeetCode——Best Time to Buy and Sell Stock IV
Description:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
动态规划,有递推式:参考:http://blog.csdn.net/feliciafay/article/details/45128771
local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),
global[i][j]=max(local[i][j],global[i-1][j])
public class Solution {
public int maxProfit(int k, int[] prices) {
int len = prices.length;
if(len < 2) {
return 0;
}
if(k > len) {
return bigProfit(prices);
}
int[] local = new int[k+1];
int[] global = new int[k+1];
Arrays.fill(local, 0);
Arrays.fill(global, 0);
for(int i=0; i<len-1; i++) {
int diff = prices[i+1] - prices[i];
for(int j=k; j>=1; j--) {
local[j] = Math.max(global[j-1]+(diff>0?diff:0), local[j]+diff);
global[j] = Math.max(global[j], local[j]);
}
}
return global[k];
}
public int bigProfit(int[] prices) {
int res = 0;
for(int i=0; i<prices.length-1; i++) {
if(prices[i] < prices[i+1]) {
res += prices[i+1] - prices[i];
}
}
return res;
}
}
LeetCode——Best Time to Buy and Sell Stock IV的更多相关文章
- [LeetCode] Best Time to Buy and Sell Stock 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 Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
- 【dp】leetcode Best Time to Buy and Sell Stock IV
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/description/ [题意] 给定n天股市的票价,最多交易k次, ...
- 【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] 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 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] Best Time to Buy and Sell Stock 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] 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 第188题,我的解法,Best Time to Buy and Sell Stock IV
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...
随机推荐
- init进程接管孤儿进程的验证
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include<stdlib ...
- sqlserver日期函数<转>
一.sql server日期时间函数Sql Server中的日期与时间函数 1. 当前系统日期.时间 select getdate() 2. dateadd 在向指定日期加上一段时间的基 ...
- iOS边练边学--UITableViewCell的常见属性设置
// 取消选中的样式(常用) 让当前 cell 按下无反应 cell.selectionStyle = UITableViewCellSelectionStyleNone; // 设置选中的背景色,U ...
- java-上传文件与现实上传文件
项目结构: 项目展示: 数据库: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - fileupload ********* ...
- Rattle:数据挖掘的界面化操作
R语言是一个自由.免费.源代码开放的软件,它是一个用于统计计算和统计制图的优秀工具.这里的统计计算可以是数据分析.建模或是数据挖掘等,通过无数大牛提供的软件包,可以帮我们轻松实现算法的实施. 一些读者 ...
- Oracle中的job的定时任务
Oracle job有定时执行的功能,可以在指定的时间点或每天的某个时间点自行执行任务. 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; selec ...
- selenium测试(Java)--浏览器控制(四)
1. 控制浏览器窗口大小 1 package com.test.window; 2 3 import org.openqa.selenium.Dimension; 4 import org.openq ...
- struts2中struts.xml和web.xml文件解析及工作原理
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...
- 初涉RxAndroid结合Glide实现多图片载入操作
转载请注明出处:王亟亟的大牛之路 本来周末就想发了然后各种拖拉就没有然后了,那么就今天早上写吧,废话不多開始正题 什么是RxJava或者RxAndroid我就不多废话了,理论知识一大堆人给我们做好了. ...
- CentOS 6.5 下Vim 配置图解
分享个CentOS 6.5 下Vim 配置图文详解,希望对大家有所帮助. 1. 登录并进入你常用的用户名下,查看其主目录 命令: # su xxx $ cd xxx $ ls -a 2.查看并建立目录 ...