Skip to content

Commit 690ce12

Browse files
committed
Add solution #1558
1 parent b11ebbf commit 690ce12

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,366 LeetCode solutions in JavaScript
1+
# 1,367 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1189,6 +1189,7 @@
11891189
1553|[Minimum Number of Days to Eat N Oranges](./solutions/1553-minimum-number-of-days-to-eat-n-oranges.js)|Hard|
11901190
1556|[Thousand Separator](./solutions/1556-thousand-separator.js)|Easy|
11911191
1557|[Minimum Number of Vertices to Reach All Nodes](./solutions/1557-minimum-number-of-vertices-to-reach-all-nodes.js)|Medium|
1192+
1558|[Minimum Numbers of Function Calls to Make Target Array](./solutions/1558-minimum-numbers-of-function-calls-to-make-target-array.js)|Medium|
11921193
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11931194
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11941195
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1558. Minimum Numbers of Function Calls to Make Target Array
3+
* https://leetcode.com/problems/minimum-numbers-of-function-calls-to-make-target-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums. You have an integer array arr of the same length with all
7+
* values set to 0 initially. You also have the following modify function.
8+
*
9+
* You want to use the modify function to convert arr to nums using the minimum number of calls.
10+
*
11+
* Return the minimum number of function calls to make nums from arr.
12+
*
13+
* The test cases are generated so that the answer fits in a 32-bit signed integer.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var minOperations = function(nums) {
21+
let totalOperations = 0;
22+
let maxDivisions = 0;
23+
24+
for (let num of nums) {
25+
let currentDivisions = 0;
26+
let increments = 0;
27+
28+
while (num > 0) {
29+
if (num % 2 === 1) {
30+
increments++;
31+
num--;
32+
} else {
33+
currentDivisions++;
34+
num /= 2;
35+
}
36+
}
37+
38+
totalOperations += increments;
39+
maxDivisions = Math.max(maxDivisions, currentDivisions);
40+
}
41+
42+
return totalOperations + maxDivisions;
43+
};

0 commit comments

Comments
 (0)