Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions leetcode3/변지협/v3/1094. Car Pooling.py
Original file line number Diff line number Diff line change
@@ -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


Original file line number Diff line number Diff line change
@@ -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

Loading