File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,366 LeetCode solutions in JavaScript
1
+ # 1,367 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1189
1189
1553|[ Minimum Number of Days to Eat N Oranges] ( ./solutions/1553-minimum-number-of-days-to-eat-n-oranges.js ) |Hard|
1190
1190
1556|[ Thousand Separator] ( ./solutions/1556-thousand-separator.js ) |Easy|
1191
1191
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|
1192
1193
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|
1193
1194
1576|[ Replace All ?'s to Avoid Consecutive Repeating Characters] ( ./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js ) |Medium|
1194
1195
1598|[ Crawler Log Folder] ( ./solutions/1598-crawler-log-folder.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments