코딩테스트
-
문제 (링크) 직선상에 1km 간격으로 배치되어 있는 도시들의 나라 flatland 도시들 중 몇몇은 space station를 가지고 있음 space station으로부터 도시들의 거리들 중 최댓값을 구하기 풀이 def flatlandSpaceStations(n, c): max_gap = 0 if len(c)==1: # end of cities or middle return max(n-c[0]-1, c[0]) c = sorted(c) for i in range(len(c)-1): if c[i+1]-c[i] >max_gap: max_gap=c[i+1]-c[i] return max(max_gap//2, n-c[-1]-1, c[0]) max 함수를 잘 활용해야 하는 문제였다. space station의 위치..
[hackerrank] Flatland Space Stations (Python)문제 (링크) 직선상에 1km 간격으로 배치되어 있는 도시들의 나라 flatland 도시들 중 몇몇은 space station를 가지고 있음 space station으로부터 도시들의 거리들 중 최댓값을 구하기 풀이 def flatlandSpaceStations(n, c): max_gap = 0 if len(c)==1: # end of cities or middle return max(n-c[0]-1, c[0]) c = sorted(c) for i in range(len(c)-1): if c[i+1]-c[i] >max_gap: max_gap=c[i+1]-c[i] return max(max_gap//2, n-c[-1]-1, c[0]) max 함수를 잘 활용해야 하는 문제였다. space station의 위치..
2023.11.19 -
문제 (링크) 괄호 타입에는 [], {}, ()가 있다 balanced bracket이란? 짝이 없는 괄호가 없고, 괄호의 부분집합이 짝이 맞는 괄호안에 들어가있어야 한다 [ 가 열려있으면 같은 타입의 괄호로 닫히기 전까지 다른 타입의 닫힘 괄호가 올 수 없다는게 포인트 풀이 def isBalanced(s): # Write your code here pairs= {'[':']', '{':'}', '(':')'} stacks = [] for i in s: if i in pairs: stacks.append(pairs[i]) else: try: if i != stacks.pop(): return "NO" except: return "NO" if len(stacks)==0: return "YES" else: ..
[hackerrank] balanced brackets (Python)문제 (링크) 괄호 타입에는 [], {}, ()가 있다 balanced bracket이란? 짝이 없는 괄호가 없고, 괄호의 부분집합이 짝이 맞는 괄호안에 들어가있어야 한다 [ 가 열려있으면 같은 타입의 괄호로 닫히기 전까지 다른 타입의 닫힘 괄호가 올 수 없다는게 포인트 풀이 def isBalanced(s): # Write your code here pairs= {'[':']', '{':'}', '(':')'} stacks = [] for i in s: if i in pairs: stacks.append(pairs[i]) else: try: if i != stacks.pop(): return "NO" except: return "NO" if len(stacks)==0: return "YES" else: ..
2023.06.27 -
문제 (링크) 주어진 문자열의 각 알파벳 개수가 모두 같으면 YES 리턴 문자열에서 문자 하나를 뺀 뒤 각 알파벳 개수가 모두 같으면 YES 리턴 이 외의 경우 문자열은 not valid, NO 리턴 파이썬 풀이 def isValid(s): # Write your code here orig_list = list(s) unique_list = list(set(s)) nums = [] for i in unique_list: nums.append(orig_list.count(i)) nums.sort() #ascending ifnum_1= [i for i in nums] ifnum_1[-1] = ifnum_1[-1]-1 if (len(set(nums))==1)| (len(set(ifnum_1))==1): ret..
[hackerrank] Sherlock and the Valid String (Python)문제 (링크) 주어진 문자열의 각 알파벳 개수가 모두 같으면 YES 리턴 문자열에서 문자 하나를 뺀 뒤 각 알파벳 개수가 모두 같으면 YES 리턴 이 외의 경우 문자열은 not valid, NO 리턴 파이썬 풀이 def isValid(s): # Write your code here orig_list = list(s) unique_list = list(set(s)) nums = [] for i in unique_list: nums.append(orig_list.count(i)) nums.sort() #ascending ifnum_1= [i for i in nums] ifnum_1[-1] = ifnum_1[-1]-1 if (len(set(nums))==1)| (len(set(ifnum_1))==1): ret..
2023.06.21 -
문제 (링크) 플레이어 1, 플레이어 2가 번갈아 가며 탑 부수기 게임을 진행 각 플레이어는 최적의 방식으로 게임을 진행함 input : n (int, 탑의 개수) /m (int, 탑의 높이) 각 탑은 탑의 높이를 균등하게 나눌 수 있는 수만큼 층을 감소시킬 수 있음 각 탑의 높이가 모두 1이 되게 만드는 플레이어가 승리 파이썬 풀이 def towerBreakers(n, m): # Write your code here if m==1: return 2 elif n%2 ==0: return 2 elif n%2 ==1: return 1 문제에서 '최적의 방법으로 게임을 진행'한다고 했고, 균등하게 나눌 수 있는 수만큼 층을 나눈다고 설명해 탑의 층수의 약수로만 나눌 수 있는 것처럼 처음에 생각했다. 처음에는 ..
[hackerrank] Tower Breakers (python)문제 (링크) 플레이어 1, 플레이어 2가 번갈아 가며 탑 부수기 게임을 진행 각 플레이어는 최적의 방식으로 게임을 진행함 input : n (int, 탑의 개수) /m (int, 탑의 높이) 각 탑은 탑의 높이를 균등하게 나눌 수 있는 수만큼 층을 감소시킬 수 있음 각 탑의 높이가 모두 1이 되게 만드는 플레이어가 승리 파이썬 풀이 def towerBreakers(n, m): # Write your code here if m==1: return 2 elif n%2 ==0: return 2 elif n%2 ==1: return 1 문제에서 '최적의 방법으로 게임을 진행'한다고 했고, 균등하게 나눌 수 있는 수만큼 층을 나눈다고 설명해 탑의 층수의 약수로만 나눌 수 있는 것처럼 처음에 생각했다. 처음에는 ..
2023.06.18 -
※문제 해석은 책 그대로 쓰지 않고 직접 합니다. 리트코드 706. 해시맵 디자인 1. 문제 Design a HashMap without using any built-in hash table libraries. 빌트인 해쉬 테이블 라이브러리를 사용하지 말고 해시맵을 디자인하세요. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. MyHashMap()은 빈 맵으로 초기화합니다. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corre..
[leetcode] 리트코드 706번 해시맵 디자인※문제 해석은 책 그대로 쓰지 않고 직접 합니다. 리트코드 706. 해시맵 디자인 1. 문제 Design a HashMap without using any built-in hash table libraries. 빌트인 해쉬 테이블 라이브러리를 사용하지 말고 해시맵을 디자인하세요. Implement the MyHashMap class: MyHashMap() initializes the object with an empty map. MyHashMap()은 빈 맵으로 초기화합니다. void put(int key, int value) inserts a (key, value) pair into the HashMap. If the key already exists in the map, update the corre..
2023.02.01