diff --git a/src/delf/hackerrank/GreedyFlorist.java b/src/delf/hackerrank/GreedyFlorist.java new file mode 100644 index 0000000..15090b0 --- /dev/null +++ b/src/delf/hackerrank/GreedyFlorist.java @@ -0,0 +1,22 @@ +package hackerrank; + +import java.util.Arrays; + +/** + * Greedy Florist + * https://www.hackerrank.com/challenges/greedy-florist/problem + */ +public class GreedyFlorist { + public static void main(String[] args) { + System.out.println(getMinimumCost(3, new int[]{1, 3, 5, 7, 9})); + } + + static int getMinimumCost(int k, int[] c) { + Arrays.sort(c); + int answer = 0; + for (int i = c.length - 1; i >= 0; i--) { + answer += (((c.length - (i + 1)) / k) + 1) * c[i]; + } + return answer; + } +} diff --git a/src/delf/hackerrank/IceCreamParlor.java b/src/delf/hackerrank/IceCreamParlor.java new file mode 100644 index 0000000..76bd5f6 --- /dev/null +++ b/src/delf/hackerrank/IceCreamParlor.java @@ -0,0 +1,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 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; + } + }*/ + } +} diff --git a/src/delf/programmers/skillcheck/SkillCheck0302.java b/src/delf/programmers/skillcheck/SkillCheck0302.java new file mode 100644 index 0000000..9207ac3 --- /dev/null +++ b/src/delf/programmers/skillcheck/SkillCheck0302.java @@ -0,0 +1,33 @@ +package programmers.skillcheck; + +import java.util.Arrays; + +public class SkillCheck0302 { + public int[] solution(int n, int s) { + int div = s / n; + int rest = s % n; + + if (div == 0) { + return new int[]{-1}; + } + + int[] answer = new int[n]; + Arrays.fill(answer, div); + + if (rest == 0) { + return answer; + } + + int cursor = 0; + for (int i = 0; i < rest; i++, cursor %= n) { + answer[cursor++]++; + } + Arrays.sort(answer); + return answer; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new SkillCheck0302().solution(2, 9))); + + } +}