-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.java
57 lines (45 loc) · 1011 Bytes
/
greedy.java
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
package algoAssignments;
import java.util.ArrayList;
public class greedy {
public static void greed(ArrayList<Integer> tank, int[] amounts, int highest, int sum, int goal) {
if (sum == goal)
{
display(tank,amounts);
return;
}
if (sum > goal)
{
return;
}
for (int value:amounts) {
if(value>=highest)
{
ArrayList<Integer> copy = new ArrayList<>();
copy.addAll(tank);
copy.add(value);
//Recursive Element
greed(copy,amounts,value,sum+value,goal);
}
}
}
public static void display(ArrayList<Integer> tank, int[] amounts) {
for (int amount:amounts) {
int count = 0;
for(int key:tank) {
if (key == amount)
{
count++;
}
}
System.out.print(amount);
System.out.print(":");
System.out.println(count);
}
System.out.println();
}
public static void main(String[] args) {
ArrayList<Integer> tank = new ArrayList<>();
int[] amounts = {2,2,3,9,10,11,12,5,6,6};
greed(tank,amounts,0,0,30);
}
}