Skip to content

Commit 6b4976e

Browse files
Merge pull request #2170 from CodingTestStudy2/변지협
[변지협] 2026/10/13
2 parents 15399c9 + 7528c20 commit 6b4976e

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
1. 아이디어 :
3+
1943. Describe the Painting.py
4+
4개월전에 코딩테스트 참여했을 때 풀었던 문제인데 구조적으로 100% 동일한 문제임.
5+
from을 +, to를 -로 둬서 해시의 누적합을 구하고, 여기서 누적합이 capacity를 초과하는지 확인하면 된다.
6+
7+
2. 시간복잡도 :
8+
o(n)
9+
10+
3. 자료구조/알고리즘 :
11+
- 배열(누적합), 해시맵(탑승/하차 위치 기록)
12+
'''
13+
from collections import defaultdict
14+
class Solution:
15+
def carPooling(self, trips: list[list[int]], capacity: int) -> bool:
16+
dic = defaultdict(int)
17+
18+
for n, _from, to in trips:
19+
dic[_from] += n
20+
dic[to] -= n
21+
22+
print(dic)
23+
24+
current = 0
25+
for i in [value for key, value in sorted(dic.items(), key=lambda x:x[0])]:
26+
current += i
27+
if current > capacity:
28+
return False
29+
30+
return True
31+
32+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
1. 아이디어 :
3+
따져봤을 때 2번 안에 퀸이 다 갈 수 있음.
4+
0번: src == target
5+
1번: 퀸의 이동경로 (src!=target)
6+
2번: 나머지 전부
7+
8+
2. 시간복잡도 :
9+
o(1) - 경로만 바로 구하면 됨
10+
11+
3. 자료구조/알고리즘 :
12+
'''
13+
from collections import deque
14+
class Solution:
15+
def minQueenMoves(self, source: list[int], target: list[int]) -> int:
16+
x1,y1 = tuple(source)
17+
x2,y2 = tuple(target)
18+
if x1 == x2 and y1 == y2:
19+
return 0
20+
21+
st = set()
22+
23+
for i in range(1,9):
24+
st.add((x1,i))
25+
st.add((i,y1))
26+
27+
print(st)
28+
29+
x = x1
30+
y = y1
31+
while 1 <= x < 9 and 1<= y < 9:
32+
xtmp = x+1
33+
ytmp = y+1
34+
x = xtmp
35+
y = ytmp
36+
st.add((x,y))
37+
38+
x = x1
39+
y = y1
40+
while 1 <= x < 9 and 1<= y < 9:
41+
xtmp = x-1
42+
ytmp = y-1
43+
x = xtmp
44+
y = ytmp
45+
st.add((x,y))
46+
x = x1
47+
y = y1
48+
49+
while 1 <= x < 9 and 1<= y < 9:
50+
xtmp = x+1
51+
ytmp = y-1
52+
x = xtmp
53+
y = ytmp
54+
st.add((x,y))
55+
x = x1
56+
y = y1
57+
58+
while 1 <= x < 9 and 1<= y < 9:
59+
xtmp = x-1
60+
ytmp = y+1
61+
x = xtmp
62+
y = ytmp
63+
st.add((x,y))
64+
65+
if (x2,y2) in st:
66+
return 1
67+
else:
68+
return 2
69+

0 commit comments

Comments
 (0)