본문 바로가기

알고리즘&자료구조

(18)
[LeetCode] Backtracking - 22. Generate Parentheses 문제 : https://leetcode.com/problems/generate-parentheses/ 복잡한 예제 떠올리기 이 문제의 경우 복잡한 예제는 없다. 입력값은 1 다음결과 2.2 주어진 괄호들을 이용해 무작해로 모든 combination을 만들고 check_parenthesis()로 통과된 것들만 저장 2.3 open 괄호 갯수 >= close 괄호 갯수 성질을 이용하여 combination 수를 줄임 코드를 초안 끄적거리기 (draft) 처음에는 1.1, 2.2를 이용하여 문제를 풀어보기로 하였다. candidates = "()" * n #무작위로 호출할 괄호 후보 만들기 def check_parenthesis(parentheses): # parentheses 규칙에 맞는지 확인하여 True..
[프로그래머스] 코딩테스트 연습 > 스택/큐 > 프린터 def solution(priorities, location): answer = 0 index = [i for i in range(len(priorities))] max_num = max(priorities) while len(priorities) > 0: num = priorities.pop(0) i = index.pop(0) if num != max_num: priorities.append(num) index.append(i) else: answer += 1 if i == location: return answer max_num = max(priorities) return answer
[프로그래머스] 코딩테스트 연습 > 힙(Heap) > 더 맵게 import heapq def solution(scoville, K): answer = 0 heapq.heapify(scoville) while True: num1 = heapq.heappop(scoville) if num1 >= K : break if len(scoville) == 0: return -1 num2 = heapq.heappop(scoville) num = num1 + num2*2 heapq.heappush(scoville, num) answer += 1 return answer
[프로그래머스] 코딩테스트 연습 > 정렬 > K번째수 def solution(array, commands): answer = [] for com in commands: answer.append(sorted(array[com[0]-1:com[1]])[com[2]-1]) return answer def solution(array, commands): return list(map(lambda x:sorted(array[x[0]-1:x[1]])[x[2]-1], commands))
[프로그래머스] 코딩테스트 연습 > 스택/큐 > 기능개발 https://programmers.co.kr/learn/courses/30/lessons/42586 def solution(progresses, speeds): answer = [] left = [100-leftover for leftover in progresses] max = 0 for i in range(len(left)): if left[i]%speeds[i] != 0 : a = left[i]//speeds[i] + 1 else : a = left[i]//speeds[i] if a
[codility] MissingInteger Task descriptionThis is a demo task.Write a function:def solution(A)that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A.For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5.Given A = [1, 2, 3], the function should return 4.Given A = [−1, −3], the function should return 1.Write an efficient algorithm for the foll..
[codility] MaxCounters Task description You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive operations: if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), if A[K] = N + 1 t..
[codility] PermCheck Task description A non-empty array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2 is a permutation, but array A such that: A[0] = 4 A[1] = 1 A[2] = 3 is not a permutation, because value 2 is missing. The goal is to check whether array A is a permutation. ..