作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/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:

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

题目大意

判断一个数组中的最大数字是不是其他数字的至少2倍。如果是的话返回最大数字的索引,否则返回-1.

解题方法

寻找两次最大值

最大值是其他值的二倍,也就是说最大值是次大值的二倍即可。

题目已经说了,最大值只存在一个。所以找到最大值,然后找到其索引,弹出该值之后再求最大值。

class Solution(object):
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return 0
largest = max(nums)
ind = nums.index(largest)
nums.pop(ind)
if largest >= 2 * max(nums):
return ind
else:
return -1

排序

先排序,然后看最大是不是次大的二倍,这样也可以。不过排序会改变位置,所以先保存最大数字的位置。

class Solution(object):
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return 0
largest = max(nums)
ind = nums.index(largest)
nums.sort()
if largest >= 2 * nums[-2]:
return ind
else:
return -1

大顶堆

使用大顶堆保存了数字和索引的映射,这样弹出来两个位置,便是最大和次大,在判断即可。

class Solution(object):
def dominantIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return 0
heap = [(-num, i) for i, num in enumerate(nums)]
heapq.heapify(heap)
largest, ind = heapq.heappop(heap)
if largest <= 2 * heapq.heappop(heap)[0]:
return ind
return -1

日期

2018 年 1 月 28 日
2018 年 11 月 21 日 —— 又是一个美好的开始

【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)的更多相关文章

  1. [LeetCode] 747. Largest Number At Least Twice of Others_Easy

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

  2. leetcode 747. Largest Number At Least Twice of Others

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

  3. 【LeetCode】434. Number of Segments in a String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...

  4. 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...

  5. 【LeetCode】1118. Number of Days in a Month 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断是否是闰年 日期 题目地址:https://lee ...

  6. 【LeetCode】806. Number of Lines To Write String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...

  7. 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...

  8. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  9. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

随机推荐

  1. mysql 计算日期为当年第几季度

    select T21620.日期 as F21634, QUARTER('98-04-01')  as quarter                       #返回日期是一年的第几个季度   - ...

  2. PL\SQL和PL/SQL Developer 12安装与配置

    安装: (1)在已有安装包的情况下,直接将安装包解压到文件夹下,注意不要解压缩到c:\programs Files(x86)的文件夹下,不能解压缩到有中文文件夹命名的文件夹下面 (2)没有安装包的情况 ...

  3. hbase参数调优

    @ 目录 HBase参数调优 hbase.regionserver.handler.count hbase.hregion.max.filesize hbase.hregion.majorcompac ...

  4. acquaint

    Interpersonal relationships are dynamic systems that change continuously during their existence. Lik ...

  5. 12. Fedora 中文乱码问题

    1. Rhythmbox(音乐播放器乱码) yum install python-mutagen mid3iconv -e GBK *.mp3 2. totem电影播放机播放列表乱码解决1).修改to ...

  6. Hive(十二)【调优】

    目录 1.Fetch抓取 2.本地模式 3.表的优化 3.1大小表join 3.2大表Join大表 3.3map join 3.4group By 3.5 count(distinct) 3.6笛卡尔 ...

  7. Template Metaprogramming in C++

    说实话,学习C++以来,第一次听说"Metaprogramming"这个名词. Predict the output of following C++ program. 1 #in ...

  8. jquery的each和js原生for循环性能对比

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  9. When do we use Initializer List in C++?

    Initializer List is used to initialize data members of a class. The list of members to be initialize ...

  10. malloc() vs new

    Following are the differences between malloc() and operator new. (1)new calls constructors, while ma ...