파이썬
-
백준 10828번 어떻게 풀었나, 스택(Stack) 구현백준 2023. 1. 5. 00:01
푼 방법 문제에 적혀있는 데로 구현 해 주었다.. 더 알고 싶은 부분 한줄에 조건문 쓰는 법 if 아래 두 코드는 똑같다. if animal is dog: ret = dog if animal is dog: ret = dog if - else 결과 = A if 조건 else B if - elif - else 결과 = A if 조건 else B if 조건 else C 코드 import sys N = int(sys.stdin.readline()) stack = [] for i in range(N): cmd = sys.stdin.readline().strip() # push X: 정수 X를 스택에 넣는 연산이다. if "push" in cmd: stack.append(cmd[5:]) # pop: 스택에서 가장 ..
-
백준 10989번 어떻게 풀었나백준 2022. 12. 31. 00:01
푼 방법 2751번(수 정렬2)과 똑같은 코드를 제출했다. 그런데 pypy3, python3 둘다 제출해도 메모리 초과가 떳다. 이건 내가 알 수 없는 문제다라고 생각하고 바로 구글링 했다. - 검색을 해 보니 sort()를 쓰면 메모리 초과가 된다고 한다. 그래서 계수정렬(counting sort)를 써야 된다. 더 배우고 싶은 부분 계수정렬 코드 import sys N = int(sys.stdin.readline()) counting = [0]*10000 for i in range(N): a = int(sys.stdin.readline()) counting[a-1] += 1 for k in range(10000): if counting[k] != 0: for j in range(counting[k])..