-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShopInCandyStore.java
55 lines (43 loc) · 1.74 KB
/
ShopInCandyStore.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
package algorithm.dynamicprogramming;
import java.util.Arrays;
/**
* https://practice.geeksforgeeks.org/problems/shop-in-candy-store/0/?ref=self
*
* @author saukedia1
*
*/
public class ShopInCandyStore {
public static void main(String[] args) {
int categories = 4;
int offerCount = 2;
int[] priceArr = { 3, 2, 1, 4 };
Arrays.sort(priceArr);
System.out.println(recursiveApproachForMinAmount(categories, offerCount, priceArr, 0, priceArr.length));
System.out.println(recursiveApproachForMaxAmount(categories, offerCount, priceArr, 0, priceArr.length));
System.out.println(dynamicApproachForMinAmount(categories, offerCount, priceArr));
}
public static int recursiveApproachForMinAmount(int cat, int off, int[] arr, int low, int high) {
if (cat <= 1 + off) {
return arr[low];
}
return arr[low] + recursiveApproachForMinAmount(cat - (1 + off), off, arr, low + 1, high - off);
}
public static int recursiveApproachForMaxAmount(int cat, int off, int[] arr, int low, int high) {
if (cat <= 1 + off) {
return arr[high - 1];
}
return arr[high - 1] + recursiveApproachForMinAmount(cat - (1 + off), off, arr, low + off, high - 1);
}
public static int dynamicApproachForMinAmount(int cat, int off, int[] arr) {
int[] result = new int[cat + 1];
result[0] = 0;
for (int i = 1; i < arr.length; i++) {
if (i <= off + 1)
result[i] = arr[0];
else
result[i] = arr[i - off - 1 - 1] + arr[i - 1];
}
System.out.println(Arrays.toString(result));
return result[cat];
}
}