LeetCode 1538. Guess the Majority in a Hidden Array

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

We have an integer array nums, where all the integers in nums are 0 or 1. You will not be given direct access to the array, instead, you will have an API ArrayReader which have the following functions:

  • int query(int a, int b, int c, int d): where 0 <= a < b < c < d < ArrayReader.length().
    The function returns the distribution of the value of the 4 elements and returns:
    4 : if the values of the 4 elements are the same (0 or 1).
    2 : if three elements have a value equal to 0 and one element has value equal to 1 or vice versa.
    0 : if two element have a value equal to 0 and two elements have a value equal to 1.
  • int length(): Returns the size of the array.
    You are allowed to call query() 2 * n times at most where n is equal to ArrayReader.length().

Return any index of the most frequent value in nums, in case of tie, return -1.

Follow up: What is the minimum number of calls needed to find the majority element?

Example 1:
Input: nums = [0,0,1,0,1,1,1,1]
Output: 5
Explanation: The following calls to the API
reader.length() 
// returns 8 because there are 8 elements in the hidden array.
reader.query(0,1,2,3) 
// returns 2 this is a query that compares the elements nums[0], nums[1], nums[2], nums[3]
// Three elements have a value equal to 0 and one element has value equal to 1 or viceversa.
reader.query(4,5,6,7) 
// returns 4 because nums[4], nums[5], nums[6], nums[7] have the same value.
we can infer that the most frequent value is found in the last 4 elements.
Index 2, 4, 6, 7 is also a correct answer.Example 2:
Input: nums = [0,0,1,1,0]
Output: 0Example 3:
Input: nums = [1,0,1,0,1,0,1,0]
Output: -1Constraints:
5 <= nums.length <= 10^5
0 <= nums[i] <= 1

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

2. 解题

  • 参考题解
