diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/3774. Absolute Difference Between Maximum and Minimum K Elements.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/3774. Absolute Difference Between Maximum and Minimum K Elements.java" new file mode 100644 index 00000000..bc5dd100 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/3774. Absolute Difference Between Maximum and Minimum K Elements.java" @@ -0,0 +1,29 @@ +/* + +1. 아이디어 : k개의 큰수의합와 k개의 작은수의 합을 구해 차이값을 구하기 + 정렬 후, K번만큼 for문을 돌려 큰수와 작은수의 합을 구하고 빼준다 + + +2. 시간복잡도 : O(NlogN + K) + +3. 자료구조/알고리즘 : 계산 + + */ + +class Solution { + public int absDifference(int[] nums, int k) { + // k개의 가장 크고 작은 수 + + Arrays.sort(nums); + int maxSum = 0; + int minSum = 0; + + for(int i=0; inums2[i]를 만족하는 최대 원소를 가진 아무 배열 반환 + nums1, nums2배열을 정렬후, 투 포인터로 위 조건을 만족하는 최대 원소 배열을 구함 + 이때 nums2의 원소 위치를 미리 저장해놔야한다. + +2. 시간복잡도 : O(2NlogN + N) => O(NlogN) + +3. 자료구조/알고리즘 : 투포인터, 커스텀 정렬 + + */ + +class Solution { + public int[] advantageCount(int[] nums1, int[] nums2) { + // 최대한 많은 원소가 nums1[i] > nums2[i]를 만족 + + // 2 7 11 15 + // 8 12 24 32 + + Arrays.sort(nums1); + List nums2Idx = new ArrayList<>(); + for(int i=0; i{ + return a[1]-b[1]; + }); + + int[] ans = new int[nums1.length]; + + // 2 7 11 15 + // 1 4 10 11 + + // 8 12 24 32 + // 11 13 25 32 + + int r = nums2.length-1; + int l = 0; + + for(int i=nums2.length-1; i>=0; i--) { + int num = nums2Idx.get(i)[1]; + int pos = nums2Idx.get(i)[0]; + + if(nums1[r]>num) { + ans[pos] = nums1[r]; + r--; + } + else { + ans[pos] = nums1[l]; + l++; + } + } + + return ans; + + } +} \ No newline at end of file