文章目录

  • 1. 题目
  • 2. 解题

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: 2Constraints:
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阿明),一起加油、一起学习进步!

LeetCode 1533. Find the Index of the Large Integer(二分查找)相关推荐

  1. LeetCode 410. 分割数组的最大值(极小极大化 二分查找 / DP)

    文章目录 1. 题目 2. 解题 2.1 二分查找 2.2 DP 1. 题目 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组. 设计一个算法使得这 m 个子数组各自和 ...

  2. LeetCode 875. 爱吃香蕉的珂珂(二分查找)

    1. 题目 珂珂喜欢吃香蕉.这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉.警卫已经离开了,将在 H 小时后回来. 珂珂可以决定她吃香蕉的速度 K (单位:根/小时).每个小时,她将会选 ...

  3. LeetCode LCP 12. 小张刷题计划(二分查找)

    1. 题目 为了提高自己的代码能力,小张制定了 LeetCode 刷题计划,他选中了 LeetCode 题库中的 n 道题,编号从 0 到 n-1,并计划在 m 天内按照题目编号顺序刷完所有的题目(注 ...

  4. LeetCode 1870. 准时到达的列车最小时速(二分查找)

    文章目录 1. 题目 2. 解题 2.1 模拟超时 2.2 二分查找 1. 题目 给你一个浮点数 hour ,表示你到达办公室可用的总通勤时间. 要到达办公室,你必须按给定次序乘坐 n 趟列车. 另给 ...

  5. LeetCode 1847. 最近的房间(排序离线计算 + 二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 一个酒店里有 n 个房间,这些房间用二维整数数组 rooms 表示,其中 rooms[i] = [roomIdi, sizei] 表示有一个房间号为 ro ...

  6. LeetCode 1760. 袋子里最少数目的球(二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数数组 nums ,其中 nums[i] 表示第 i 个袋子里球的数目.同时给你一个整数 maxOperations . 你可以进行如下操作至多 ...

  7. LeetCode 528. 按权重随机选择(前缀和+二分查找)

    文章目录 1. 题目 2. 解题 1. 题目 给定一个正整数数组 w ,其中 w[i] 代表下标 i 的权重(下标从 0 开始),请写一个函数 pickIndex ,它可以随机地获取下标 i,选取下标 ...

  8. LeetCode 1642. 可以到达的最远建筑(二分查找 / 优先队列贪心)

    文章目录 1. 题目 2. 解题 2.1 二分查找 2.2 优先队列+贪心 1. 题目 给你一个整数数组 heights ,表示建筑物的高度.另有一些砖块 bricks 和梯子 ladders . 你 ...

  9. LeetCode 778. 水位上升的泳池中游泳(二分查找+dfs)

    文章目录 1. 题目 2. 解题 1. 题目 在一个 N x N 的坐标方格 grid 中,每一个方格的值 grid[i][j] 表示在位置 (i,j) 的平台高度. 现在开始下雨了.当时间为 t 时 ...

最新文章

  1. 像童话一样学习OSPF原理
  2. sdut 2139BFS
  3. ROS知识:安装rosdep中出现time out的问题
  4. Oracle导入到不同的角色,oracle 不同版本之间的导入导出
  5. java的部署目录在哪里_Java:Tomcat的部署实例之资源目录
  6. javascript 内存回收机制
  7. python语言有哪些类型的运算符_python(4)-变量 数据类型和运算符
  8. [转人工智能工程师学习路线及具备的5项基本技能
  9. Redis(三)、支持数据类型及常用操作命令
  10. Linux测试磁盘能力
  11. NameNode DataNode
  12. Linux - 搭建LDAP统一认证服务
  13. Unity 事件番外篇:UnityEvent
  14. 笔记本Windows7无法连接上家庭wifi,急急急!!!
  15. 万亿级大数据监控平台建设实践
  16. Word排版技巧数模论文必备
  17. 喜欢的歌——隐形的翅膀(张韶涵)
  18. 探索永无止境 万洲金业荣膺GMCA第三届蝉鸣奖“年度最具创新力奖”
  19. 自己动手实现RAFT算法
  20. 洛谷P2015 二叉苹果树

热门文章

  1. python中错误和异常处理
  2. Python面向切面编程是什么
  3. Python 生成requirement 使用requirements.txt
  4. java clicked_关于java:JComponents在调用mouseClicked()之后消失
  5. ajax当页post请求,tag落地页--通过ajax-post请求数据
  6. 怎样王远端服务器上传文件,传王电子传真使用指南-Freefax传真服务器,传王A6,免费传真...
  7. typedef的使用方法
  8. gcc编译c文件的简单操作
  9. C++远征之封装篇——对象数组,对象成员
  10. ef sqlserver切换到mysql_可以为MySql和SqlServer使用EF上下文吗?