0%

###123题解(k=2):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


/**
* @author xixing
* @version 1.0
* @date 2020/2/19 11:30
*/
public class N123 {
public static int maxProfit(int[] prices) {

int [][][] dp=new int[prices.length][3][2];
dp[0][2][0]=0;
dp[0][2][1]=-prices[0];
dp[0][1][1]=-prices[0];
dp[0][1][0]=0;
for(int i=1;i<prices.length;i++){
dp[i][2][0]=Math.max(dp[i-1][2][0],dp[i-1][2][1]+prices[i]);
dp[i][2][1]=Math.max(dp[i-1][2][1],dp[i-1][1][0]-prices[i]);
dp[i][1][0]=Math.max(dp[i-1][1][0],dp[i-1][1][1]+prices[i]);
dp[i][1][1]=Math.max(dp[i-1][1][1],-prices[i]);

}
return dp[prices.length-1][2][0];


}

大致思路

首先

dp[3][2][1]为第三天,最多可以购买两次,目前已经购买了彩票

建立动态规划表达式

dp[i][k][0]=max(dp[i-1][k][0],dp[i-1][k][1]+prices[i])

dp[i][k][1]=max(dp[i-1][k][1],dp[i-1][k-1][0]-prices[i])

初始化数组

dp[0][1][1]=-prices[0] dp[0][1][0]=0 dp[0][2][1]=-prices[0] dp[0][2][0]=0

解释

第零天,最多有一次购买机会,我买了的情况,第零天,最多有一次购买机会,我没买

188题解1(超出内存限制):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static int maxProfit(int k, int[] prices) {

if(prices.length==0){
return 0;
}

int dp[][][]=new int[prices.length][k+1][2];

for(int i=0;i<prices.length;i++){
for(int k1=k;k1>=1;k1--){
if(i==0){
dp[0][k1][0]=0;
dp[0][k1][1] = -prices[i];
continue;
}
dp[i][k1][0]=Math.max(dp[i-1][k1][0],dp[i-1][k1][1]+prices[i]);
dp[i][k1][1]=Math.max(dp[i-1][k1][1],dp[i-1][k1-1][0]-prices[i]);
}

}

return dp[prices.length-1][k][0];


}

188题解2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static int maxProfit(int k, int[] prices) {
/**
* java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
* at line 7, Solution.maxProfit
* at line 57, __DriverSolution__.__helper__
* at line 87, __Driver__.main
*/
if(prices.length==0){
return 0;
}
//当k>一半的时候,相当于可以每隔一天进行交易,直接调用122题的贪心算法即可

if(k>prices.length/2){
return maxProfit122(prices);
}

int dp[][][]=new int[prices.length][k+1][2];

for(int i=0;i<prices.length;i++){
for(int k1=k;k1>=1;k1--){
if(i==0){
dp[0][k1][0]=0;
dp[0][k1][1] = -prices[i];
continue;
}
dp[i][k1][0]=Math.max(dp[i-1][k1][0],dp[i-1][k1][1]+prices[i]);
dp[i][k1][1]=Math.max(dp[i-1][k1][1],dp[i-1][k1-1][0]-prices[i]);
}

}

return dp[prices.length-1][k][0];


}
public static int maxProfit122(int[] prices) {
if (prices.length==0){
return 0;
}
int temp=prices[0];
int profile=0;
for(int i=1;i<prices.length;i++){

if(prices[i]>temp){
profile=profile+prices[i]-temp;
temp=prices[i];
}else {
temp=prices[i];
}

}
return profile;
}

买卖股票最好时机

买卖股票最好时机2(122题)

买卖股票最好时机3(123题)

买卖股票最好时机4(188题)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<mirror>
<id>aliyun-public</id>
<mirrorOf>*</mirrorOf>
<name>aliyun public</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>

<mirror>
<id>aliyun-central</id>
<mirrorOf>*</mirrorOf>
<name>aliyun central</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>

<mirror>
<id>aliyun-spring</id>
<mirrorOf>*</mirrorOf>
<name>aliyun spring</name>
<url>https://maven.aliyun.com/repository/spring</url>
</mirror>

<mirror>
<id>aliyun-spring-plugin</id>
<mirrorOf>*</mirrorOf>
<name>aliyun spring-plugin</name>
<url>https://maven.aliyun.com/repository/spring-plugin</url>
</mirror>

<mirror>
<id>aliyun-apache-snapshots</id>
<mirrorOf>*</mirrorOf>
<name>aliyun apache-snapshots</name>
<url>https://maven.aliyun.com/repository/apache-snapshots</url>
</mirror>

<mirror>
<id>aliyun-google</id>
<mirrorOf>*</mirrorOf>
<name>aliyun google</name>
<url>https://maven.aliyun.com/repository/google</url>
</mirror>

<mirror>
<id>aliyun-gradle-plugin</id>
<mirrorOf>*</mirrorOf>
<name>aliyun gradle-plugin</name>
<url>https://maven.aliyun.com/repository/gradle-plugin</url>
</mirror>

<mirror>
<id>aliyun-jcenter</id>
<mirrorOf>*</mirrorOf>
<name>aliyun jcenter</name>
<url>https://maven.aliyun.com/repository/jcenter</url>
</mirror>

<mirror>
<id>aliyun-releases</id>
<mirrorOf>*</mirrorOf>
<name>aliyun releases</name>
<url>https://maven.aliyun.com/repository/releases</url>
</mirror>

<mirror>
<id>aliyun-snapshots</id>
<mirrorOf>*</mirrorOf>
<name>aliyun snapshots</name>
<url>https://maven.aliyun.com/repository/snapshots</url>
</mirror>

<mirror>
<id>aliyun-grails-core</id>
<mirrorOf>*</mirrorOf>
<name>aliyun grails-core</name>
<url>https://maven.aliyun.com/repository/grails-core</url>
</mirror>

<mirror>
<id>aliyun-mapr-public</id>
<mirrorOf>*</mirrorOf>
<name>aliyun mapr-public</name>
<url>https://maven.aliyun.com/repository/mapr-public</url>
</mirror>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class N220 {
public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
TreeSet<Integer> set=new TreeSet<Integer>();
for(int i=0;i<nums.length;i++){
//找出树里面比nums[i]大的最小数
Integer s=set.ceiling(nums[i]);
if(s!=null&&s-t<=nums[i]){
//测试用例有nums[i]与Integer.MAX_VALUE相等的,如果不判断会溢出
// s-nums[i]<=t会溢出所以我们改成s-t<=nums[i]
return true;

}
//找出树里面比nums[i]小的最大数
s=set.floor(nums[i]);
if(s!=null&&nums[i]<=t+s){
return true;


}
set.add(nums[i]);
if(set.size()>k){
set.remove(nums[i-k]);
}

}

return false;

}

public static void main(String[] args) {
int[] nums={-1,2147483647};

System.out.println(containsNearbyAlmostDuplicate(nums,3,2147483647));
}

}

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment