3229. Minimum Operations to Make Array Equal to Target Hard
1/**
2 * [3229] Minimum Operations to Make Array Equal to Target
3 *
4 * You are given two positive integer arrays nums and target, of the same length.
5 * In a single operation, you can select any subarray of nums and increment each element within that subarray by 1 or decrement each element within that subarray by 1.
6 * Return the minimum number of operations required to make nums equal to the array target.
7 *
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [3,5,1,2], target = [4,6,2,4]</span>
11 * Output: <span class="example-io">2</span>
12 * Explanation:
13 * We will perform the following operations to make nums equal to target:<br />
14 * - Increment nums[0..3] by 1, nums = [4,6,2,3].<br />
15 * - Increment nums[3..3] by 1, nums = [4,6,2,4].
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">nums = [1,3,2], target = [2,1,4]</span>
20 * Output: <span class="example-io">5</span>
21 * Explanation:
22 * We will perform the following operations to make nums equal to target:<br />
23 * - Increment nums[0..0] by 1, nums = [2,3,2].<br />
24 * - Decrement nums[1..1] by 1, nums = [2,2,2].<br />
25 * - Decrement nums[1..1] by 1, nums = [2,1,2].<br />
26 * - Increment nums[2..2] by 1, nums = [2,1,3].<br />
27 * - Increment nums[2..2] by 1, nums = [2,1,4].
28 * </div>
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length == target.length <= 10^5
33 * 1 <= nums[i], target[i] <= 10^8
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/minimum-operations-to-make-array-equal-to-target/
39// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-equal-to-target/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn minimum_operations(nums: Vec<i32>, target: Vec<i32>) -> i64 {
45
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_3229() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.