LeetCode 1533. Find the Index of the Large Integer(二分查找)「建议收藏」

LeetCode 1533. Find the Index of the Large Integer(二分查找)「建议收藏」文章目录1.题目2.解题1.题目Wehaveanintegerarrayarr,wherealltheintegersinarrareequalexceptforoneintegerwhichislargerthantherestoftheintegers.Youwillnotbegivendirectaccesstothearray,instead,youwillhaveanAPIArrayReaderwhi

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

文章目录

1. 题目

We have an integer array arr, where all the integers in arr are equal except for one integer which is larger than the rest of the integers. You will not be given direct access to the array, instead, you will have an API ArrayReader which have the following functions:

  • int compareSub(int l, int r, int x, int y): where 0 <= l, r, x, y < ArrayReader.length(), l <= r and x <= y. The function compares the sum of sub-array arr[l…r] with the sum of the sub-array arr[x…y] and returns:
    1 if arr[l]+arr[l+1]+...+arr[r] > arr[x]+arr[x+1]+...+arr[y].
    0 if arr[l]+arr[l+1]+...+arr[r] == arr[x]+arr[x+1]+...+arr[y].
    -1 if arr[l]+arr[l+1]+...+arr[r] < arr[x]+arr[x+1]+...+arr[y].
  • int length(): Returns the size of the array.

You are allowed to call compareSub() 20 times at most. You can assume both functions work in O(1) time.

Return the index of the array arr which has the largest integer.

Follow-up:

  • What if there are two numbers in arr that are bigger than all other numbers?
  • What if there is one number that is bigger than other numbers and one number that is smaller than other numbers?
Example 1:
Input: arr = [7,7,7,7,10,7,7,7]
Output: 4
Explanation: The following calls to the API
reader.compareSub(0, 0, 1, 1) 
// returns 0 this is a query comparing the sub-array (0, 0) with the sub array (1, 1), (i.e. compares arr[0] with arr[1]).
Thus we know that arr[0] and arr[1] doesn't contain the largest element.
reader.compareSub(2, 2, 3, 3) 
// returns 0, we can exclude arr[2] and arr[3].
reader.compareSub(4, 4, 5, 5) 
// returns 1, thus for sure arr[4] is the largest element in the array.
Notice that we made only 3 calls, so the answer is valid.

Example 2:
Input: nums = [6,6,12]
Output: 2
 
Constraints:
2 <= arr.length <= 5 * 10^5
1 <= arr[i] <= 100
All elements of arr are equal except for one element which is larger than all other elements.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-the-index-of-the-large-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

/** * // This is the ArrayReader's API interface. * // You should not implement it, or speculate about its implementation * class ArrayReader { * public: * // Compares the sum of arr[l..r] with the sum of arr[x..y] * // return 1 if sum(arr[l..r]) > sum(arr[x..y]) * // return 0 if sum(arr[l..r]) == sum(arr[x..y]) * // return -1 if sum(arr[l..r]) < sum(arr[x..y]) * int compareSub(int l, int r, int x, int y); * * // Returns the length of the array * int length(); * }; */

class Solution { 
   
public:
    int getIndex(ArrayReader &reader) { 
   
        int n = reader.length(), l = 0, r = n-1, midl, midr;
        int flag;
        while(l < r)
        { 
   
        	if((r-l)&1)//差为奇数
        		midl = (l+r)/2,
        		midr = (l+r)/2+1;
        	else//偶数
        		midl = midr = (l+r)/2;
        	flag = reader.compareSub(l, midl, midr,r);
        	if(flag > 0)//左边和大
        		r = midl;
        	else if(flag < 0)//右边和大
        		l = midr;
        	else//相等找到了
        		return midl;
        }
        return l;
    }
};

200 ms 39.7 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Spring的IOC和AOP原理

    Spring的IOC和AOP原理Spring的IOC和AOP原理本文讲的是面试之Spring框架IOC和AOP的实现原理,IoC(InversionofControl)(1).IoC(InversionofControl)是指容器控制程序对象之间的关系,而不是传统实现中,由程序代码直接操控。控制权由应用代码中转到了外部容器,控制权的转移是所。IoC(InversionofControl)(1).IoC(InversionofControl)是指容器控制程序对象之间的关系,而不是传统实现中,由程序代码直接操控。

    2022年6月22日
    33
  • JAVA常见知识

    JAVA常见知识JAVA常见知识

    2022年4月24日
    30
  • 韩顺平的php东方航空_韩顺平PHP从入门到精通视频教程[通俗易懂]

    韩顺平的php东方航空_韩顺平PHP从入门到精通视频教程[通俗易懂]传智播客_韩顺平_php从入门到精通视频教程第001讲html介绍html运行原理①传智播客_韩顺平_php从入门到精通视频教程第002讲html运行原理②html文件基本结构html元素和属性传智播客_韩顺平_php从入门到精通视频教程第003讲符号实体url说明超链接发送电邮传智播客_韩顺平_php从入门到精通视频教程第004讲图像表格实际应用-菜谱课…

    2025年6月30日
    0
  • 二级反渗透1T/H二级反渗透纯水机 纯净水反渗透设备 反渗透设备

    反渗透技术原理反渗透技术是美国六十年代后期为解决宇航员在太空的饮水问题而研制的高新技术,也是目前的膜分离技术。简单地说,反渗透装置是利用半透膜在压力差的作用下使含盐水脱盐纯化的设备,它能有效地去除水中的无机盐、细菌、病毒、色素、热源、重金属离子及农药、化肥、清洁剂、胶体物质等污染物。反渗透膜孔径非常小,一般在2-10埃左右,而水中的各种离子杂质的直径约为几十埃,病毒、细菌的直径为几百至几十万埃,因此这些物质都是无法透过反渗透膜的,被截止在膜的浓水端,随浓水排出,透过反渗透膜的即是无菌,无毒害且富氧的纯净

    2022年4月17日
    45
  • virus.win32.parite.h病毒查杀

    virus.win32.parite.h病毒查杀virus.win32.parite.h病毒查杀第一步,病毒不会无缘无故的出现,一般是有病毒下载器(通常蛰伏在流氓软件中),或者是有后门病毒将这些病毒下载下来。用优化大师或金山或360都行,流氓软件清除工具(360插件扫描和***查杀中部分选项,注意不要一下子处理威胁,要选择流氓软件、后门项目处理)第二步,要下载两个专杀(绿盟有打包的,也可以自己一个个找)1、北信源Win32…

    2022年7月25日
    7
  • 浅析Python优势所在

    浅析Python优势所在浅析Python优势所在 Python优势的最大有点就是比其他语言更简单易学,功能强大的解释型编程语言,它有简洁明了的语法,高效率的高层数据结构,能够简单而有效地实现面向对象编程,欢迎大家学习参考。如果你仅仅认为用Python优势只能写写“HelloWorld”,那你就大错特错了。Python可以被应用到网络开发、GUI开发、图形开发、Web开发、游戏开发、手机

    2022年10月4日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号