当前位置:网站首页>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).
边栏推荐
- FLV格式详解
- Cmake Express
- Spark独立集群动态上线下线Worker节点
- LeetCode 1447. Simplest fraction
- Audio and video development interview questions
- input 只能输入数字,限定输入
- 拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
- Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
- 我在字节跳动「修电影」
- Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
猜你喜欢

图像处理一百题(1-10)

解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)

Base dice (dynamic programming + matrix fast power)

The concept of spark independent cluster worker and executor

第五章 Yarn资源调度器

antd upload beforeUpload中禁止触发onchange

Local visualization tools are connected to redis of Alibaba cloud CentOS server

【锟斤拷】的故事:谈谈汉字编码和常用字符集

Chapter 6 datanode

第三章 MapReduce框架原理
随机推荐
Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
Codeforces Round #802(Div. 2)A~D
Codeforces Round #798 (Div. 2)A~D
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
SQL quick start
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Chapter 6 rebalance details
QT implementation window gradually disappears qpropertyanimation+ progress bar
Codeforces Global Round 19
Simply try the new amp model of deepfacelab (deepfake)
Acwing: the 56th weekly match
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
简单尝试DeepFaceLab(DeepFake)的新AMP模型
One hundred questions of image processing (1-10)
Input can only input numbers, limited input
(lightoj - 1369) answering queries (thinking)
QT implementation fillet window
Bidirectional linked list - all operations