3149. Find the Minimum Cost Array Permutation Hard

@problem@discussion
#Array#Dynamic Programming#Bit Manipulation#Bitmask



1/**
2 * [3149] Find the Minimum Cost Array Permutation
3 *
4 * You are given an array nums which is a <span data-keyword="permutation">permutation</span> of [0, 1, 2, ..., n - 1]. The score of any permutation of [0, 1, 2, ..., n - 1] named perm is defined as:
5 * score(perm) = |perm[0] - nums[perm[1]]| + |perm[1] - nums[perm[2]]| + ... + |perm[n - 1] - nums[perm[0]]|
6 * Return the permutation perm which has the minimum possible score. If multiple permutations exist with this score, return the one that is <span data-keyword="lexicographically-smaller-array">lexicographically smallest</span> among them.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [1,0,2]</span>
11 * Output: <span class="example-io">[0,1,2]</span>
12 * Explanation:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/04/example0gif.gif" style="width: 235px; height: 235px;" />
14 * The lexicographically smallest permutation with minimum cost is [0,1,2]. The cost of this permutation is |0 - 0| + |1 - 2| + |2 - 1| = 2.
15 * </div>
16 * <strong class="example">Example 2:
17 * <div class="example-block">
18 * Input: <span class="example-io">nums = [0,2,1]</span>
19 * Output: <span class="example-io">[0,2,1]</span>
20 * Explanation:
21 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/04/example1gif.gif" style="width: 235px; height: 235px;" />
22 * The lexicographically smallest permutation with minimum cost is [0,2,1]. The cost of this permutation is |0 - 1| + |2 - 2| + |1 - 0| = 2.
23 * </div>
24 *  
25 * Constraints:
26 * 
27 * 	2 <= n == nums.length <= 14
28 * 	nums is a permutation of [0, 1, 2, ..., n - 1].
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/find-the-minimum-cost-array-permutation/
34// discuss: https://leetcode.com/problems/find-the-minimum-cost-array-permutation/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn find_permutation(nums: Vec<i32>) -> Vec<i32> {
40        vec![]
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_3149() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.