본문 바로가기

algorithm/hackerRank23

[HackkerRank] Recursion: Davis' Staircase 1. Link to the problem https://www.hackerrank.com/challenges/ctci-recursive-staircase/problem 2. Feedback [부분집합 문제] 재귀 사용 brute-force 로 푼다면, 재귀이기 때문에 n * n-1 * n-2 … 1 로 O(n의 제곱) 이 중 중복된 작업을 제거하고자 캐시 사용 3. Source public class RecursionDavisStaircase { private static int cache[]; public static void main(String [] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int testcase.. 2018. 6. 26.
[HackkerRank] Hash Tables: Ice Cream Parlor 1. Link to the problem https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem 2. Feedback Hash 를 이용해서 나머지 하나의 값을 찾는 것까지는 생각했는데, 중복 값 처리가 안될거 같아 이 방법을 포기 결국 중복 값이 HashMap 에 덮어쳐도 array 값을 이용해서 덮어쳐져있는 값을 불러오기에 중복 값 문제 해결 됨. 또 중요한 것은 array 의 index != map 의 value 와 일치하면 안됨. 같은 값이니 3. Source package hackerrank; import java.util.HashMap; import java.util.Map; import java.util.Scanner; publ.. 2018. 6. 25.
[HackkerRank] Bit Manipulation: Lonely Integer 1. Link to the problem https://www.hackerrank.com/challenges/ctci-lonely-integer/problem 2. Feedback A = {a1, a2, a3, a4, … ,an} 은 홀수이며, 2개 이상 짝을 이루며, 오직 하나의 수만 유니크 XOR 이란 2개의 명제 중에서 1개만 참인 것을 추출하는 bit manipulation 이다. 모든 수를 XOR 한다면, 같은 pairs 는 비트가 0으로 될테고, 유니크한 값의 비트만 1의 비트가 켜진다. 3. Source public class BitManipulationLonelyInteger { public static void main(String [] args) { Scanner sc = new Sca.. 2018. 6. 25.
HackkerRank: Time Complexity:Primality 1. Link to the problem https://www.hackerrank.com/challenges/ctci-big-o/problem 2. Point brute force 로 문제를 풀면 O(n) 이 나온다. U (Unnecessary Work) Math.sqrt 사용하여 불필요한 작업을 제거했다. Math.sqrt 를 사용한다는 의미는 n 의 루트를 씌움으로써 루트 n 이후의 불필요한 작업은 제거한 것이다. 왜냐면 루트 n * 루트 n 은 n 이기 때문이다. 위 결론에 따라 루트 n 이 prime 인지만 판별하면 되고, 루트 n 의 정수를 추출해서 i = 2 ~ 추출된 정수로 나눈다면 답이 나온다. 3. Source public static void main(String [] args) { S.. 2018. 6. 23.