In a given integer array nums, there is always exactly one largest element.

Find whether the largest element in the array is at least twice as much as every other number in the array.

If it is, return the index of the largest element, otherwise return -1.

Example 1:

Input: nums = [3, 6, 1, 0]
Output: 1
Explanation: 6 is the largest integer, and for every other number in the array x,
6 is more than twice as big as x. The index of value 6 is 1, so we return 1.

Example 2:

Input: nums = [1, 2, 3, 4]
Output: -1
Explanation: 4 isn't at least as big as twice the value of 3, so we return -1.

Note:

  1. nums will have a length in the range [1, 50].
  2. Every nums[i] will be an integer in the range [0, 99].

这道题让我们找一个至少是其他数字两倍的最大数字,那么我们想,首先明确的一点是这个要求的数字一定是数组中的最大数字,因为其是其他所有的数字的至少两倍。然后就是,如果该数字是数组中第二大的数字至少两倍的话,那么它一定是其他所有数字的至少两倍,所以我们可以遍历一次数组分别求出最大数字和第二大数字,然后判断一下最大数字是否是第二大数字的两倍即可,注意这里我们判断两倍的方法并不是直接相除,为了避免除以零的情况,我们采用减法,参见代码如下:

解法一:

class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, secondMx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] > mx) {
secondMx = mx;
mx = nums[i];
mxId = i;
} else if (nums[i] > secondMx) {
secondMx = nums[i];
}
}
return (mx - secondMx >= secondMx) ? mxId : -;
}
};

当然我们也可以使用更straightforward的方法,首先遍历一遍数组找出最大数字,然后再遍历一遍数组,验证这个数字是否是其他数字的至少两倍,参见代码如下:

解法二:

class Solution {
public:
int dominantIndex(vector<int>& nums) {
int mx = INT_MIN, mxId = ;
for (int i = ; i < nums.size(); ++i) {
if (mx < nums[i]) {
mx = nums[i];
mxId = i;
}
}
for (int num : nums) {
if (mx != num && mx - num < num) return -;
}
return mxId;
}
};

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数的更多相关文章

  1. Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数

    在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...

  2. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  3. [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  4. [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)

    ①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...

  5. Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)

    747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...

  6. C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3746 访问. 在一个给定的数组nums中,总是存在一个最大元素 ...

  7. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. Leetcode Largest Number c++ solution

    Total Accepted: 16020 Total Submissions: 103330     Given a list of non negative integers, arrange t ...

  9. LeetCode: Largest Number 解题报告 以及Comparator, CompareTo 应用

    Largest Number Given a list of non negative integers, arrange them such that they form the largest n ...

随机推荐

  1. Java注解(1)-注解基础

    注解(Annotation)是在JAVA5中开始引入的,它为在代码中添加信息提供了一种新的方式.注解在一定程度上把元数据与源代码文件结合在一起,正如许多成熟的框架(Spring)所做的那样.那么,注解 ...

  2. [Scala] 安装及环境配置(图文)

    Window 上安装配置 1.Java(JDK)环境配置,详见 Java(JDK)环境 2.从 Scala 官网下载安装包:https://downloads.lightbend.com/scala/ ...

  3. [学习笔记] 模拟退火 (Simulated Annealing)

    真没想到这东西真的在考场上用到了...顺便水篇blog以示诈尸好了(逃 模拟退火算法 模拟退火是一种随机化算法, 用于求函数的极值qwq 比如给出一个问题, 我们要求最优解的值, 但是可能的方案数量极 ...

  4. python全栈学习--day5

    字典 特点:字典是python中唯一的映射类型,采用键值对(key-value) 的形式存数据. 存储大量的数据,是关系型数据,查询数据快. 字典初始说明: 遍历字典从列表开始,列表是从头便利到尾的. ...

  5. Leetcode 19——Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. JavaScript(第二十一天)【DOM元素尺寸和位置】

    学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解.   一.获取元素CSS大小 ...

  7. C语言程序设计(基础)- 第14、15周作业

    从本周开始,将作业标记为学校自然周,而不是开课的周数. 要求一(25经验值) 完成14.15周的所有PTA中题目集. 注意1:一周两次pta作业,包括四次. 要求二(50经验值) 博客的具体书写内容和 ...

  8. Beta集合

    Beta冲刺day1 Beta冲刺day2 Beta冲刺day3 Beta冲刺day4 Beta冲刺day5 Beta冲刺day6 Beta冲刺day7 测试总结 总结合集 Beta预备

  9. Tornado介绍及自定义组件

    Tornado 的性能是相当优异的,因为它试图解决一个被称之为"C10k"问题,就是处理大于或等于一万的并发.一万呀,这可是不小的量 条件:处理器为 AMD Opteron, 主频 ...

  10. DML数据操作语言之增加,删除,更新

    1.数据的增加 数据的增加要用到insert语句  ,基本格式是: insert into <表名> (列名1,列名2,列名3,......) values (值1,值2,值3,..... ...