공부하고 기록하는, 경제학과 출신 개발자의 노트

hackerrank 4

[Python] Hackerrank. Climbing the Leaderboard (Medium)

https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem Climbing the Leaderboard | HackerRank Help Alice track her progress toward the top of the leaderboard! www.hackerrank.com 전체 scoreboard 값이 주어져 있고, alice의 score가 주어지면 해당 score는 전체 scoreboard에서 몇 등인지를 표시하는 문제. 해커랭크에서 이 문제는 'Implementation'으로 분류돼 있다. 처음 봤을 땐 단순히 'alice의 매 score를 scoreboard에서 binary search해서 풀면 되겠다' 생각했는데, time l..

[Python] Hackerrank. Sherlock and the Valid String (Medium)

https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem Sherlock and the Valid String | HackerRank Remove some characters from the string such that the new string's characters have the same frequency. www.hackerrank.com 문자열의 개수를 셌을 때, 문자열 한 개만 지웠을 때 모든 문자열의 개수가 동일하면 YES, 아니면 NO를 출력하는 함수. "문자열 한 개만 지웠을 때" YES가 가능하려면, 세 가지 경우가 있다. 1. 문자열 개수 자체가 1개인 경우 2. 가장 많이 등장한 문자열에서 1개를 제거했을 때,..

[Python] Hackerrank. Big Sorting (Easy)

https://www.hackerrank.com/challenges/big-sorting/problem Big Sorting | HackerRank Sort an array of very long numeric strings. www.hackerrank.com 난해한 문제는 아니었지만, Python sorting의 특징을 하나 알 수 있었던 문제. 해당 문제 forum을 들어가보면 더 자세히 알 수 있다. Python에서 int -> str, str -> int 형변환은 연산량이 비싼 편이다. 특히 이 문제처럼 숫자값이 클 경우 연산량이 매우 비싸게 작용한다. 따라서 일반적으로 생각하는 문제해결방법인 '문자열을 숫자로 변환 -> 대소비교 -> 정렬' 식의 연산은 time limit에 걸린다. 대안으로 k..

[Python] Hackerrank. Find the Nearest Clone (Medium)

https://www.hackerrank.com/challenges/find-the-nearest-clone/problem Find the nearest clone | HackerRank Find the shortest path length between any two nodes with the given condition. www.hackerrank.com constraint 값이 꽤 커 보여서, 2D Array로 그래프를 구성하는 대신 defaultdict를 사용했다. bfs로 그래프를 순회하면서, 시작점의 color와 순회지점의 color가 일치하는 지점에서 순회 횟수를 반환하면 된다.