-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVMware.java
More file actions
51 lines (46 loc) · 1.61 KB
/
VMware.java
File metadata and controls
51 lines (46 loc) · 1.61 KB
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
public class VMware {
public long subarraySum(int[] arr) {
long result = 0;
for (int i = 0; i < arr.length; i++) {
int temp = 0;
for (int j = 0; j < arr.length - i; j++) {
temp += arr[i + j];
result += temp;
}
}
return result;
}
public long subarraySum2(int[] arr) {
long result = 0;
for (int i = 0; i < arr.length; i++) {
result += (arr.length - i ) * (i + 1) * arr[i];
}
return result;
}
public int buddleShop(int[] numNotebook, int[] buddleCost, int budget) {
int numShop = numNotebook.length;
int[] dp = new int[budget + 1];
for (int i = 0; i < numShop; i++) {
for (int j = buddleCost[i]; j <= budget; j++) {
dp[j] = Math.max(dp[j], dp[j - buddleCost[i]] + numNotebook[i]);
}
}
// for (int i = 0; i <= numShop; i++) {
// for (int j = 0; j <= budget; j++) {
// System.out.print(dp[j] + "\t");
// }
// System.out.println();
// }
return dp[budget];
}
public static void main(String[] args) {
VMware vm = new VMware();
int[] arr = new int[] {1, 2, 3};
System.out.println(vm.subarraySum(arr));
System.out.println(vm.subarraySum2(arr));
int[] numNotebook = new int[] {6, 5, 10, 2, 16, 8};
int[] buddleCost = new int[] {3, 2, 5, 1, 6, 4};
int budget = 10;
System.out.println(vm.buddleShop(numNotebook, buddleCost, budget));
}
}