Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

'''
1. 아이디어 :
그냥 조건대로 풀면 된다.

2. 시간복잡도 :
o(n logn - 정렬)

3. 자료구조/알고리즘 :
'''

class Solution:
def absDifference(self, nums: List[int], k: int) -> int:
nums.sort()

return sum(nums[::-1][:k]) - sum(nums[:k])
47 changes: 47 additions & 0 deletions leetcode3/변지협/v3/870. Advantage Shuffle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

'''
1. 아이디어 :
두개를 정렬하고, 스택에 넣어 비교하면서 꺼내서 dictionary에 정리한다.

2. 시간복잡도 :
o(n logn - 정렬)

3. 자료구조/알고리즘 :
'''

from collections import defaultdict

class Solution:
def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
tmp1 = sorted(nums1)[::-1]
tmp2 = sorted(nums2)[::-1]
tmp_lst = []
dic = defaultdict(list)
ans = []

print(tmp1)
print(tmp2)

while True:
if not tmp1:
break

if tmp1[-1] > tmp2[-1]:
t1 = tmp1.pop()
t2 = tmp2.pop()
dic[t2].append(t1)
else:
t1 = tmp1.pop()
tmp_lst.append(t1)

print(dic, tmp_lst)

for i in nums2:
# print(i,dic)
if i in dic and dic[i]:
ans.append(dic[i].pop())
else:
t = tmp_lst.pop()
ans.append(t)

return ans