-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrelative_sort_array.py
More file actions
35 lines (27 loc) ยท 1.07 KB
/
relative_sort_array.py
File metadata and controls
35 lines (27 loc) ยท 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 1122. Relative Sort Array
# https://leetcode.com/problems/relative-sort-array/
from collections import defaultdict
from typing import List
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
# arr2 ๋ฅผ ํค๋ก ์ผ์์, arr1 ์ ์ ๋ ฌํ๋ ๋ฌธ์ ์ด๋ค.
# arr1 ์ ์์๋ฅผ ์นด์ดํ
ํด ์ค ํ, arr2 ์์ ๋๋ก ์นด์ดํ
ํ ๊ฐ์ ๋งํผ ๊ฐ์ ๋ง๋ค์ด์ค๋ค.
# ์ฌ์ฉํ ํค(๊ฐ)๋ ์ ๊ฑฐํด์ค๋ค.
# ๊ทธ๋ฆฌ๊ณ ๋จ์ ํค(๊ฐ) ๋ค์ ๋ํ์ฌ ์ ๋ ฌ (s_count) ์ ์งํํ ํ ๋ค์ ๊ทธ ํค์ ๊ฐ์ ๋งํผ ๋ฐ๋ณตํด์ ๊ฐ์ ๋ํด์ค๋ค.
count = defaultdict(int)
for i in range(len(arr1)):
count[arr1[i]] += 1
res = []
for k in arr2:
j = 0
while j < count[k]:
res.append(k)
j += 1
del count[k]
s_count = sorted(count)
for k in s_count:
j = 0
while j < count[k]:
res.append(k)
j += 1
return res