返回介绍

solution / 1000-1099 / 1060.Missing Element in Sorted Array / README_EN

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

1060. Missing Element in Sorted Array

中文文档

Description

Given an integer array nums which is sorted in ascending order and all of its elements are unique and given also an integer k, return the kth missing number starting from the leftmost number of the array.

 

Example 1:

Input: nums = [4,7,9,10], k = 1
Output: 5
Explanation: The first missing number is 5.

Example 2:

Input: nums = [4,7,9,10], k = 3
Output: 8
Explanation: The missing numbers are [5,6,8,...], hence the third missing number is 8.

Example 3:

Input: nums = [1,2,4], k = 3
Output: 6
Explanation: The missing numbers are [3,5,6,7,...], hence the third missing number is 6.

 

Constraints:

  • 1 <= nums.length <= 5 * 104
  • 1 <= nums[i] <= 107
  • nums is sorted in ascending order, and all the elements are unique.
  • 1 <= k <= 108

 

Follow up: Can you find a logarithmic time complexity (i.e., O(log(n))) solution?

Solutions

Solution 1

class Solution:
  def missingElement(self, nums: List[int], k: int) -> int:
    def missing(i: int) -> int:
      return nums[i] - nums[0] - i

    n = len(nums)
    if k > missing(n - 1):
      return nums[n - 1] + k - missing(n - 1)
    l, r = 0, n - 1
    while l < r:
      mid = (l + r) >> 1
      if missing(mid) >= k:
        r = mid
      else:
        l = mid + 1
    return nums[l - 1] + k - missing(l - 1)
class Solution {
  public int missingElement(int[] nums, int k) {
    int n = nums.length;
    if (k > missing(nums, n - 1)) {
      return nums[n - 1] + k - missing(nums, n - 1);
    }
    int l = 0, r = n - 1;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (missing(nums, mid) >= k) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return nums[l - 1] + k - missing(nums, l - 1);
  }

  private int missing(int[] nums, int i) {
    return nums[i] - nums[0] - i;
  }
}
class Solution {
public:
  int missingElement(vector<int>& nums, int k) {
    auto missing = [&](int i) {
      return nums[i] - nums[0] - i;
    };
    int n = nums.size();
    if (k > missing(n - 1)) {
      return nums[n - 1] + k - missing(n - 1);
    }
    int l = 0, r = n - 1;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (missing(mid) >= k) {
        r = mid;
      } else {
        l = mid + 1;
      }
    }
    return nums[l - 1] + k - missing(l - 1);
  }
};
func missingElement(nums []int, k int) int {
  missing := func(i int) int {
    return nums[i] - nums[0] - i
  }
  n := len(nums)
  if k > missing(n-1) {
    return nums[n-1] + k - missing(n-1)
  }
  l, r := 0, n-1
  for l < r {
    mid := (l + r) >> 1
    if missing(mid) >= k {
      r = mid
    } else {
      l = mid + 1
    }
  }
  return nums[l-1] + k - missing(l-1)
}

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

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

发布评论

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