목록Development/Algorithm & Coding Test (189)
컴공생의 다이어리
백준 20922번 : 겹치는 건 싫어 (문제 바로가기) 내 코드 import sys from collections import defaultdict input = sys.stdin.readline n, k = map(int, input().split()) arr = list(map(int, input().split())) max_len = 0 left, right, count = 0, 0, defaultdict(int) while right < n: if count[arr[right]] < k: count[arr[right]] += 1 right += 1 else: count[arr[left]] -= 1 left += 1 max_len = max(max_len, right - left) print(max..
백준 2003번 : 수들의 합 2 (문제 바로가기) 내 코드 import sys input = sys.stdin.readline n, m = map(int, input().split()) arr = list(map(int, input().split())) cnt, temp_sum, left = 0, 0, 0 for right in range(n): temp_sum += arr[right] while temp_sum > m: # 지금까지 합이 m보다 크다면 temp_sum -= arr[left] # 구했던 합의 제일 왼쪽에서 값 삭제 left += 1 if temp_sum == m: cnt += 1 print(cnt)
백준 16507번 : 어두운 건 무서워 (문제 바로가기) 내 코드 import sys input = sys.stdin.readline r, c, q = map(int, input().split()) arr = [list(map(int, input().split())) for _ in range(r)] sum_arr = [[0] * (c + 1) for _ in range(r + 1)] for i in range(1, r + 1): for j in range(1, c + 1): sum_arr[i][j] = sum_arr[i - 1][j] + sum_arr[i][j - 1] + arr[i - 1][j - 1] - sum_arr[i - 1][j - 1] for _ in range(q): r1, c1, r2, ..
백준 1024번 : 수열의 합 (문제 바로가기) 내 코드 n, l = map(int, input().split()) # 수식 # n = (x+1) + (x+2) + ... + (x+l) # n = lx + l(l+1)/2 # lx = n - l(l+1)/2 for i in range(l, 101): x = n - i * (i + 1) / 2 if x % i == 0: x = int(x / i) if x >= -1: print(*list(range(x + 1, x + i + 1))) break else: print(-1)
백준 12851번 : 숨바꼭질 2 (문제 바로가기) 내 코드 from collections import deque n, k = map(int, input().split()) visited = [[-1, 0]] * 100001 visited[n] = [0, 1] queue = deque([n]) while queue: # bfs 수행 pos = queue.popleft() for next_pos in (pos - 1, pos + 1, pos * 2): if 0
백준 16948번 : 데스 나이트 (문제 바로가기) 내 코드 import sys from collections import deque input = sys.stdin.readline n = int(input()) r1, c1, r2, c2 = map(int, input().split()) cnt = 0 arr = [[-1] * n for _ in range(n)] # -1로 초기화 arr[r1][c1] = 0 # 시작 지점을 0으로 세팅 queue = deque([(r1, c1)]) directions = [(-2, -1), (-2, 1), (0, -2), (0, 2), (2, -1), (2, 1)] while queue: # bfs 수행 r, c = queue.popleft() for rr, cc in..
[프로그래머스] 선입 선출 스케줄링 - 파이썬(Python) def solution(n, cores): len_cores = len(cores) if n = n: right = mid else: left = mid + 1 n -= sum(map(lambda x: (right - 1) // x, cores)) for i in range(len_cores): if right % cores[i] == 0..
[프로그래머스] 가장 큰 수 - 파이썬(Python) def solution(numbers): numbers = list(map(str, numbers)) numbers.sort(key=lambda x: x * 3, reverse=True) return str(int("".join(numbers))) https://school.programmers.co.kr/learn/courses/30/lessons/42746 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr
[프로그래머스] 이중우선순위큐 - 파이썬(Python) import heapq def solution(operations): heap = [] for op in operations: print(op) command, num = op.split() if command == 'D': if not heap: # 삭제할 데이터가 없다면 continue if num == '1': # 최댓값 삭제 heap.remove(heapq.nlargest(1, heap)[0]) else: # 최솟값 삭제 heapq.heappop(heap) else: heapq.heappush(heap, int(num)) return [heapq.nlargest(1, heap)[0], heap[0]] if heap else [0, 0] htt..
[프로그래머스] 올바른 괄호 - 파이썬(Python) from collections import deque def solution(s): stack = deque() for i in s: if i == ')' and not stack: # 스택에 아무것도 없는데 ')'가 있는 경우 return False elif i == ')' and stack[-1] == '(': # 괄호쌍 없애줌 stack.pop() else: # i가 '('일 때 stack.append(i) return True if not stack else False https://school.programmers.co.kr/learn/courses/30/lessons/12909 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭..