From bbd3aa60a7d9734252c1627e2f124fad30b2c38b Mon Sep 17 00:00:00 2001 From: Delf-Lee Date: Wed, 24 Feb 2021 21:04:58 +0900 Subject: [PATCH 1/4] Update programmers skill check 3 --- .../skillcheck/SkillCheck0302.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/delf/programmers/skillcheck/SkillCheck0302.java 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))); + + } +} From 8aecb7cf99489ab85eb9e61544661a5075ac6e9c Mon Sep 17 00:00:00 2001 From: Delf-Lee Date: Mon, 1 Mar 2021 21:24:06 +0900 Subject: [PATCH 2/4] Update hacker rank: Greedy Florist --- src/delf/hackerrank/GreedyFlorist.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/delf/hackerrank/GreedyFlorist.java 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; + } +} From e4e7d6c4b854df01ad1f1d02817859b036b60ad6 Mon Sep 17 00:00:00 2001 From: Delf-Lee Date: Tue, 2 Mar 2021 22:12:50 +0900 Subject: [PATCH 3/4] Update hacker rank: Ice Cream Parlor(first try) --- src/delf/hackerrank/IceCreamParlor.java | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/delf/hackerrank/IceCreamParlor.java diff --git a/src/delf/hackerrank/IceCreamParlor.java b/src/delf/hackerrank/IceCreamParlor.java new file mode 100644 index 0000000..231e3dd --- /dev/null +++ b/src/delf/hackerrank/IceCreamParlor.java @@ -0,0 +1,36 @@ +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])) { + System.out.println((map.get(cost[i]) + 1) + " " + (i + 1)); + return; + } + map.put(cost[i], i); + } + + for (int i = 1; i < money; 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; + } + } + } +} From 96235d2eafe628d6b4de922fae3f8e1f5e9554ed Mon Sep 17 00:00:00 2001 From: Delf-Lee Date: Wed, 3 Mar 2021 23:25:30 +0900 Subject: [PATCH 4/4] Update hacker rank: Ice Cream Parlor --- src/delf/hackerrank/IceCreamParlor.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/delf/hackerrank/IceCreamParlor.java b/src/delf/hackerrank/IceCreamParlor.java index 231e3dd..76bd5f6 100644 --- a/src/delf/hackerrank/IceCreamParlor.java +++ b/src/delf/hackerrank/IceCreamParlor.java @@ -14,23 +14,35 @@ public static void main(String[] args) { static void whatFlavors(int[] cost, int money) { Map map = new HashMap<>(); for (int i = 0; i < cost.length; i++) { - if(cost[i] > money) { + if (cost[i] > money) { continue; } if (map.containsKey(cost[i])) { - System.out.println((map.get(cost[i]) + 1) + " " + (i + 1)); - return; + if (cost[i] * 2 == money) { + System.out.println((map.get(cost[i]) + 1) + " " + (i + 1)); + return; + } } map.put(cost[i], i); } - for (int i = 1; i < money; 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; } - } + }*/ } }