목록Algorithm/Programmers (76)
S E P H ' S
코딩테스트 연습 - n^2 배열 자르기 정수 n, left, right가 주어집니다. 다음 과정을 거쳐서 1차원 배열을 만들고자 합니다. n행 n열 크기의 비어있는 2차원 배열을 만듭니다. i = 1, 2, 3, ..., n에 대해서, 다음 과정을 반복합니다. 1행 1열부 programmers.co.kr 풀이 지문 2에서 '1행 1열 부터 i행 i열까지의 영역 내의 모든 빈 칸 숫자 i로 채웁니다'라고 되어있다. 즉, 행과 열에 하나라도 2가 있으면 그 곳은 2, 3이 있으면 3이 되는 것. 그래서 n이 3이라 가정할 때, 2차원 배열은 다음과 같다. 1 (0,0) 2 (0,1) 3 (0,2) 2 (1,0) 2 (1,1) 3 (1,2) 3 (2,0) 3 (2,1) 3 (2,2) 3번에 따라서 이를 행마다..
코딩테스트 연습 - 가장 긴 팰린드롬 앞뒤를 뒤집어도 똑같은 문자열을 팰린드롬(palindrome)이라고 합니다. 문자열 s가 주어질 때, s의 부분문자열(Substring)중 가장 긴 팰린드롬의 길이를 return 하는 solution 함수를 완성해 주세요. 예를들 programmers.co.kr def isPalin(x): if x == x[::-1]: return True def solution(s): answer = 0 for i in range(len(s)): for j in range(i+1, len(s)+1): if isPalin(s[i:j]): if answer < len(s[i:j]): answer = len(s[i:j]) return answer
코딩테스트 연습 - 기지국 설치 N개의 아파트가 일렬로 쭉 늘어서 있습니다. 이 중에서 일부 아파트 옥상에는 4g 기지국이 설치되어 있습니다. 기술이 발전해 5g 수요가 높아져 4g 기지국을 5g 기지국으로 바꾸려 합니다. 그런데 5 programmers.co.kr 풀이 1. 1의 위치에서 부터 기지국이 전파를 전달하는 거리에 위치해 있는지 판단. 2. 범위 안에 있다면 locate를 현재 기지국의 오른쪽 방향으로 거리 밖으로 이동 시키고 idx도 다음 기지국을 가리키도록 1 증가 def solution(n, stations, w): answer = 0 idx = 0 locate = 1 while locate = stations[idx]-w: locate += stations[idx] + w + 1 id..
코딩테스트 연습 - 숫자 게임 xx 회사의 2xN명의 사원들은 N명씩 두 팀으로 나눠 숫자 게임을 하려고 합니다. 두 개의 팀을 각각 A팀과 B팀이라고 하겠습니다. 숫자 게임의 규칙은 다음과 같습니다. 먼저 모든 사원이 무작위로 programmers.co.kr 풀이 1. A의 최솟값이 B의 최댓값 보다 크면 B팀은 절대 못 이김 2. A를 역순으로 정렬. 3. B의 큰 값부터 비교하기 위해 heapq 사용 import heapq def solution(A, B): if min(A) > max(B): return 0 A.sort(reverse=True) B = [-i for i in B] heapq.heapify(B) cnt = 0 for a in A: if a >= abs(B[0]): continue e..