1649. Create Sorted Array through Instructions Hard

@problem@discussion
#Array#Binary Search#Divide and Conquer#Binary Indexed Tree#Segment Tree#Merge Sort#Ordered Set



1/**
2 * [1649] Create Sorted Array through Instructions
3 *
4 * Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty container nums. For each element from left to right in instructions, insert it into nums. The cost of each insertion is the minimum of the following:
5 * 
6 * 
7 * 	The number of elements currently in nums that are strictly less than instructions[i].
8 * 	The number of elements currently in nums that are strictly greater than instructions[i].
9 * 
10 * 
11 * For example, if inserting element 3 into nums = [1,2,3,5], the cost of insertion is min(2, 1) (elements 1 and 2 are less than 3, element 5 is greater than 3) and nums will become [1,2,3,3,5].
12 * 
13 * Return the total cost to insert all elements from instructions into nums. Since the answer may be large, return it modulo 10^9 + 7
14 * 
15 *  
16 * Example 1:
17 * 
18 * 
19 * Input: instructions = [1,5,6,2]
20 * Output: 1
21 * Explanation: Begin with nums = [].
22 * Insert 1 with cost min(0, 0) = 0, now nums = [1].
23 * Insert 5 with cost min(1, 0) = 0, now nums = [1,5].
24 * Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6].
25 * Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].
26 * The total cost is 0 + 0 + 0 + 1 = 1.
27 * 
28 * Example 2:
29 * 
30 * 
31 * Input: instructions = [1,2,3,6,5,4]
32 * Output: 3
33 * Explanation: Begin with nums = [].
34 * Insert 1 with cost min(0, 0) = 0, now nums = [1].
35 * Insert 2 with cost min(1, 0) = 0, now nums = [1,2].
36 * Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3].
37 * Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].
38 * Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].
39 * Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].
40 * The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
41 * 
42 * 
43 * Example 3:
44 * 
45 * 
46 * Input: instructions = [1,3,3,3,2,4,2,1,2]
47 * Output: 4
48 * Explanation: Begin with nums = [].
49 * Insert 1 with cost min(0, 0) = 0, now nums = [1].
50 * Insert 3 with cost min(1, 0) = 0, now nums = [1,3].
51 * Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3].
52 * Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].
53 * Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].
54 * Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].
55 * ​​​​​​​Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].
56 * ​​​​​​​Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].
57 * ​​​​​​​Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].
58 * The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
59 * 
60 * 
61 *  
62 * Constraints:
63 * 
64 * 
65 * 	1 <= instructions.length <= 10^5
66 * 	1 <= instructions[i] <= 10^5
67 * 
68 */
69pub struct Solution {}
70
71// problem: https://leetcode.com/problems/create-sorted-array-through-instructions/
72// discuss: https://leetcode.com/problems/create-sorted-array-through-instructions/discuss/?currentPage=1&orderBy=most_votes&query=
73
74// submission codes start here
75
76impl Solution {
77    pub fn create_sorted_array(instructions: Vec<i32>) -> i32 {
78        0
79    }
80}
81
82// submission codes end
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_1649() {
90    }
91}
92


Back
© 2025 bowen.ge All Rights Reserved.