diff --git "a/leetcode3/\354\240\225\354\247\204\354\230\201/3774. Absolute Difference Between Maximum and Minimum K Elements" "b/leetcode3/\354\240\225\354\247\204\354\230\201/3774. Absolute Difference Between Maximum and Minimum K Elements" new file mode 100644 index 00000000..162a8f35 --- /dev/null +++ "b/leetcode3/\354\240\225\354\247\204\354\230\201/3774. Absolute Difference Between Maximum and Minimum K Elements" @@ -0,0 +1,16 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +var absDifference = function(nums, k) { + const arr = nums.sort((a, b) => a-b); + let sum1 = 0; + let sum2 = 0; + for(let i = 0; i a - b); + + // nums2는 원래 위치와 함께 내림차순 정렬 + const sortedNums2 = []; + + for (let i = 0; i < nums2.length; i++) { + sortedNums2.push([nums2[i], i]); + } + + sortedNums2.sort((a, b) => b[0] - a[0]); + + const answer = new Array(nums1.length); + let left = 0; + let right = nums1.length - 1; + + for (let i = 0; i < sortedNums2.length; i++) { + const value = sortedNums2[i][0]; + const originalIndex = sortedNums2[i][1]; + + if (nums1[right] > value) { + // 이길 수 있으면 가장 큰 숫자로 이긴다 + answer[originalIndex] = nums1[right]; + right--; + } else { + // 가장 큰 숫자로도 못 이기면 가장 작은 숫자를 버린다 + answer[originalIndex] = nums1[left]; + left++; + } + } + + return answer; +};