-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1144-decrease-elements-to-make-array-zigzag.js
More file actions
121 lines (112 loc) · 4.33 KB
/
1144-decrease-elements-to-make-array-zigzag.js
File metadata and controls
121 lines (112 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Decrease Elements To Make Array Zigzag
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
var movesToMakeZigzag = function (nums) {
function calculateMovesForEvenPeaks(initialNumbers) {
let totalMovesFirstType = 0;
let tempArrayFirstType = [...initialNumbers];
for (
let currentPositionFirst = 0;
currentPositionFirst < tempArrayFirstType.length;
currentPositionFirst++
) {
if (currentPositionFirst % 2 === 0) {
if (currentPositionFirst > 0) {
let valueNeededLeft = tempArrayFirstType[currentPositionFirst] - 1;
if (tempArrayFirstType[currentPositionFirst - 1] >= valueNeededLeft) {
totalMovesFirstType +=
tempArrayFirstType[currentPositionFirst - 1] - valueNeededLeft;
tempArrayFirstType[currentPositionFirst - 1] = valueNeededLeft;
}
}
if (currentPositionFirst < tempArrayFirstType.length - 1) {
let valueNeededRight = tempArrayFirstType[currentPositionFirst] - 1;
if (
tempArrayFirstType[currentPositionFirst + 1] >= valueNeededRight
) {
totalMovesFirstType +=
tempArrayFirstType[currentPositionFirst + 1] - valueNeededRight;
tempArrayFirstType[currentPositionFirst + 1] = valueNeededRight;
}
}
} else {
let leftComparisonValue =
currentPositionFirst > 0
? tempArrayFirstType[currentPositionFirst - 1]
: Infinity;
let rightComparisonValue =
currentPositionFirst < tempArrayFirstType.length - 1
? tempArrayFirstType[currentPositionFirst + 1]
: Infinity;
let minimumTargetValue =
Math.min(leftComparisonValue, rightComparisonValue) - 1;
if (tempArrayFirstType[currentPositionFirst] >= minimumTargetValue) {
totalMovesFirstType +=
tempArrayFirstType[currentPositionFirst] - minimumTargetValue;
tempArrayFirstType[currentPositionFirst] = minimumTargetValue;
}
}
}
return totalMovesFirstType;
}
function calculateMovesForOddPeaks(initialNumbers) {
let totalMovesSecondType = 0;
let tempArraySecondType = [...initialNumbers];
for (
let currentPositionSecond = 0;
currentPositionSecond < tempArraySecondType.length;
currentPositionSecond++
) {
if (currentPositionSecond % 2 !== 0) {
if (currentPositionSecond > 0) {
let requiredLeftAmount =
tempArraySecondType[currentPositionSecond] - 1;
if (
tempArraySecondType[currentPositionSecond - 1] >= requiredLeftAmount
) {
totalMovesSecondType +=
tempArraySecondType[currentPositionSecond - 1] -
requiredLeftAmount;
tempArraySecondType[currentPositionSecond - 1] = requiredLeftAmount;
}
}
if (currentPositionSecond < tempArraySecondType.length - 1) {
let requiredRightAmount =
tempArraySecondType[currentPositionSecond] - 1;
if (
tempArraySecondType[currentPositionSecond + 1] >=
requiredRightAmount
) {
totalMovesSecondType +=
tempArraySecondType[currentPositionSecond + 1] -
requiredRightAmount;
tempArraySecondType[currentPositionSecond + 1] =
requiredRightAmount;
}
}
} else {
let neighborCheckLeft =
currentPositionSecond > 0
? tempArraySecondType[currentPositionSecond - 1]
: Infinity;
let neighborCheckRight =
currentPositionSecond < tempArraySecondType.length - 1
? tempArraySecondType[currentPositionSecond + 1]
: Infinity;
let optimalValueSecond =
Math.min(neighborCheckLeft, neighborCheckRight) - 1;
if (tempArraySecondType[currentPositionSecond] >= optimalValueSecond) {
totalMovesSecondType +=
tempArraySecondType[currentPositionSecond] - optimalValueSecond;
tempArraySecondType[currentPositionSecond] = optimalValueSecond;
}
}
}
return totalMovesSecondType;
}
const movesWhenEvenArePeaks = calculateMovesForEvenPeaks(nums);
const movesWhenOddArePeaks = calculateMovesForOddPeaks(nums);
return Math.min(movesWhenEvenArePeaks, movesWhenOddArePeaks);
};