747_Largest-Number-At-Least-Twice-of-Others
747_Largest-Number-At-Least-Twice-of-Others
Description
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:
nums
will have a length in the range[1, 50]
.- Every
nums[i]
will be an integer in the range[0, 99]
.
Solution
Java solution
class Solution {
public int dominantIndex(int[] nums) {
if (nums.length == 1) {
return 0;
}
int largest = 0, secondLargest = 0, k = 0;
for (int i=0; i<nums.length; i++) {
if (nums[i] > largest) {
secondLargest = largest;
largest = nums[i];
k = i;
} else if (nums[i] > secondLargest) {
secondLargest = nums[i];
}
}
return largest >= 2*secondLargest ? k : -1;
}
}
Runtime: 17 ms
Python solution
class Solution:
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return 0
largest, second_largest, k = 0, 0, 0
for i, num in enumerate(nums):
if num > largest:
second_largest, largest, k = largest, num, i
elif num > second_largest:
second_largest = num
if largest >= 2*second_largest:
return k
else:
return -1
Runtime: 44 ms
747_Largest-Number-At-Least-Twice-of-Others的更多相关文章
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- 有理数的稠密性(The rational points are dense on the number axis.)
每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Number of Boomerangs 回旋镖的数量
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
随机推荐
- 疑难杂症--SQL SERVER 18056的错误
朋友遇到一个很棘手的问题,查看服务器日志,报以下错误: ::,spid296,未知,错误: ,严重性: ,状态: . ::,spid495,未知, The client was unable < ...
- 虚拟化 - Hyper-V
不能和VMware.VirtualBox同时使用 网络 交换机其实就是指网卡,只不过是虚拟的 内部交换机 外部交换机
- C#获取图片扩展名
代码: private string GetImageExtension(MemoryStream ms) { try { Image image = Image.FromStream(ms); if ...
- C#多线程编程系列(一)- 简介
目录 系列大纲 一.前言 二.目录结构 四.章节结构 五.相关链接 系列大纲 目前只整理到第二章,线程同步,笔者后面会慢慢更新,争取能把这本书中精华的知识都分享出来. C#多线程编程系列(一)- 简介 ...
- JavaScript一个页面中有多个audio标签,其中一个播放结束后自动播放下一个,audio连续播放
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IIS发布好的网页突然不显示图片了
按以下步骤把地址加到ie的本地intranet就好了
- docker的介绍以及常用命令
一.docker的介绍 1. Docker是什么? Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚 ...
- 洛谷P5280 [ZJOI2019]线段树(线段树)
题面 传送门 题解 考场上就这么一道会做的其它连暴力都没打--活该爆炸-- 首先我们得看出问题的本质:有\(m\)个操作,总共\(2^m\)种情况分别对应每个操作是否执行,求这\(2^m\)棵线段树上 ...
- java从txt文档读写数据
package com.abin.facade.ws.mail.function; import java.io.BufferedReader; import java.io.File; import ...
- lamp-linux3
LAMP编程之Linux(3) 一.权限管理 1.权限介绍(重点) 在Linux中分别有读.写.执行权限: 读权限: 对于文件夹来说,读权限影响用户是否能够列出目录结构 对于文件来说,读权限影响用户是 ...