当前位置:网站首页>LeetCode 1558. Get the minimum number of function calls of the target array
LeetCode 1558. Get the minimum number of function calls of the target array
2022-07-06 16:43:00 【Daylight629】
1558. Get the minimum number of function calls to the target array

Give you a nums The size is the same and the initial value is 0 Array of arr , Please call the above function to get the integer array nums .
Please return and will arr become nums Minimum number of function calls .
The answer is guaranteed in 32 Bits within signed integers .
Example 1:
Input :nums = [1,5]
Output :5
explain : Add... To the second number 1 :[0, 0] become [0, 1] (1 operations ).
Multiply all numbers by 2 :[0, 1] -> [0, 2] -> [0, 4] (2 operations ).
Add... To both numbers 1 :[0, 4] -> [1, 4] -> [1, 5] (2 operations ).
The total number of operations is :1 + 2 + 2 = 5 .
Example 2:
Input :nums = [2,2]
Output :3
explain : Add... To both numbers 1 :[0, 0] -> [0, 1] -> [1, 1] (2 operations ).
Multiply all numbers by 2 : [1, 1] -> [2, 2] (1 operations ).
The total number of operations is : 2 + 1 = 3 .
Example 3:
Input :nums = [4,2,5]
Output :6
explain :( initial )[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5] (nums Array ).
Example 4:
Input :nums = [3,2,2,4]
Output :7
Example 5:
Input :nums = [2,4,8,16]
Output :8
Tips :
1 <= nums.length <= 10^50 <= nums[i] <= 10^9
Two 、 Method 1
greedy
class Solution {
public int minOperations(int[] nums) {
int ret = 0, maxn = 0;
for (int num : nums) {
maxn = Math.max(maxn, num);
while (num != 0) {
if ((num & 1) != 0) {
ret++;
}
num >>= 1;
}
}
if (maxn != 0) {
while (maxn != 0) {
ret++;
maxn >>= 1;
}
ret--;
}
return ret;
}
}
Complexity analysis
Time complexity :O(n×m), among nn It's an array nums The length of ,m Is the maximum number of bits of the binary representation of the elements in the array , These elements in this question are 32 Bit signed integers , therefore m<32.
Spatial complexity :O(1).
边栏推荐
- 【锟斤拷】的故事:谈谈汉字编码和常用字符集
- Study notes of Tutu - process
- (lightoj - 1354) IP checking (Analog)
- js时间函数大全 详细的讲解 -----阿浩博客
- Codeforces round 797 (Div. 3) no f
- Li Kou leetcode 280 weekly match
- 第6章 Rebalance详解
- Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
- Mp4 format details
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
猜你喜欢

Simply try the new amp model of deepfacelab (deepfake)

Chapter 5 yarn resource scheduler
![Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)](/img/92/9465a6c9f1ab88c4851a47fabe750c.jpg)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)

解决Intel12代酷睿CPU单线程只给小核运行的问题

SF smart logistics Campus Technology Challenge (no T4)

(lightoj - 1369) answering queries (thinking)

Install Jupiter notebook under Anaconda

Anaconda下安装Jupyter notebook

Chapter 6 rebalance details

Ffmpeg command line use
随机推荐
js时间函数大全 详细的讲解 -----阿浩博客
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
input 只能输入数字,限定输入
原生js实现全选和反选的功能 --冯浩的博客
MariaDB的安装与配置
Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
Click QT button to switch qlineedit focus (including code)
Chapter 7__ consumer_ offsets topic
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
Chapter 2 shell operation of hfds
Input can only input numbers, limited input
Codeforces Round #799 (Div. 4)A~H
Useeffect, triggered when function components are mounted and unloaded
Generate random password / verification code
Research Report on market supply and demand and strategy of Chinese table lamp industry
Codeforces Round #771 (Div. 2)
Codeforces round 797 (Div. 3) no f
业务系统从Oracle迁移到openGauss数据库的简单记录
(lightoj - 1349) Aladdin and the optimal invitation (greed)
SF smart logistics Campus Technology Challenge (no T4)