From 7528c20cfc555e6785405eaa1df08f2f1584c826 Mon Sep 17 00:00:00 2001 From: rimogsu Date: Tue, 13 Oct 2026 22:30:20 +0900 Subject: [PATCH] 1094,4061 --- .../v3/1094. Car Pooling.py" | 32 +++++++++ ...1. Minimum Queen Moves to Reach Target.py" | 69 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/1094. Car Pooling.py" create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/4061. Minimum Queen Moves to Reach Target.py" diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1094. Car Pooling.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1094. Car Pooling.py" new file mode 100644 index 00000000..ea665b51 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/1094. Car Pooling.py" @@ -0,0 +1,32 @@ +''' +1. 아이디어 : +1943. Describe the Painting.py +4개월전에 코딩테스트 참여했을 때 풀었던 문제인데 구조적으로 100% 동일한 문제임. +from을 +, to를 -로 둬서 해시의 누적합을 구하고, 여기서 누적합이 capacity를 초과하는지 확인하면 된다. + +2. 시간복잡도 : +o(n) + +3. 자료구조/알고리즘 : +- 배열(누적합), 해시맵(탑승/하차 위치 기록) +''' +from collections import defaultdict +class Solution: + def carPooling(self, trips: list[list[int]], capacity: int) -> bool: + dic = defaultdict(int) + + for n, _from, to in trips: + dic[_from] += n + dic[to] -= n + + print(dic) + + current = 0 + for i in [value for key, value in sorted(dic.items(), key=lambda x:x[0])]: + current += i + if current > capacity: + return False + + return True + + diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/4061. Minimum Queen Moves to Reach Target.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/4061. Minimum Queen Moves to Reach Target.py" new file mode 100644 index 00000000..a2ede354 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/4061. Minimum Queen Moves to Reach Target.py" @@ -0,0 +1,69 @@ +''' +1. 아이디어 : +따져봤을 때 2번 안에 퀸이 다 갈 수 있음. +0번: src == target +1번: 퀸의 이동경로 (src!=target) +2번: 나머지 전부 + +2. 시간복잡도 : +o(1) - 경로만 바로 구하면 됨 + +3. 자료구조/알고리즘 : +''' +from collections import deque +class Solution: + def minQueenMoves(self, source: list[int], target: list[int]) -> int: + x1,y1 = tuple(source) + x2,y2 = tuple(target) + if x1 == x2 and y1 == y2: + return 0 + + st = set() + + for i in range(1,9): + st.add((x1,i)) + st.add((i,y1)) + + print(st) + + x = x1 + y = y1 + while 1 <= x < 9 and 1<= y < 9: + xtmp = x+1 + ytmp = y+1 + x = xtmp + y = ytmp + st.add((x,y)) + + x = x1 + y = y1 + while 1 <= x < 9 and 1<= y < 9: + xtmp = x-1 + ytmp = y-1 + x = xtmp + y = ytmp + st.add((x,y)) + x = x1 + y = y1 + + while 1 <= x < 9 and 1<= y < 9: + xtmp = x+1 + ytmp = y-1 + x = xtmp + y = ytmp + st.add((x,y)) + x = x1 + y = y1 + + while 1 <= x < 9 and 1<= y < 9: + xtmp = x-1 + ytmp = y+1 + x = xtmp + y = ytmp + st.add((x,y)) + + if (x2,y2) in st: + return 1 + else: + return 2 +