问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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不用和自己比较,另外注意不要用除法,否则要处理数组中的0for(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. 【快乐水题】747. 至少是其他数字两倍的最大数

    原题: 力扣链接:747. 至少是其他数字两倍的最大数 题目简述: 给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 . 请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字 ...

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

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

  3. php 最大数字,PHP 计算至少是其他数字两倍的最大数的实现代码

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

  4. LeetCode简单题之至少是其他数字两倍的最大数

    题目 给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 . 请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 .如果是,则返回 最大元素的下标 ,否则返回 -1 . ...

  5. 【leetcode-数组】至少是其他数字两倍的最大数

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

  6. LintCode 1053. 至少是其他数字两倍的最大数 JavaScript算法

    描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 说明 nums 的长度范围在[1, ...

  7. C#LeetCode刷题-数组

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

  8. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

  9. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

最新文章

  1. yolo_model to output理解
  2. 深入研究.NET 5的开放式遥测
  3. QT:MainWindow初始化列表解释
  4. 使用POI读写word docx文件
  5. php markdown 文章目录,markdown 文本内跳转,生成目录
  6. Bartender模板三种动态图片设置方法(仅供参考)
  7. MATLAB零基础入门教程视频课程
  8. 我的世界服务器内无限刷物品,我的世界怎么无限刷物品
  9. 我所理解的协方差矩阵
  10. html5 图片命名,html5 css命名规范
  11. 拔丝芋头的Java学习日记--Day1
  12. 人工智能聊天机器人(有详细安装教程)/ 自动学习型
  13. 从端到云——工业物联网项目全栈快速开发实践
  14. python是一种解释型、面向什么的计算机程序设计语言_python语言是一种什么类型...
  15. 植物大战僵尸——最强阵容
  16. 微信小程序文本框保留两位小数(非四舍五入)
  17. 什么是 SAP Support Package Stack
  18. 机器学习算法——概率类模型评估指标4(校准可靠性曲线及预测概率直方图)
  19. 常见而又容易被中小企业忽视的六个网络安全漏洞
  20. 基于改进YOLOv5的挖机铲斗缺陷检测系统(源码&教程)

热门文章

  1. CMake和Make——make makefile cmake qmake都是什么,有什么区别?
  2. Shell——echo命令
  3. QT 连接 sql server数据库 完整演示
  4. c语言 字符串转换为int或float
  5. 字符缓冲流 读写数据
  6. 云题库进入其它章节的办法 0925
  7. 办公自动化-python编写ppt-创建第一页-主标题与内容的编写
  8. django-数据库的操作-原始版本-表格的查询
  9. linux-选择输入法
  10. C与PHP的联系与区别