问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3746 访问。

在一个给定的数组nums中,总是存在一个最大元素 。

查找数组中的最大元素是否至少是数组中每个其他数字的两倍。

如果是,则返回最大元素的索引,否则返回-1。

输入: nums = [3, 6, 1, 0]

输出: 1

解释: 6是最大的整数, 对于数组中的其他整数,6大于数组中其他元素的两倍。6的索引是1, 所以我们返回1.

输入: nums = [1, 2, 3, 4]

输出: -1

解释: 4没有超过3的两倍大, 所以我们返回 -1.

提示:

nums 的长度范围在[1, 50].

每个 nums[i] 的整数范围在 [0, 99].


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.

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.

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:

nums will have a length in the range [1, 50].

Every nums[i] will be an integer in the range [0, 99].


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3746 访问。

public class Program {

    public static void Main(string[] args) {
int[] cost = null; cost = new int[] { 10, 15, 20 };
var res = DominantIndex(cost);
Console.WriteLine(res); Console.ReadKey();
} private static int DominantIndex(int[] nums) {
//记录最大值和索引,最大值也可以不要,只存索引
int max = int.MinValue;
int index = -1;
for(int i = 0; i < nums.Length; i++) {
if(nums[i] > max) {
max = nums[i];
index = i;
}
}
//i不用和自己比较,另外注意不要用除法,否则要处理数组中的0
for(int i = 0; i < nums.Length; i++) {
if(i != index && max < 2 * nums[i]) {
return -1;
}
}
return index;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3746 访问。

-1

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)的更多相关文章

  1. [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 ...

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

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

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

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

  4. LeetCode747 至少是其他数字两倍的最大数

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

  5. Leetcode747至少是其他数字两倍的最大数

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

  6. [LeetCode] Largest Number At Least Twice of Others 至少是其他数字两倍的最大数

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

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

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

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

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

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. nodejs之EventEmitter实现

    Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js 里面的许多对象都会分发事件:一个 net.Server 对象会在每次有新连接时触发一个事件, 一个 fs. ...

  2. python基础算法

    一.简介 定义和特征 定义:算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制.也就是说,能够对一定规范的输入,在有限时 ...

  3. k_means算法+python实现

    文章目录 一.原理 二.算法步骤 三.实例如下: 四.python代码实现: 一.原理 K均值算法使用的聚类准则函数是误差平方和准则,通过反复迭代优化聚类结果,使所有样本到各自所属类别的中心的距离平方 ...

  4. 设计模式:abstract factory模式

    含义:抽象工厂将“抽象零件”组装成“抽象产品” 理解:相比于工厂方法模式,可以根据不同的接口创建不同的产品,说白了就是将一个接口变成两个接口,各自返回不同的抽象产品 例子: class Car //抽 ...

  5. Linux内存参数

    用free -m查看的结果:# free -m         total    used    free     shared buffers     cachedMem:           50 ...

  6. REST是什么?RESTFul又是什么?这二者的关系是怎样的?

    REST(一种软件架构风格) 全称:Representational State Transfer 含义:(表述性 状态 转移) 是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可 ...

  7. leetcode 翻转字符串

    https://leetcode-cn.com/problems/reverse-words-in-a-string/ TLE代码: class Solution { public: string r ...

  8. Python按值传递参数和按引用传递参数

    Python按值传递参数和按引用传递参数: 按值传递参数: 使用一个变量的值(数字,字符串),放到实参的位置上 注:传递过去的是变量的副本,无论副本在函数中怎么变,变量的值都不变 传递常量: # 传递 ...

  9. == 和 is 的区别

    import copy a = ['a','b','c'] b = a #b和a引用自同一块地址空间 print("a==b :",a==b) print("a is b ...

  10. PHP filesize() 函数

    定义和用法 filesize() 函数返回指定文件的大小. 如果成功,该函数返回文件大小的字节数.如果失败,则返回 FALSE. 语法 filesize(filename) 参数 描述 filenam ...