파이썬 106

[프로그래머스] 해시 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/42576 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 from collections import defaultdict def solution(participant, completion): hash_table = defaultdict(int) for i in participant: hash_table[i] += 1 for i in completion: hash_table[i] -= 1 if hash_table[i] == 0: del hash_..

BOJ/Python 2024.03.23

leetcode TwoSum 파이썬 - HashTable

https://leetcode.com/problems/two-sum/description/ 내 코드# 시간복잡도 O(N^2) class Solution(object): def twoSum(self, nums, target): result = [] for i in range(0, len(nums)): for j in range(i+1, len(nums)): if nums[i] + nums[j] == target: result += [i, j] return result# 시간복잡도 O(N) class Solution(object): def twoSum(self, nums, target): hash_table = {} # 해시 테이블 -> 딕셔너리 for i, num in enumerate(nums): com..

leetcode 2024.03.21

백준 14502번 연구소 파이썬 BFS

https://www.acmicpc.net/problem/14502 14502번: 연구소인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크www.acmicpc.net 내 답안import sys import copy from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) lab = [list(map(int, input().split())) for _ in range(N)] dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] def find_spaces(lab..

BOJ/Python 2024.03.20

백준 14719번 빗물 파이썬

https://www.acmicpc.net/problem/14719 14719번: 빗물 첫 번째 줄에는 2차원 세계의 세로 길이 H과 2차원 세계의 가로 길이 W가 주어진다. (1 ≤ H, W ≤ 500) 두 번째 줄에는 블록이 쌓인 높이를 의미하는 0이상 H이하의 정수가 2차원 세계의 맨 왼쪽 위치 www.acmicpc.net 내 코드 h, w = map(int, input().split()) blocks = list(map(int, input().split())) result = 0 for i in range(1, w-1): left_max = max(blocks[:i]) right_max = max(blocks[i+1:]) standard = min(left_max, right_max) depth ..

BOJ/Python 2024.03.19

백준 1966번 프린터 큐 파이썬

https://www.acmicpc.net/problem/1966 1966번: 프린터 큐 여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에 www.acmicpc.net 내 답안 from collections import deque N = int(input()) for i in range(N): N, M = map(int, input().split()) priority = deque(list(map(int, input().split()))) ind = deque([i for i in range(N)]) cnt = 0 while priority: m = max(prio..

BOJ/Python 2024.03.08

[프로그래머스] 의상 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/42578 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 답안 from collections import defaultdict def solution(clothes): dic = defaultdict(int) answer = 1 for _, y in clothes: dic[y] += 1 for _, j in dic.items(): answer *= (j+1) return answer-1 Review 이 문제는 보자마자 구현이 아닌 수학적으로 접근해야..

BOJ/Python 2024.02.12

[프로그래머스] 키패드 누르기 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/67256 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 문제 보기 더보기 내 코드 def judge(start, end): keypad = { '1': (0, 0), '2': (0, 1), '3': (0, 2), '4': (1, 0), '5': (1, 1), '6': (1, 2), '7': (2, 0), '8': (2, 1), '9': (2, 2), '*': (3, 0), '0': (3, 1), '#': (3, 2) } start_distance = ..

BOJ/Python 2023.11.24

[프로그래머스] 주차 요금 계산 파이썬

https://school.programmers.co.kr/learn/courses/30/lessons/92341 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 내 코드 import math def solution(fees, records): default = 1439 # 23:59 result = {} answer = [] # 차 번호순으로 정렬후 뒤집어 정렬 lst = reversed(sorted(records, key=lambda x: int(x.split()[1]))) for l in lst: i, j, k = l.split() m = int(i[..

프로그래머스 2023.06.01

[Python] 문자열 치환하는 방법 replace

replace str.replace(old, new[, count]) 모든 부분 문자열 old 가 new 로 치환된 문자열의 복사본을 돌려줍니다. 선택적 인자 count 가 주어지면, 앞의 count 개만 치환됩니다. old : 바꾸고 싶은 문자열 new : 새로 바꾸고 싶은 문자열 count : 앞에서 부터 count 개수 만큼 문자 바꾸기 예시 ) a = "hello world" b = "abcd****efgh" print(a.replace("hello", "hi")) print(b.replace("*", "0")) # 전부 변경 print(b.replace("*", "0", 2)) # 부분 변경 (앞에서 부터) """ *output* hi world abcd0000efgh abcd00**efgh """

Algorithm 2023.04.06

백준 10814번 나이순 정렬 파이썬

https://www.acmicpc.net/problem/10814 10814번: 나이순 정렬 온라인 저지에 가입한 사람들의 나이와 이름이 가입한 순서대로 주어진다. 이때, 회원들을 나이가 증가하는 순으로, 나이가 같으면 먼저 가입한 사람이 앞에 오는 순서로 정렬하는 프로그램을 www.acmicpc.net 내 코드 import sys input = sys.stdin.readline N = int(input()) member = [] for i in range(N): x, y = input().split() member.append([int(x), y, i]) result = sorted(member, key = lambda x: (x[0], x[2])) for i in range(N): print(res..

BOJ/Python 2023.01.18