Skip to content

Commit dcc0a87

Browse files
committed
add lintcode backpack
1 parent c9e71da commit dcc0a87

File tree

4 files changed

+160
-4
lines changed

4 files changed

+160
-4
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.yiyun;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.IntStream;
8+
9+
/**
10+
* @class: ArrayToMap
11+
* @description: 将两个数组映射成 HashMap
12+
* @author: yi-yun
13+
* @create: 2019-07-18 21:21
14+
**/
15+
public class ArrayToMap {
16+
@Test
17+
public void fun1() {
18+
String[] keys = new String[]{"one", "two", "three"};
19+
Integer[] values = new Integer[]{1, 2, 3};
20+
Map<String, Integer> map = IntStream.range(0, keys.length).boxed().collect(
21+
Collectors.toMap(i -> keys[i], i -> values[i])
22+
);
23+
}
24+
25+
@Test
26+
public void fun2() {
27+
String[] keys = new String[]{"one", "two", "three", "one"};
28+
Integer[] values = new Integer[]{1, 2, 3, 4};
29+
Map<String, Integer> map = IntStream.range(0, keys.length).boxed().collect(
30+
Collectors.toMap(i -> keys[i], i -> values[i])
31+
);
32+
}
33+
}

myoffer/src/main/java/algorithm/dp/_1_ZeroOnePack.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ public void packOptimizationTest(int[] weight, int[] values, int n, int w) {
132132
}
133133
for (int i = 1; i < n; i++) {
134134
for (int j = w - weight[i]; j >= 0; j--) {
135-
int v = states[j] + values[i];
136-
if (v > states[j + weight[i]]) {
137-
states[j + weight[i]] = v;
138-
}
135+
int v = states[j] + values[i];
136+
if (v > states[j + weight[i]]) {
137+
states[j + weight[i]] = v;
138+
}
139139
}
140140
}
141141
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package lintcode;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @class: _125_backpack2
7+
* @description: 背包问题2
8+
* @author: yi-yun
9+
* @create: 2019-07-18 22:14
10+
**/
11+
public class _125_backpack2 {
12+
/**
13+
* @param m: An integer m denotes the size of a backpack
14+
* @param A: Given n items with size A[i]
15+
* @param V: Given n items with value V[i]
16+
* @return: The maximum value
17+
*/
18+
//恰好装满
19+
public int backPackII(int m, int[] A, int[] V) {
20+
// write your code here
21+
int n = A.length;
22+
int[] states = new int[m + 1];
23+
for (int i = 0; i < m + 1; i++) {
24+
states[i] = Integer.MIN_VALUE;
25+
}
26+
states[0] = 0;
27+
if (A[0] <= m) {
28+
states[A[0]] = V[0];
29+
}
30+
for (int i = 1; i < n; i++) {
31+
for (int j = m - A[i]; j >= 0; j--) {
32+
if (states[j] >= 0) {
33+
states[j + A[i]] = Integer.max(states[j] + V[i], states[j + A[i]]);
34+
}
35+
}
36+
}
37+
int res = Integer.MIN_VALUE;
38+
for (int i = m; i >= 0; i--) {
39+
res = Integer.max(res, states[i]);
40+
}
41+
return res;
42+
}
43+
44+
//尽量装满 m 最大重量 A 重量数组 V 价值的数组
45+
public int backPackII2(int m, int[] A, int[] V) {
46+
// write your code here
47+
int n = A.length;
48+
int[] states = new int[m + 1];
49+
for (int i = 0; i < m + 1; i++) {
50+
states[i] = 0;
51+
}
52+
if (A[0] <= m) {
53+
states[A[0]] = V[0];
54+
}
55+
for (int i = 1; i < n; i++) {
56+
for (int j = m - A[i]; j >= 0; j--) {
57+
states[j + A[i]] = Integer.max(states[j] + V[i], states[j + A[i]]);
58+
}
59+
}
60+
return states[m];
61+
}
62+
63+
64+
@Test
65+
public void fun1() {
66+
backPackII(10, new int[]{2, 3, 5, 7}, new int[]{1, 5, 2, 4});
67+
}
68+
69+
@Test
70+
public void fun2() {
71+
backPackII2(200,
72+
new int[]{79, 58, 86, 11, 28, 62, 15, 68},
73+
new int[]{83, 14, 54, 79, 72, 52, 48, 62});
74+
}
75+
76+
@Test
77+
public void fun3() {
78+
backPackII(10,
79+
new int[]{2, 3, 8},
80+
new int[]{2, 5, 8});
81+
}
82+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package lintcode;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @class: _92_backpack
7+
* @description: 01背包,尽量装满
8+
* @author: yi-yun
9+
* @create: 2019-07-18 21:55
10+
**/
11+
public class _92_backpack {
12+
/**
13+
* @param m: An integer m denotes the size of a backpack
14+
* @param A: Given n items with size A[i]
15+
* @return: The maximum size
16+
*/
17+
public int backPack(int m, int[] A) {
18+
// write your code here
19+
int n = A.length;
20+
int[] states = new int[m + 1];
21+
if (A[0] <= m) {
22+
states[A[0]] = 1;
23+
}
24+
for (int i = 0; i < n; i++) {
25+
for (int j = m - A[i]; j >= 0; j--) {
26+
states[A[i] + j] = Math.max(states[A[i] + j], states[j] + A[i]);
27+
}
28+
}
29+
return states[m];
30+
}
31+
32+
@Test
33+
public void fun1() {
34+
backPack(10, new int[]{3, 4, 8, 5});
35+
}
36+
37+
@Test
38+
public void fun2() {
39+
backPack(12, new int[]{2, 3, 5, 7});
40+
}
41+
}

0 commit comments

Comments
 (0)