본문 바로가기

hackkerrank6

[HackkerRank] Sherlock and Anagrams 1. Link to the problem https://www.hackerrank.com/challenges/sherlock-and-anagrams/problem?h_l=interview&playlist_slugs[]=interview-preparation-kit&playlist_slugs[]=dictionaries-hashmaps 2. Feedback Brute force 로 풀었는데, O(n3) 이 나와서 최적화 하는데 시간 엄청 잡아먹음. 문제 Constaint 에 1 2018. 10. 12.
[HackerRank] Sock Merchant 1. Link to the problem https://www.hackerrank.com/challenges/sock-merchant/problem?h_l=interview&playlist_slugs[]=interview-preparation-kit&playlist_slugs[]=warmup 2. Feedback 색깔이 맞는 양말 개수를 구하라. 메모리를 이용하여 문제 해결. Map 에 데이터를 담아 양말 색깔을 누적시킨 뒤, 나누기 2하여 양말 개수를 구함. 최종적으로 구한 양말 개수를 누적하여 답 추출 3. Source public class SockMerchant { public static void main(String [] args) { Scanner in = new Scanner(System... 2018. 10. 11.
[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: 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.