<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> both of the same length. The <strong>advantage</strong> of <code>nums1</code> with respect to <code>nums2</code> is the number of indices <code>i</code> for which <code>nums1[i] &gt; nums2[i]</code>.</p>

<p>Return <em>any permutation of </em><code>nums1</code><em> that maximizes its <strong>advantage</strong> with respect to </em><code>nums2</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums1 = [2,7,11,15], nums2 = [1,10,4,11]
<strong>Output:</strong> [2,11,7,15]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums1 = [12,24,8,32], nums2 = [13,25,32,11]
<strong>Output:</strong> [24,32,8,12]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums2.length == nums1.length</code></li>
	<li><code>0 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def advantageCount(self, nums1: List[int], nums2: List[int]) -> List[int]:
        nums1.sort()
        result = [-1] * len(nums1)  # 결과를 저장할 리스트
        unused_nums1 = []  # 사용하지 않은 nums1의 값들을 저장
        
        # nums2의 값들과 비교할 때 사용할 정렬된 nums1의 값들
        sorted_indices = sorted(range(len(nums2)), key=lambda i: nums2[i])
        
        j = 0  # nums1에서 사용할 인덱스
        for i in sorted_indices:
            while j < len(nums1) and nums1[j] <= nums2[i]:
                unused_nums1.append(nums1[j])
                j += 1
            if j < len(nums1):
                result[i] = nums1[j]
                j += 1
        
        # remaining elements in nums1 should fill the remaining positions in result
        for i in range(len(result)):
            if result[i] == -1:
                result[i] = unused_nums1.pop()
        
        return result
        # def find_remaining_values(nums1: List[int], result: List[int]) -> List[int]:
        #     nums1_count = Counter(nums1)
        #     result_count = Counter(result)
            
        #     remaining_count = nums1_count - result_count
            
        #     remaining_values = []
        #     for value, count in remaining_count.items():
        #         remaining_values.extend([value] * count)
            
        #     return remaining_values
        # nums1.sort()
        # print(nums1)
        # result = []
        # unmatched = []
        # used_val = [0]*len(nums1)
        # for idx2, num2 in enumerate(nums2):
        #     idx = 0
        #     while idx < len(nums1):
        #         num1 = nums1[idx]
        #         if num2 < num1 and used_val[idx] == 0:
        #             result.append(num1)
        #             used_val[idx] = 1
        #             break
        #         if idx == len(nums1)-1:     
        #             unmatched.append(idx2)
        #         idx += 1 
        # no_mat = find_remaining_values(nums1, result)
        # k = 0 
        # for idx2 in unmatched:
        #     result.insert(idx2, no_mat[k])
        #     k += 1
        # return result
        
34552