LeetCode 1060. Missing Element in Sorted Array

原题链接在这里:https://leetcode.com/problems/missing-element-in-sorted-array/

题目:

Given a sorted array A of unique numbers, find the K-th missing number starting from the leftmost number of the array.

Example 1:

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

Example 2:

Input: A = [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: A = [1,2,4], K = 3
Output: 6
Explanation:
The missing numbers are [3,5,6,7,...], hence the third missing number is 6.

Note:

  1. 1 <= A.length <= 50000
  2. 1 <= A[i] <= 1e7
  3. 1 <= K <= 1e8

题解:

If the missing numbers count < k, then missing number must be after nums[n-1].

res = nums[n-1] + missingCount.

Otherwise, need to find out the starting index to calculate the missing number.

Use binary search to have mid as candidate.

Now missing numbers count between numd[l] and nums[mid] is nums[mid] - nums[l]-(mid-l).

If this count >= k, then starting index must not fall into (l, r], including r.

Otherwise, starting index fall into right side. But need to update k as k-missingCount.

Time Complexity: O(logn). n = nums.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int missingElement(int[] nums, int k) {
 3         int n = nums.length;
 4         if(nums[n-1]-nums[0]+1-n < k){
 5             return nums[n-1] + k-(nums[n-1]-nums[0]+1-n);
 6         }
 7
 8         int l = 0;
 9         int r = n-1;
10         while(l<r-1){
11             int mid = l + (r-l)/2;
12             int missingCount = nums[mid]-nums[l]-(mid-l);
13             if(missingCount >= k){
14                 // When missingCount <= k, starting index to calculate must not fall into (l, r].
15                 r = mid;
16             }else{
17                 l = mid;
18                 k -= missingCount;
19             }
20         }
21
22         return nums[l]+k;
23     }
24 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11902286.html

时间: 2024-11-08 22:53:31

LeetCode 1060. Missing Element in Sorted Array的相关文章

Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target v

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Given a sorted array of n integers, find the starting and ending position of a given target va

[leetcode][34] Find First and Last Position of Element in Sorted Array

34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n)

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill