Coding Test

Coding Test

[Python] 백준 2161 - 카드1

문제https://www.acmicpc.net/problem/2161 해설큐의 구조를 활용하면 된다. def solution(cards): answer = [] while len(cards)-1: answer.append(cards.pop(0)) cards.insert(len(cards)-1, cards.pop(0)) answer.append(cards.pop(0)) return " ".join(answer)num = int(input())print(solution([str(i+1) for i in range(num)]))

Coding Test

[Python] 프로그래머스 - 같은 숫자는 싫어

문제https://school.programmers.co.kr/learn/courses/30/lessons/12906 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 해설중복 제거 후 원본 순서대로 반환하는 로직이다. 자연어 처리에서 단어 Encoding 방식인 바이트 페어 인코딩(Byte Pair Encoding, BPE)과 유사하다.def solution(arr): answer = [] for v in arr: if not arr or answer[-1] != v: answer.append(v) return answer

Coding Test

[Python] 백준 10845 - 큐

문제https://www.acmicpc.net/problem/10845 해설큐의 동작을 구현하는 문제이다. def solution(command): queue = [] for c in command: if c[0] == "push": queue.append(c[1]) elif c[0] == "pop": print("-1" if len(queue) == 0 else queue.pop(0)) elif c[0] == "size": print(len(queue)) elif c[0] == "empty": print("1" if len(queue) == 0 else "0") ..

Coding Test

[Python] 백준 12605 - 단어순서 뒤집기

문제https://www.acmicpc.net/problem/12605 해설간단한 문자열 뒤집기였다. 여러 가지 방식으로 접근하여 문제를 해결해 보았다. Solution 1.스택을 사용하여 하나씩 pop 하는 걸 의도했을지도 모르겠지만 제목 그대로 나는 단어순서만 역정렬하여 해결하였다.  def solution(lst): for i, s in enumerate(lst): sentence = " ".join(s) print(f"Case #{i+1}: {sentence}")cnt = int(input())sentences = [input().split(" ")[::-1] for _ in range(cnt)]solution(sentences) Solution 2.아무 쓸모도 없고..

Coding Test

[Python] 백준 10828 - 스택

문제https://www.acmicpc.net/problem/10828 해설스택을 구현하는 문제였다. def solution(command): stack = [] for c in command: if c[0] == "push": stack.append(c[1]) elif c[0] == "pop": print("-1" if len(stack) == 0 else stack.pop()) elif c[0] == "size": print(len(stack)) elif c[0] == "empty": print("1" if len(stack) == 0 else "0") ..

Coding Test

[Python] 프로그래머스 - 완주하지 못한 선수

문제https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=python3 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr 해설시간복잡도로 인해 개자 작성하던 코드의 시간 복잡도가 \( O(n^2) \)로 수렴하여 Timeout fail이 발생하였다. 이를 해결하기 위해 Counter 함수 사용으로 \( O(n) \)으로 수렴하도록 하는 것이 핵심이었다. Solution 1.collections 패키지를 이용하여 Counter 메소드를 활용해보았다. import collectionsdef solution(participant, co..

AlienCoder
'Coding Test' 카테고리의 글 목록 (7 Page)
loading