Skip to content

Commit bdef470

Browse files
committed
870,3774
1 parent 9d1c85c commit bdef470

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
'''
3+
1. 아이디어 :
4+
그냥 조건대로 풀면 된다.
5+
6+
2. 시간복잡도 :
7+
o(n logn - 정렬)
8+
9+
3. 자료구조/알고리즘 :
10+
'''
11+
12+
class Solution:
13+
def absDifference(self, nums: List[int], k: int) -> int:
14+
nums.sort()
15+
16+
return sum(nums[::-1][:k]) - sum(nums[:k])
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
'''
3+
1. 아이디어 :
4+
두개를 정렬하고, 스택에 넣어 비교하면서 꺼내서 dictionary에 정리한다.
5+
6+
2. 시간복잡도 :
7+
o(n logn - 정렬)
8+
9+
3. 자료구조/알고리즘 :
10+
'''
11+
12+
from collections import defaultdict
13+
14+
class Solution:
15+
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
16+
tmp1 = sorted(nums1)[::-1]
17+
tmp2 = sorted(nums2)[::-1]
18+
tmp_lst = []
19+
dic = defaultdict(list)
20+
ans = []
21+
22+
print(tmp1)
23+
print(tmp2)
24+
25+
while True:
26+
if not tmp1:
27+
break
28+
29+
if tmp1[-1] > tmp2[-1]:
30+
t1 = tmp1.pop()
31+
t2 = tmp2.pop()
32+
dic[t2].append(t1)
33+
else:
34+
t1 = tmp1.pop()
35+
tmp_lst.append(t1)
36+
37+
print(dic, tmp_lst)
38+
39+
for i in nums2:
40+
# print(i,dic)
41+
if i in dic and dic[i]:
42+
ans.append(dic[i].pop())
43+
else:
44+
t = tmp_lst.pop()
45+
ans.append(t)
46+
47+
return ans

0 commit comments

Comments
 (0)