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


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


相关推荐

  • soft thresholding and hard thresholding

    soft thresholding and hard thresholding今天在看MichaelElad大牛的论文《OntheRoleofSparseandRedundantRepresentationsinImageProcessing》中,看到了softthresholding和hardthresholding这两个概念,很是不明白,所以上网查了一些资料,先把网上查的东西贴出来讨论下。网上资料大体是说这是指两类的函数,分别

    2022年6月8日
  • mfc怎么把cstring转换成int(string型转化为int型)

       m_ed.GetWindowTextW(str);  char*time;  time=(char*)(LPCTSTR)str;  inttiti;  titi=atoi(time); 网上搜索了很久.也没有找到好的转换 然后我偶然的试了这个方法..没想到可以成功转换==#  

    2022年4月11日
  • 004-protostuff踩坑-java bean新增字段反序列化失败问题

    004-protostuff踩坑-java bean新增字段反序列化失败问题protostuff避免更改java对象字段,比如新增一个,导致redis等缓存的数据反序列化失败问题??问题重现:我们有个方法通过attrKey查询List,同时方法中有缓存,会优先查询缓存,没有读库,然后写缓存返回。方法伪代码如下:publicvoidsetId(StringattrKey){//从缓存查询List<ConfAttr>attrValues=cacheClient.get(attrKey);

    2022年5月7日
  • #2006 – MySQL server has gone away 问题解决方法

    #2006 – MySQL server has gone away 问题解决方法

    2021年11月23日
  • 医疗器械进销存软件管理系统_简单仓库管理系统

    医疗器械进销存软件管理系统_简单仓库管理系统开发环境:Eclipse/MyEclipse、Tomcat8、Jdk1.8数据库:MySQL适用于:课程设计,毕业设计,学习等等系统介绍有偿

    2022年9月4日
  • 图像处理入门教程[通俗易懂]

    图像处理入门教程[通俗易懂]  最近有人问我图像处理怎么研究,怎么入门,怎么应用,我竟一时语塞。仔细想想,自己也搞了两年图像方面的研究,做个两个创新项目,发过两篇论文,也算是有点心得,于是总结总结和大家分享,希望能对大家有所帮助。在写这篇教程之前我本想多弄点插图,让文章看起来花哨一点,后来我觉得没必要这样做,大家花时间沉下心来读读文字没什么不好,况且学术和技术本身也不是多么花哨的东西。  一、图像处理的应用  这个其实没什么…

    2022年5月17日

发表回复

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

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