/*** // This is the ArrayReader's API interface.* // You should not implement it, or speculate about its implementation* class ArrayReader {*   public:*     // Compares 4 different elements in the array*     // return 4 if the values of the 4 elements are the same (0 or 1).*     // return 2 if three elements have a value equal to 0 and one element has value equal to 1 or vice versa.*     // return 0 : if two element have a value equal to 0 and two elements have a value equal to 1.*     int query(int a, int b, int c, int d);**     // Returns the length of the array*     int length();* };*/class Solution {
public:int guessMajority(ArrayReader &reader) {int n = reader.length();int start = reader.query(0,1,2,3);int g1 = 1, g2 = 0, idx1 = 0, idx2 = -1;//假设idx = 0 的数是第一类,其个数为 g1 = 1for(int i = 4; i < n; ++i){if(reader.query(1,2,3,i)==start)//0, i 是否是一类g1++;elseg2++, idx2 = i;}//0 与 4,5...n-1 是否是一类//还要确定1,2,3int q = reader.query(0,2,3,4);int p = reader.query(1,2,3,4);if(q == p)//0和1是一类g1++;elseg2++, idx2 = 1;q = reader.query(0,1,3,4);// p = reader.query(1,2,3,4);if(q == p)//0和2是否是一类g1++;elseg2++,idx2 = 2;q = reader.query(0,1,2,4);// p = reader.query(1,2,3,4);if(q == p)//0和3是否是一类g1++;elseg2++,idx2 = 3;if(g1 == g2) return -1;if(g1 > g2) return idx1;return idx2;}
};

280 ms 31 MB


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

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

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

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

相关文章

html5怎么跟安卓交互,html5怎么与android交互

满意答案Android中构建HTML5应用 使用WebView控件,与其他控件的使用方法相同在layout中使用一个标签,WebView不包括导航栏&#xff0c;地址栏等完整浏览器功能&#xff0c;只用于显示一个html. 在WebView中加载Web页面: (1)注意在manifest文件中加入访问互联网的权限&#xff1…

Attempt to write to a read-only database Sqlite

使用WCF做了一个中间层服务&#xff0c;准备把数据同步到Sqlite数据库中&#xff0c;在本地测试的时候没有问题&#xff0c;但放到服务器上却报出了 标题的错误。 StackOverFlow上说是数据库文件的权限问题&#xff0c;于是我把Users的写入权限分配给Users组&#xff0c;问题解…

LeetCode 1258. 近义词句子(哈希+并查集+排序+回溯)

文章目录1. 题目2. 解题1. 题目 给你一个近义词表 synonyms 和一个句子 text &#xff0c; synonyms 表中是一些近义词对 &#xff0c;你可以将句子 text 中每个单词用它的近义词来替换。 请你找出所有用近义词替换后的句子&#xff0c;按 字典序排序 后返回。 示例 1&#…

2022年计算机考研学校,2022计算机考研院校推荐

2022计算机考研院校推荐2022考研复习已经如火如荼的展开了&#xff0c;但是依然有很多同学没有确定好目标院校。计算机是许多考研学生青睐的考研专业&#xff0c;那么选择什么院校成为了关键。为了方便大家更好的择校&#xff0c;新东方在线考计算机研小编下面整理了计算机专业…

LeetCode 1066. 校园自行车分配 II(状态压缩DP)

文章目录1. 题目2. 解题2.1 回溯超时2.2 状态压缩DP1. 题目 在由 2D 网格表示的校园里有 n 位工人&#xff08;worker&#xff09;和 m 辆自行车&#xff08;bike&#xff09;&#xff0c;n < m。所有工人和自行车的位置都用网格上的 2D 坐标表示。 我们为每一位工人分配一…

Bootstrap(二)—格栅系统!

W(A*n)-i W是一个页面的总宽度&#xff0c;比如950px&#xff1b;而A代表一个版块的宽度&#xff0c;设置为apx&#xff1b;n就是分为几个版块&#xff1b;而i就是区块之间的间隔。 例如 950&#xff08;a*24&#xff09;-10 而a40px&#xff1b;改变A和i的值就会生成不同的布…

python中的面向对象:类与对象(重点!!!)

Python中重点与难点&#xff0c;必须熟练掌握&#xff01;&#xff01;&#xff01; 一、面向对象 1、面向对象概念 面向对象(Object Oriented,OO)是软件开发方法&#xff0c;是一种思想。面向对象的对象是指客观世界存在的事物。 我们之前编程所熟悉的思路叫做面向过程&am…

计算机设备报废流程图,报废流程图.ppt

报废流程图球拍現場報廢流程圖 輪二現場報廢流程圖 輪一、安全現場報廢流程圖 航醫報廢流程圖 討論后(現場掃真實報廢) 討論后(品保掃真實報廢) 球拍、輪二報廢後臺標記 1、mail_index 0:簽核完成 1:掃真實報廢完 2:送簽核鎖定 3:駁回狀態 4:發mial完鎖定 2、FLAG(航醫沒有用到…

LeetCode 625. 最小因式分解(贪心)

文章目录1. 题目2. 解题1. 题目 给定一个正整数 a&#xff0c;找出最小的正整数 b 使得 b 的所有数位相乘恰好等于 a。 如果不存在这样的结果或者结果不是 32 位有符号整数&#xff0c;返回 0。 样例 1 输入&#xff1a; 48 输出&#xff1a; 68样例 2 输入&#xff1a; 15…

python面向对象中的类

1. 类的构成 类(Class) 由3个部分构成&#xff1a; 类的名称:类名&#xff1b; 类的属性:一组数据&#xff1b; 类的方法:允许对进行操作的方法 (行为) 列如,——人类的设计: 人类的设计我们只关心三样东西&#xff1a; 事物名称(类名):人(Person) 属性:身高(height)、年龄…

有符号数和无符号数的区别

C语言允许我们定义有符号数和无符号数&#xff0c;例如一个字节的存储空间&#xff0c;我们可以定义成unsigned char,也可以定义成signedchar&#xff0c;但是你知道它们的区别吗&#xff1f;你知道它们是怎样被存储&#xff0c;又怎样被区分处理吗&#xff1f; 1.有符号数和无…

u盘无法显示在计算机,插进电脑就是不认 不显示盘符的U盘是闹哪样?

唉&#xff0c;本来今天想和大家聊聊Windows 10春季创意者更新有啥好玩的东西&#xff0c;外加有哪些大坑需要注意和避免&#xff0c;结果微软跳票了&#xff0c;以下就省略下千字的小编内心独白吧。每次大版本的Windows更新对懒人或者对电脑来说&#xff0c;是个绝佳清理电脑的…

LeetCode 582. 杀死进程(图的遍历)

文章目录1. 题目2. 解题2.1 DFS2.2 BFS1. 题目 给 n 个进程&#xff0c;每个进程都有一个独一无二的 PID &#xff08;进程编号&#xff09;和它的 PPID &#xff08;父进程编号&#xff09;。 每一个进程只有一个父进程&#xff0c;但是每个进程可能会有一个或者多个孩子进程…

指向函数的指针 - C语言

欢迎访问我的新博客&#xff1a;http://www.milkcu.com/blog/ 原文地址&#xff1a;http://www.milkcu.com/blog/archives/1368947400.html 基本概念 在C语言中&#xff0c;函数本身不是变量&#xff0c;但可以定义指向函数的指针。这种类型的指针可以被赋值、存放在数组中、传…

python中的创建对象

创建对象 python中&#xff0c;可以根据已经定义的类去创建出一个个对象 创建对象的格式为: 对象名 类名() # 定义类 class Car&#xff08;object&#xff09;: # 移动 def move(self): print(车在奔跑...) # 鸣笛 def toot(self): …

计算机专业配置笔记本,学计算机专业的买什么样配置和价格的笔记本好?

学计算机专业的买什么样配置和价格的笔记本好&#xff1f;答案:5 信息版本&#xff1a;手机版解决时间 2021-05-10 08:17已解决2021-05-10 03:07学计算机专业的买什么样配置和价格的笔记本好&#xff1f;最佳答案2021-05-10 03:58二代i3 1G独显 2G内存 500G硬盘全部回答1楼202…

LeetCode 737. 句子相似性 II(并查集)

文章目录1. 题目2. 解题1. 题目 给定两个句子 words1, words2 &#xff08;每个用字符串数组表示&#xff09;&#xff0c;和一个相似单词对的列表 pairs &#xff0c;判断是否两个句子是相似的。 例如&#xff0c;当相似单词对是 pairs [["great", "fine&qu…

python中的_init_的使用

1. __init__方法 想一想:有没有办法能够在创建对象的时候&#xff0c;就顺便把车这个对象的属性给设置呢&#xff1f; 答:__init__()方法 使用方式&#xff1a; def 类名&#xff08;object&#xff09;: #初始化函数&#xff0c;用来完成一些默认的设定 def __in…