3072. Distribute Elements into Two Arrays II
Problem
3072. Distribute Elements into Two Arrays II
Given a 1-indexed integer array nums of length n.
Define function greaterCount such that greaterCount(arr, val) returns the number of elements in arr that are strictly greater than val.
You need to distribute all elements of nums into two arrays arr1 and arr2 using n operations. In the first operation, add nums[1] to arr1. In the second operation, add nums[2] to arr2. For the i-th operation (i > 2):
- If
greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), addnums[i]toarr1. - If
greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), addnums[i]toarr2. - If
greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), add to the array with fewer elements. - If still equal, add
nums[i]toarr1.
Return the concatenation of arr1 and arr2.
Approach
-
Initialization: Reverse
numsso we can usepop()instead ofpop(0)(learned from a senior —pop()is much faster). Assign the first element toarr1andtemp1, and the second toarr2andtemp2. -
Iterative processing: Use
whileto traverse remaining elements. For each element, usebisect.bisect_rightonarr1andarr2to count elements smaller than the current one. Subtract fromlen(arr1)/len(arr2)to get the count of elements greater than it. To use binary search,arr1andarr2must stay sorted — useinsort()in Python. We also maintain a separate answer array to preserve insertion order. -
Merge the answer
Code
import bisect
from typing import List
class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
nums = nums[::-1]
temp = nums.pop()
arr1 = [temp]
temp1 = [temp]
temp = nums.pop()
arr2 = [temp]
temp2 = [temp]
while nums:
temp = nums.pop()
# [28] [2]
index1 = bisect.bisect_right(arr1, temp)
index2 = bisect.bisect_right(arr2, temp)
length_1 = len(arr1) - index1
length_2 = len(arr2) - index2
if length_1 > length_2:
bisect.insort(arr1, temp)
temp1.append(temp)
elif length_1 < length_2:
bisect.insort(arr2, temp)
temp2.append(temp)
else:
if len(arr1) > len(arr2):
bisect.insort(arr2, temp)
temp2.append(temp)
else:
bisect.insort(arr1, temp)
temp1.append(temp)
return temp1 + temp2贡献者
最近更新
Involution Hell© 2026 byCommunityunderCC BY-NC-SA 4.0
2894. Divisible and Non-Divisible Sums Difference
LeetCode 2894. Divisible and Non-Divisible Sums Difference — direct mathematical solution using the arithmetic series formula to compute the sum of numbers divisible by m and those that are not in [1, n], then taking their difference. Suitable for job seekers and algorithm beginners working through easy math problems on LeetCode.
3138. Minimum Length of Anagram Concatenation
LeetCode 3138. Minimum Length of Anagram Concatenation — solution by enumerating substring lengths and using a hash map to verify whether the string can be formed by concatenating anagrams. The core technique is using character frequency arrays to check whether the substring's repeat multiplier matches the original string. Suitable for job seekers preparing for coding assessments and interviews who want to strengthen skills in string and counting algorithms.