Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active October 30, 2025 16:53
Show Gist options
  • Select an option

  • Save tatsuyax25/e87b6798bd2e8293f43eec77e0cbfbaa to your computer and use it in GitHub Desktop.

Select an option

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
/**
* @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