3072. Distribute Elements Into Two Arrays II Hard
1/**
2 * [3072] Distribute Elements Into Two Arrays II
3 *
4 * You are given a 1-indexed array of integers nums of length n.
5 * We define a function greaterCount such that greaterCount(arr, val) returns the number of elements in arr that are strictly greater than val.
6 * You need to distribute all the elements of nums between two arrays arr1 and arr2 using n operations. In the first operation, append nums[1] to arr1. In the second operation, append nums[2] to arr2. Afterwards, in the i^th operation:
7 *
8 * If greaterCount(arr1, nums[i]) > greaterCount(arr2, nums[i]), append nums[i] to arr1.
9 * If greaterCount(arr1, nums[i]) < greaterCount(arr2, nums[i]), append nums[i] to arr2.
10 * If greaterCount(arr1, nums[i]) == greaterCount(arr2, nums[i]), append nums[i] to the array with a lesser number of elements.
11 * If there is still a tie, append nums[i] to arr1.
12 *
13 * The array result is formed by concatenating the arrays arr1 and arr2. For example, if arr1 == [1,2,3] and arr2 == [4,5,6], then result = [1,2,3,4,5,6].
14 * Return the integer array result.
15 *
16 * <strong class="example">Example 1:
17 *
18 * Input: nums = [2,1,3,3]
19 * Output: [2,3,1,3]
20 * Explanation: After the first 2 operations, arr1 = [2] and arr2 = [1].
21 * In the 3^rd operation, the number of elements greater than 3 is zero in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
22 * In the 4^th operation, the number of elements greater than 3 is zero in both arrays. As the length of arr2 is lesser, hence, append nums[4] to arr2.
23 * After 4 operations, arr1 = [2,3] and arr2 = [1,3].
24 * Hence, the array result formed by concatenation is [2,3,1,3].
25 *
26 * <strong class="example">Example 2:
27 *
28 * Input: nums = [5,14,3,1,2]
29 * Output: [5,3,1,2,14]
30 * Explanation: After the first 2 operations, arr1 = [5] and arr2 = [14].
31 * In the 3^rd operation, the number of elements greater than 3 is one in both arrays. Also, the lengths are equal, hence, append nums[3] to arr1.
32 * In the 4^th operation, the number of elements greater than 1 is greater in arr1 than arr2 (2 > 1). Hence, append nums[4] to arr1.
33 * In the 5^th operation, the number of elements greater than 2 is greater in arr1 than arr2 (2 > 1). Hence, append nums[5] to arr1.
34 * After 5 operations, arr1 = [5,3,1,2] and arr2 = [14].
35 * Hence, the array result formed by concatenation is [5,3,1,2,14].
36 *
37 * <strong class="example">Example 3:
38 *
39 * Input: nums = [3,3,3,3]
40 * Output: [3,3,3,3]
41 * Explanation: At the end of 4 operations, arr1 = [3,3] and arr2 = [3,3].
42 * Hence, the array result formed by concatenation is [3,3,3,3].
43 *
44 *
45 * Constraints:
46 *
47 * 3 <= n <= 10^5
48 * 1 <= nums[i] <= 10^9
49 *
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/distribute-elements-into-two-arrays-ii/
54// discuss: https://leetcode.com/problems/distribute-elements-into-two-arrays-ii/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59 pub fn result_array(nums: Vec<i32>) -> Vec<i32> {
60 vec![]
61 }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_3072() {
72 }
73}
74
Back
© 2025 bowen.ge All Rights Reserved.