From bdef470a9ace343c67e374e28c7a58191cacca0e Mon Sep 17 00:00:00 2001 From: rimogsu Date: Thu, 3 Sep 2026 22:32:48 +0900 Subject: [PATCH] 870,3774 --- ...Between Maximum and Minimum K Elements.py" | 16 +++++++ .../v3/870. Advantage Shuffle.py" | 47 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/3774. Absolute Difference Between Maximum and Minimum K Elements.py" create mode 100644 "leetcode3/\353\263\200\354\247\200\355\230\221/v3/870. Advantage Shuffle.py" diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/3774. Absolute Difference Between Maximum and Minimum K Elements.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/3774. Absolute Difference Between Maximum and Minimum K Elements.py" new file mode 100644 index 00000000..ac9bedbb --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/3774. Absolute Difference Between Maximum and Minimum K Elements.py" @@ -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]) \ No newline at end of file diff --git "a/leetcode3/\353\263\200\354\247\200\355\230\221/v3/870. Advantage Shuffle.py" "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/870. Advantage Shuffle.py" new file mode 100644 index 00000000..c74ee2c7 --- /dev/null +++ "b/leetcode3/\353\263\200\354\247\200\355\230\221/v3/870. Advantage Shuffle.py" @@ -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 \ No newline at end of file