Last active
October 30, 2025 16:53
-
-
Save tatsuyax25/e87b6798bd2e8293f43eec77e0cbfbaa to your computer and use it in GitHub Desktop.
You are given an integer array target. You have an integer array initial of the same size as target with all elements initially zeros. In one operation you can choose any subarray from initial and increment each value by one. Return the minimum num
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} target | |
| * @return {number} | |
| */ | |
| var minNumberOperations = function(target) { | |
| // Initialize the total number of operations needed | |
| let operations = 0; | |
| // Loop through the target array | |
| for (let i = 0; i < target.length; i++) { | |
| if (i === 0) { | |
| // For the first element, we need to perform 'target[0]' operations | |
| operations += target[0]; | |
| } else { | |
| // For subsequent elements, we only need to add the difference | |
| // if the current value is greater than the previous one | |
| let diff = target[i] - target[i - 1]; | |
| if (diff > 0) { | |
| operations += diff; | |
| } | |
| } | |
| } | |
| // Return the total number of operations | |
| return operations; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment