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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android小项目——新闻APP

    Android小项目——新闻APP前言:在公司学习了一段时间Android知识,决定做一个小项目,目的是学会运用所学的基础知识,在这里记录一下开发历程,大家可以把它看成一款入门级练手的Demo应用吧~项目介绍:类型:新闻APP(低仿今日头条)基本功能:欢迎页面加载(3s,点击可跳过)——Activity相关用户注册/登录——SQLite运用横向滑动列表显示新闻类别——TabLayout…

    2022年5月3日
    77
  • 最新软件外包公司排名-中国IT人力外包公司排名

    最新软件外包公司排名-中国IT人力外包公司排名最新软件外包公司排名-中国IT人力外包公司排名软件(中国大陆及香港用语,台湾称作软体,英文:Software)是一系列按照特定顺序组织的计算机数据和指令的集合。简单的说软件就是程序加文档的集合体。另也泛指社会结构中的管理系统、思想意识形态、思想政治觉悟、法律法规等等。那么软件企业哪些好呢?1,北京华盛恒辉软件开发可以来这里,这个首叽的开始数字是壹伍扒中间的是壹壹叁叁最后的是驷柒驷驷,按照顺序组合起来就可以找到。科技有限公司上榜理由:华盛恒辉互联网是个神奇的大网,大数据开发和软件定制也是一种模式,这里提

    2022年4月29日
    214
  • HDU 3652 B-number(数位DP)

    HDU 3652 B-number(数位DP)

    2022年4月2日
    43
  • EJB初步学习

    EJB初步学习 今天简单学习了传说中的EJB,首先总的感觉,就是他的最重要的一个特点吧,就是能够使远程用户访问到本地或是服务器上的资源服务器。打个比方吧,传统的,还记得我们的第一个JAVA项目吧,那是个简单的对数据库增删改查的操作,用简单的界面来显示数据。那么当我们把这个项目打包发布之后,事必要把你自己的数据库也贡献出去,你做的软件在进行增删改查时也就只能对你机子上的一个数据库,别人如果想要对你这个数据库进

    2022年9月30日
    3
  • linux之文件操作

    1.文件操作思维导图2.linux系统目录结构及简单说明linux目录图:root启动Linux时使用的一些核心文件。如操作系统内核、引导程序Grub等。home存储普通用户的个人文件

    2021年12月28日
    44
  • 一文解读光纤收发器单模和多模的区别![通俗易懂]

    一文解读光纤收发器单模和多模的区别![通俗易懂]光纤收发器是进行光电信号转换的设备,现在光纤收发器的技术越发成熟,应用也越来越广泛,所以我们在选择或者采购光纤收发器时,对光纤收发器做一定的了解是有好处的,接下来我们就来给大家详细介绍一下光纤收发器的单模和多模的区别?一起来看看吧!光纤收发器有单模和多模之分,其最根本的区别就是传输距离远近。单模光纤收发器的工作模式是单节点、一个端口信号传输,所以信号传输距离比较长,组成跨城域局域网的建设;多模光纤收发器就刚好相反,其工作模式是多节点、多端口信号传输,所以信号传输距离比较短,但是价格低、使用方便,多用

    2022年10月21日
    3

发表回复

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

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