返回介绍

solution / 1500-1599 / 1558.Minimum Numbers of Function Calls to Make Target Array / README_EN

发布于 2024-06-17 01:03:18 字数 3702 浏览 0 评论 0 收藏 0

1558. Minimum Numbers of Function Calls to Make Target Array

中文文档

Description

You are given an integer array nums. You have an integer array arr of the same length with all values set to 0 initially. You also have the following modify function:

You want to use the modify function to convert arr to nums using the minimum number of calls.

Return _the minimum number of function calls to make _nums_ from _arr.

The test cases are generated so that the answer fits in a 32-bit signed integer.

 

Example 1:

Input: nums = [1,5]
Output: 5
Explanation: Increment by 1 (second element): [0, 0] to get [0, 1] (1 operation).
Double all the elements: [0, 1] -> [0, 2] -> [0, 4] (2 operations).
Increment by 1 (both elements)  [0, 4] -> [1, 4] -> [1, 5] (2 operations).
Total of operations: 1 + 2 + 2 = 5.

Example 2:

Input: nums = [2,2]
Output: 3
Explanation: Increment by 1 (both elements) [0, 0] -> [0, 1] -> [1, 1] (2 operations).
Double all the elements: [1, 1] -> [2, 2] (1 operation).
Total of operations: 2 + 1 = 3.

Example 3:

Input: nums = [4,2,5]
Output: 6
Explanation: (initial)[0,0,0] -> [1,0,0] -> [1,0,1] -> [2,0,2] -> [2,1,2] -> [4,2,4] -> [4,2,5](nums).

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def minOperations(self, nums: List[int]) -> int:
    return sum(v.bit_count() for v in nums) + max(0, max(nums).bit_length() - 1)
class Solution {
  public int minOperations(int[] nums) {
    int ans = 0;
    int mx = 0;
    for (int v : nums) {
      mx = Math.max(mx, v);
      ans += Integer.bitCount(v);
    }
    ans += Integer.toBinaryString(mx).length() - 1;
    return ans;
  }
}
class Solution {
public:
  int minOperations(vector<int>& nums) {
    int ans = 0;
    int mx = 0;
    for (int v : nums) {
      mx = max(mx, v);
      ans += __builtin_popcount(v);
    }
    if (mx) ans += 31 - __builtin_clz(mx);
    return ans;
  }
};
func minOperations(nums []int) int {
  ans, mx := 0, 0
  for _, v := range nums {
    mx = max(mx, v)
    for v > 0 {
      ans += v & 1
      v >>= 1
    }
  }
  if mx > 0 {
    for mx > 0 {
      ans++
      mx >>= 1
    }
    ans--
  }
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文