【LeetCode】747. Largest Number At Least Twice of Others 解题报告(Python)
作者: 负雪明烛
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:
- nums will have a length in the range [1, 50].
- 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)的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】434. Number of Segments in a String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 正则表达式 字符串分割 日期 题目地址:htt ...
- 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...
- 【LeetCode】1118. Number of Days in a Month 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断是否是闰年 日期 题目地址:https://lee ...
- 【LeetCode】806. Number of Lines To Write String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用ASIIC码求长度 使用字典保存长度 日期 题目 ...
- 【LeetCode】1019. Next Greater Node In Linked List 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减栈 日期 题目地址:https://leetc ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
随机推荐
- 学习java的第二十六天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- Java 堆、栈、队列(遇见再更新)
目录 Java 栈.队列 栈 常用方法 案例 队列 Java 栈.队列 栈 常用方法 boolean empty() 测试堆栈是否为空 Object peek() 查看堆栈顶部的对象 Object p ...
- 日常Java 2021/10/3
方法: 用System.out.println()来解释,println()是一个方法,System是系统类,out 是标准输出对象. 也就是调用系统类中的对象中的方法. 注重方法:可以是程序简洁,有 ...
- Azkaban(一)【集群安装】
目录 一.下载解压 二. 配置Mysql 三. 配置Azkaban Executor 四. 配置Azkaban WebServer 一.下载解压 1.下载地址:https://github.com/a ...
- vue3 使用 data、computed、methods
简单数据ref复杂数据reactive 使用方法: // useCount.js import {ref,reactive,computed} from 'vue' export default fu ...
- haproxy动态增减主机与keepalived高级应用
一:本文将详细介绍haproxy的配置使用以及高级功能的使用,比如通过haproxy进行动态添加删除负载集群中的后端web服务器的指定主机,另外将详细介绍keepalived的详细配置方法.配置实例及 ...
- oracle中注释都是问号?中文显示不出来问题
本人在工作中需要把开发上的库恢复到自己的虚拟机里面,然而捣鼓了许久建立好数据库之后,在使用建表语句初始化表的时候,发现注释都是????? 然后一脸懵逼不知何解,网上一大堆是说修改环境变量 NLS_LA ...
- redis入门到精通系列(三):key的通用操作和redis内部db的通用操作
五种数据类型都用到了key,key本身是一种字符串,通过key可以获取redis中保存的对象.这一篇博客就将介绍key的通用操作. (一)key基本操作 删除key del key key是否存在 e ...
- Ruby Gems更换淘宝源方法
官方的 Rubygems 源由于有些资源放在 Amazon S3 上面,所以有时会抽风,在 Linux 下我用 proxychains gem install xxx 实现了指定程序实行 Shadow ...
- 一文读懂RESTful架构
转载自https://zhuanlan.zhihu.com/p/381554129 RESTful架构究竟是什么 别着急,想要了解RESTful,我们先来了解一位大佬Roy Thomas Fieldi ...