File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[]} nums1
3+ * @param {number[]} nums2
4+ * @return {number[]}
5+ */
6+ var advantageCount = function(nums1, nums2) {
7+ // nums1 오름차순 정렬
8+ nums1.sort((a, b) => a - b);
9+
10+ // nums2는 원래 위치와 함께 내림차순 정렬
11+ const sortedNums2 = [];
12+
13+ for (let i = 0; i < nums2.length; i++) {
14+ sortedNums2.push([nums2[i], i]);
15+ }
16+
17+ sortedNums2.sort((a, b) => b[0] - a[0]);
18+
19+ const answer = new Array(nums1.length);
20+ let left = 0;
21+ let right = nums1.length - 1;
22+
23+ for (let i = 0; i < sortedNums2.length; i++) {
24+ const value = sortedNums2[i][0];
25+ const originalIndex = sortedNums2[i][1];
26+
27+ if (nums1[right] > value) {
28+ // 이길 수 있으면 가장 큰 숫자로 이긴다
29+ answer[originalIndex] = nums1[right];
30+ right--;
31+ } else {
32+ // 가장 큰 숫자로도 못 이기면 가장 작은 숫자를 버린다
33+ answer[originalIndex] = nums1[left];
34+ left++;
35+ }
36+ }
37+
38+ return answer;
39+ };
You can’t perform that action at this time.
0 commit comments