File tree Expand file tree Collapse file tree 3 files changed +30
-94
lines changed Expand file tree Collapse file tree 3 files changed +30
-94
lines changed Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ def solution (gems ): # 가장 짧은 거리
2
+ l = len (set (gems )) # 보석의 개수
3
+
4
+ left , right = 0 , 0 # 투포인터
5
+ dic = {}
6
+ dic [gems [0 ]] = 1 # dic 초기화
7
+ result = [0 , len (gems )- 1 ] # 제일 긴 범위를 잡아놓음.
8
+
9
+
10
+ while left < len (gems ) and right < len (gems ): # 두 개의 포인터가 진열대의 범위를 벗어나지 않는 선에서
11
+ if len (dic ) == l : # 딕셔너리에 담긴 값이 보석의 개수와 같다면, left를 이동시켜서 더 짧은 길이를 찾아야 함.
12
+ if right - left < result [1 ]- result [0 ]: # 중복이 존재할 수 있는 수.
13
+ result = [left , right ] # result 변경
14
+
15
+ if dic [gems [left ]] == 1 :
16
+ del dic [gems [left ]]
17
+ else :
18
+ dic [gems [left ]] -= 1
19
+ left += 1
20
+
21
+ else : # 딕셔너리에 담긴 값이 보석의 개수가 같지 않으면 right를 이동시켜서 범위를 늘림.
22
+ right += 1
23
+ if right == len (gems ):
24
+ break
25
+ if gems [right ] in dic .keys ():
26
+ dic [gems [right ]] += 1
27
+ else :
28
+ dic [gems [right ]] = 1
29
+
30
+ return [result [0 ]+ 1 , result [1 ]+ 1 ]
You can’t perform that action at this time.
0 commit comments