C#LeetCode刷题之#747-至少是其他数字两倍的最大数( Largest Number At Least Twice of Others)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [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 ...
- Java实现 LeetCode 747 至少是其他数字两倍的最大数(暴力)
747. 至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 ...
- LeetCode:至少是其他数字两倍的最大数【747】
LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...
- LeetCode747 至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...
- Leetcode747至少是其他数字两倍的最大数
Leetcode747至少是其他数字两倍的最大数 在一个给定的数组nums中,总是存在一个最大元素 .查找数组中的最大元素是否至少是数组中每个其他数字的两倍.如果是,则返回最大元素的索引,否则返回-1 ...
- [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 ...
- [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)
①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...
- Leetcode747.Largest Number At Least Twice of Others至少是其他数字两倍的最大数
在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums = [3, ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
随机推荐
- android studio 正式版打包错误的一个问题
今日在下载了别人的demo后,编译到我的手机上,然后通过qq等把软件发到其他的手机上使用时,无法安装,好像是因为这个是调试版本才安装不上,在网搜了一堆资料怎么建key怎么发布正式的版本,问题现在已解决 ...
- Database Identifiers - SID
These options include your global database name and system identifier (SID). The SID is a unique ide ...
- CUDA C++ Extensions
敲代码的时候总是会去CUDA官方文档中找找思路,感觉每次看英文文档都要耗费一点时间来翻译,干脆自己翻译一下便于以后查阅.官方文档:cuda-c-language-extensions 目录 函数修饰符 ...
- 技能实际操作:如何为Centos7 配置静态路由?
如图: 业务地址:192.168.10.0/24 ---- 192.168.20.0/24 管理地址:172.168.10.0/24 --- 172.168.20.0/24 需求:每台主机配置两张网卡 ...
- leetcode题库练习_左旋转字符串
题目:左旋转字符串 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能.比如,输入字符串"abcdefg"和数字2,该函数将返 ...
- Python基础教程(第3版)PDF高清完整版免费下载|百度云盘
百度云盘:Python基础教程(第3版)PDF高清完整版免费下载 提取码:gkiy 内容简介 本书包括Python程序设计的方方面面:首先从Python的安装开始,随后介绍了Python的基础知识和基 ...
- 第三节:Centos下安装Mysql5.6数据库
1.下载mysql5.6版本 [官网-需要什么版本自己去找]https://dev.mysql.com/downloads/mysql/5.6.html [版本]linux通用 cd /usr/loc ...
- Java瞬态变量transient
我们都知道,阳光是看得见却摸不着的.它真实的存在,但是却无法将其装在罐子里,这是因为光子不具有静止质量.这注定我们只能利用光子而不能将其捕获(或许只是暂时).在Java中,有一种变量就像光子一样, ...
- 远光武汉研发中心区块链事业部Java面试总结
面试在约定的时间准时进行,也是采用腾讯会议远程面试的方式.但是这是我第一次遇到面试官未打开摄像头的情况,后面经过沟通,双方都打开摄像头进行交流. 之前了解这个岗位主要是区块链相关的Java开发,所以事 ...
- mybatis之if判断
今天使用mybatis开发公司中台项目踩的一个坑,分享并记录一下 踩坑前因:因项目中比较多状态字段,用了大量的Integer 0和1进行判断 在功能做完后只是粗略的点了下觉得没多大问题(来自程序员强大 ...