Skip to content

Commit 38256a9

Browse files
authored
Add files via upload
1 parent 0c89378 commit 38256a9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

week14/JeongMin/보석 쇼핑.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'''
2+
투 포인터 알고리즘
3+
'''
4+
5+
from collections import defaultdict
6+
def solution(gems):
7+
gems_len=len(set(gems))
8+
dic=defaultdict(int)
9+
dic[gems[0]]=1
10+
11+
answer=[]
12+
start=0; end=0
13+
14+
while start<len(gems) and end<len(gems):
15+
if len(dic)==gems_len: #길이저장
16+
answer.append([start, end])
17+
dic[gems[start]]-=1
18+
if dic[gems[start]]==0 : del dic[gems[start]]
19+
start+=1
20+
else:
21+
end += 1
22+
if end==len(gems) : break
23+
dic[gems[end]]+=1
24+
25+
answer.sort(key=lambda x:((x[1]-x[0]), x[0]))
26+
return [answer[0][0]+1, answer[0][1]+1]

week14/JeongMin/치킨배달.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#0 빈칸, 1 집, 2 치킨집
2+
from itertools import combinations
3+
4+
n, m = map(int, input().split())
5+
graph=[]
6+
home=[]
7+
chicken=[]
8+
for _ in range(n):
9+
graph.append(list(map(int, input().split())))
10+
11+
#치킨집과 집의 좌표 받음
12+
for r in range(n):
13+
for c in range(n):
14+
if graph[r][c]==1:
15+
home.append([r, c])
16+
elif graph[r][c]==2:
17+
chicken.append([r, c])
18+
19+
def dist():
20+
ret=0
21+
for h in home:
22+
tmp=10**8
23+
home_r, home_c = h[0], h[1]
24+
for com in comb_chicken:
25+
chi_r, chi_c = com[0], com[1]
26+
tmp=min(tmp, abs(home_r-chi_r)+abs(home_c-chi_c))
27+
ret+=tmp
28+
return ret
29+
30+
ret=10**8
31+
for comb_chicken in combinations(chicken, m):
32+
ret = min(ret, dist())
33+
print(ret)

0 commit comments

Comments
 (0)