-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathIceCreamParlor.java
More file actions
48 lines (43 loc) · 1.46 KB
/
IceCreamParlor.java
File metadata and controls
48 lines (43 loc) · 1.46 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
package hackerrank;
import java.util.*;
/**
* Hash Tables: Ice Cream Parlor
* https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem
*/
public class IceCreamParlor {
public static void main(String[] args) {
whatFlavors(new int[]{2, 2, 4, 3}, 4);
}
static void whatFlavors(int[] cost, int money) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < cost.length; i++) {
if (cost[i] > money) {
continue;
}
if (map.containsKey(cost[i])) {
if (cost[i] * 2 == money) {
System.out.println((map.get(cost[i]) + 1) + " " + (i + 1));
return;
}
}
map.put(cost[i], i);
}
for (int c : map.keySet()) {
if (map.containsKey(money - c)) {
int one = map.get(c) + 1;
int another = map.get(money - c) + 1;
System.out.println(Math.min(one, another) + " " + Math.max(one, another));
return;
}
}
/*for (int i = 1; i < money; i++) {
int m = map.get(i);
if (map.containsKey(i) && map.containsKey(money - i)) {
int one = map.get(i) + 1;
int another = map.get(money - i) + 1;
System.out.println(Math.min(one, another) + " " + Math.max(one, another));
return;
}
}*/
}
}