LeetCode 1533. Find the Index of the Large Integer(二分查找)

文章目录

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474607.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java先抽到红球获胜,【图片】红蓝球概率问题,通过程序模拟抽取,计算结果已出,有兴趣来看【非现役文职吧】_百度贴吧...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼我用的c语言&#xff0c;大一学的还没忘完。。。。程序非常简单&#xff0c;就是生成随机数&#xff0c;然后根据随机数的结果进行计数就好了。代码贴下面&#xff0c;有兴趣的可以看看。懂行的请不要喷我写的烂。。。。。毕竟不是…

MySQL Server Architecture

MySQL 服务器架构&#xff1a; 转载于:https://www.cnblogs.com/macleanoracle/archive/2013/03/19/2968212.html

LeetCode MySQL 1479. 周内每天的销售情况(dayname星期几)

文章目录1. 题目2. 解题1. 题目 表&#xff1a;Orders ------------------------ | Column Name | Type | ------------------------ | order_id | int | | customer_id | int | | order_date | date | | item_id | varchar | | quantity …

php的swoole教程,PHP + Swoole2.0 初体验(swoole入门教程)

PHP Swoole2.0 初体验(swoole入门教程)环境&#xff1a;centos7 PHP7.1 swoole2.0准备工作&#xff1a;一、 swoole 扩展安装1 、下载swoolecd/usr/localwget -c https://github.com/swoole/swoole-src/archive/v2.0.8.tar.gztar -zxvf v2.0.8.tar.gzcdswoole-src-2.0.8/2 编…

Git常用命令解说

http://zensheno.blog.51cto.com/2712776/490748 1. Git概念 1.1. Git库中由三部分组成 Git 仓库就是那个.git 目录&#xff0c;其中存放的是我们所提交的文档索引内容&#xff0c;Git 可基于文档索引内容对其所管理的文档进行内容追踪&#xff0c;从而实现文档的版本控…

LeetCode MySQL 1412. 查找成绩处于中游的学生

文章目录1. 题目2. 解题1. 题目 表: Student ------------------------------ | Column Name | Type | ------------------------------ | student_id | int | | student_name | varchar | ------------------------------ student_id 是该表…

java jvm目录,JVM(Java虚拟机)中过程工作目录讲解

JVM(Java虚拟机)中进程工作目录讲解每次我们用Java命令运行我们的Java程序&#xff0c;都会在JVM中开启一个进程&#xff0c;对于每一个进程&#xff0c;都会有一个相对应的工作目录&#xff0c;这个工作目录在虚拟机初始化的时候就已经设置好了&#xff0c;默认的情况下&#…

Javascript 第七天 笔记

通过三天把以前学习的内容复习一下。以便继续学习。 BOM模型 BOM浏览器对象模型 DOM ----> document BOM -----> window Document对象其实是window的一个属性或叫子对象 Window对象的子对象介绍 Window.navigator : 表示浏览器的相关信息 Window.history : 历史记录,或者…

LeetCode MySQL 618. 学生地理信息报告(row_number)

文章目录1. 题目2. 解题1. 题目 一所美国大学有来自亚洲、欧洲和美洲的学生&#xff0c;他们的地理信息存放在如下 student 表中。 | name | continent | |--------|-----------| | Jack | America | | Pascal | Europe | | Xi | Asia | | Jane | Americ…

java非必填字段跳过校验,avalon2表单验证,非必填字段在不填写的时候不能通过验证...

avalon2表单验证,非必填字段在不填写的时候不能通过验证代码var vm avalon.define({$id: "validate1",aaa : "",validate: {onError: function(reasons) {reasons.forEach(function(reason) {console.log(reason.getMessage())})},onValidateAll: functio…

jQuery心得5--jQuery深入了解串讲1

1.CSS-DOM 操作 获取和设置元素的样式属性: css()。 获取和设置元素透明度: opacity 属性(css 的一个属性)。 获取和设置元素高度, 宽度: height(), width(). 在设置值时, 若只传递数字, 则默认单位是 px. 如需要使用其他单位则需传递一个字符串, 例如 $(“p:first”).height(“…

LeetCode MySQL 1225. 报告系统状态的连续日期(date_sub + over)

文章目录1. 题目2. 解题1. 题目 Table: Failed ----------------------- | Column Name | Type | ----------------------- | fail_date | date | ----------------------- 该表主键为 fail_date。 该表包含失败任务的天数.Table: Succeeded --------------------…

mysql tableveiw与表格,javafx将数据库内容输出到tableview表格

一 、创建Fxml文件&#xff0c;用Javafx Scene Builder 编辑页面&#xff0c;创建tableview(表格)和tablecolum(表格中的列)&#xff0c;并为其设置fxid&#xff1b;二、生成fxml文件的控制类&#xff1b;三、创建数据库的连接类(使用JDBC驱动)&#xff1b;Connect.javaimportj…

UVa 11636 Hello World!

#include<cstdio>#include<cmath>using namespace std;int main(){int n,T1;while(scanf("%d",&n),n>0){printf("Case %d: %d\n",T,(int)ceil(log10(n)/log10(2))); //换底公式}return 0;}转载于:https://www.cnblogs.com/LtRoycePhan…

php mysqliquery 返回值,PHP mysqli_multi_query() 函数_程序员人生

实例执行多个针对数据库的查询&#xff1a;$conmysqli_connect("localhost","my_user","my_password","my_db");// Check connectionif (mysqli_connect_errno($con)){echo "Failed to connect to MySQL: " . mysqli_connec…

LeetCode MySQL 1369. 获取最近第二次的活动(over窗口函数)

文章目录1. 题目2. 解题1. 题目 表: UserActivity ------------------------ | Column Name | Type | ------------------------ | username | varchar | | activity | varchar | | startDate | Date | | endDate | Date | -----------------…

侧边导航栏的实现

F:\java\c侧边栏\侧边菜单栏\MySlidingMenu转载于:https://www.cnblogs.com/ct732003684/archive/2013/03/24/2979416.html

签到 数据库php,php与数据库的连接用法 (签到一)

注册页面//插入js验证window.onload function(){var hid document.getElementById("id");if(hid.value !""){ //当用户名已存在数据库时&#xff0c;提示用户已注册alert("用户名已注册");}}//ht…

LeetCode MySQL 569. 员工薪水中位数(over窗口函数)

文章目录1. 题目2. 解题1. 题目 Employee 表包含所有员工。Employee 表有三列&#xff1a;员工Id&#xff0c;公司名和薪水。 ------------------------- |Id | Company | Salary | ------------------------- |1 | A | 2341 | |2 | A | 341 …

【IEnumerable】扩展方法的使用 C#

直接进入主题吧... IEnumerable : 公开枚举数&#xff0c;该枚举数支持在非泛型集合上进行简单迭代。 好吧&#xff0c;迭代&#xff0c;我就理解成循环&#xff0c;这些名词真晦涩&#xff0c;没意思 今天看的是 Using Extension Methods &#xff0c;使用"扩展方法…