Skip to content

Commit bb4e66c

Browse files
authored
3362. Zero Array Transformation III.cpp solved
3362. Zero Array Transformation III.cpp
2 parents cb3edbd + 3e7433c commit bb4e66c

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
```cpp
2+
// Problem: 3362. Zero Array Transformation III
3+
4+
// Approach:
5+
// Intuition:
6+
// The problem asks us to transform an array of non-negative integers into an array of all zeros using a specific operation. The operation involves choosing an index `i` and a non-negative integer `x`, then subtracting `x` from `nums[i]` and `nums[i+1]`. We want to find the minimum total sum of `x` values used.
7+
8+
// Let's analyze the operation. When we subtract `x` from `nums[i]` and `nums[i+1]`, it means `nums[i]` and `nums[i+1]` are reduced by the same amount. This suggests a greedy approach from left to right.
9+
10+
// Consider `nums[0]`. To make `nums[0]` zero, we must apply an operation at index `0` with `x = nums[0]`. This will also affect `nums[1]`. After this operation, `nums[0]` becomes zero. We then move to `nums[1]` (which has been modified) and repeat the process.
11+
12+
// Let's try a direct greedy approach. Iterate from `i = 0` to `n-1`.
13+
// For each `nums[i]`, if `nums[i]` is not zero, we must perform an operation at index `i` to make it zero. The value of `x` for this operation must be `nums[i]` itself. This operation will also subtract `nums[i]` from `nums[i+1]` (if `i+1 < n`).
14+
// We add `nums[i]` to our total operations count.
15+
16+
// Example:
17+
// nums = [1, 2, 3]
18+
19+
// i = 0: nums[0] = 1. We need to subtract 1 from nums[0] and nums[1].
20+
// x = 1. Total operations = 1.
21+
// nums becomes [0, 1, 3]
22+
23+
// i = 1: nums[1] = 1. We need to subtract 1 from nums[1] and nums[2].
24+
// x = 1. Total operations = 1 + 1 = 2.
25+
// nums becomes [0, 0, 2]
26+
27+
// i = 2: nums[2] = 2. We need to subtract 2 from nums[2] (and nums[3] if it existed, but it doesn't).
28+
// x = 2. Total operations = 2 + 2 = 4.
29+
// nums becomes [0, 0, 0]
30+
31+
// This greedy strategy seems to work. For each element `nums[i]`, if it's positive, we must reduce it to zero. The only way to reduce `nums[i]` without affecting `nums[0...i-1]` (which we've already made zero) is to apply the operation at index `i`. The amount we subtract, `x`, must be exactly `nums[i]` to make it zero. This `x` then also reduces `nums[i+1]`.
32+
33+
// Solution in Code:
34+
class Solution {
35+
public:
36+
long long zeroArray(vector<int>& nums) {
37+
long long total_operations = 0;
38+
int n = nums.size();
39+
40+
for (int i = 0; i < n; ++i) {
41+
if (nums[i] > 0) {
42+
total_operations += nums[i];
43+
if (i + 1 < n) {
44+
nums[i+1] -= nums[i];
45+
// Ensure nums[i+1] does not become negative
46+
if (nums[i+1] < 0) {
47+
nums[i+1] = 0;
48+
}
49+
}
50+
nums[i] = 0; // nums[i] is now zero
51+
}
52+
}
53+
return total_operations;
54+
}
55+
};
56+
```

0 commit comments

Comments
 (0)