首页 > 其他分享 >LeetCode 1539. Kth Missing Positive Number

LeetCode 1539. Kth Missing Positive Number

时间:2024-04-05 10:46:08浏览次数:26  
标签:arr missing int Positive Missing mid Number positive integer

原题链接在这里:https://leetcode.com/problems/kth-missing-positive-number/description/

题目:

Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

Return the kth positive integer that is missing from this array.

Example 1:

Input: arr = [2,3,4,7,11], k = 5
Output: 9
Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.

Example 2:

Input: arr = [1,2,3,4], k = 2
Output: 6
Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.

Constraints:

  • 1 <= arr.length <= 1000
  • 1 <= arr[i] <= 1000
  • 1 <= k <= 1000
  • arr[i] < arr[j] for 1 <= i < j <= arr.length

Follow up:

Could you solve this problem in less than O(n) complexity?

题解:

Assume x is the final result, from 1 to x, there are m numbers not missing.

Try to find the m position.

Use the binary search, arr[mid] - mid - 1 is the missing numbers up to index mid.

if it is smaller than k, than m must be on the right, l = mid + 1. 

else, than m could on the left including current m. r = m.

Note: initialize r as arr.length.

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

Space: O(1).

AC Java:

 1 class Solution {
 2     public int findKthPositive(int[] arr, int k) {
 3         int l = 0;
 4         int r = arr.length;
 5         while(l < r){
 6             int mid = l + (r - l) / 2;
 7             if(arr[mid] - mid  - 1 < k){
 8                 l = mid + 1;
 9             }else{
10                 r = mid;
11             }
12         }
13 
14         return l + k;
15     }
16 }

 

标签:arr,missing,int,Positive,Missing,mid,Number,positive,integer
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18115546

相关文章

  • 挑战程序设计竞赛 2.6章习题 UVA - 10006 Carmichael Numbers
    https://vjudge.csgrandeur.cn/problem/UVA-10006当今计算机科学的一个重要的领域就是密码学。有些人甚至认为密码学是计算机科学中唯一重要的领域,没有密码学生命都没有意义。阿尔瓦罗就是这样的一个人,它正在设计一个为西班牙杂烩菜饭加密的步骤。他在加密算法中应用了一......
  • [ABC211D] Number of Shortest paths 题解
    [ABC211D]NumberofShortestpaths题解思路解析题目其实说得很明白了,就是最短路计数。我们可以用一个\(s_i\)表示从起点到\(i\)的最短路计数,然后进行bfs,由于边权为\(1\),所以可以使用bfs求最短路。如果\(u\tov\)是\(v\)的最短路的其中一段,就把\(s_u\tos_v\)......
  • Hive的row_number和regexp_extract结合带来的乱码问题
    selectuserid,from_unixtime(createtime,'yyyy-MM-dd')asdateid,regexp_extract(browser,'^([^\\(]*).*$',1)asbrowser,operationsystem,device,row_number()over......
  • Enumerating Rational Numbers 题解
    EnumeratingRationalNumbers题解先下结论,这道题是一道欧拉函数板子题观察题面可以发现,生成的分数有如下特性:分数都是最简分数分母与分子互质,且分子$\le$分母当然第一个除外,那个特判即可,不用纳入考虑范围我们知道,对于任意正整数n,欧拉函数,即\(\varphi(n)\)是小......
  • js的Number对象和全局对象
    文章目录1.Number对象1.1.含义1.2.属性1.3.方法2.全局对象2.1.含义2.2.特点2.3.属性2.4.方法3.函数的本质1.Number对象1.1.含义Number对象是原始数值的包装对象。constnum=2.334;constobj=newNumber(num);console.log(obj);//Numberco......
  • (day 22)JavaScript学习笔记(内置对象1之Number、Math、Date)
    概述         这是我的学习笔记,记录了JavaScript的学习过程。在写博客的时候我会尽量详尽的记录每个知识点。如果你完全没接触过JavaScript,那么这一系列的学习笔记可能会对你有所帮助。    今天学习JavaScript内置的对象,主要是Number、Math、Date。1.内置......
  • 获取中国周的自定义函数 GetChinaWeekNumber
    报表开发,无意发现SQLServer数据库计算周跟中国周有一点不一样,一般来讲,如果新年的1月1日开始落在的周不满4天,就需要把这几天归集到上一年的周,中国周是从周一~周日,国外的是周日~周六,所以中西方周有点不一样(网上说还有闰年不一样,我没有深入了解,先了解大概,有错误请忽喷,可以用下面的函......
  • PHP round 和number_format 区别
    number_format和round有什么区别?number_format()和round()在PHP中的底层计算方法并不完全一样,主要体现在它们的目的和结果表现形式上的差异,同时也可能影响到精度:round()函数主要用于对浮点数进行数学意义上的四舍五入,它的重点在于数值本身的精度调整。该函数接收两个参......
  • LeetCode-9 Palindrome Number
    9.PalindromeNumber easyGivenaninteger x,return true if x isa palindrome ,and false otherwise. Example1:Input:x=121Output:trueExplanation:121readsas121fromlefttorightandfromrighttoleft.Example2:Input:x=-......
  • elementui组件el-input 类型为number时,去掉上下箭头,并且解决输入中文后光标上移问题
    //去掉number输入框的上下箭头.def-input-numberinput::-webkit-outer-spin-button,.def-input-numberinput::-webkit-inner-spin-button{-webkit-appearance:none;}.def-input-numberinput[type="number"]{-moz-appearance:textfield;}//解决inputnumber框......