首页 > 其他分享 >leetcode 747. Largest Number At Least Twice of Others

leetcode 747. Largest Number At Least Twice of Others

时间:2023-05-30 17:37:05浏览次数:47  
标签:max1 return nums max2 max 747 Number ans Others

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].

最直观解法:

class Solution(object):
    def dominantIndex(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max_n = max(nums)
        max_cnt = 0
        ans = -1
        for i,n in enumerate(nums):
            if n != max_n:
                if max_n < n*2:
                    return -1
            else:
                max_cnt += 1
                if max_cnt > 1:
                    return -1
                ans = i
        return ans

数学解法:

class Solution(object):
    def dominantIndex(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max1, max2 = float('-inf'), float('-inf')
        ans = -1
        for i, n in enumerate(nums):
            if n > max1:
                max1, max2 = n, max1
                ans = i
            elif n > max2:
                max2 = n
        return -1 if max2*2 > max1 else ans

 

标签:max1,return,nums,max2,max,747,Number,ans,Others
From: https://blog.51cto.com/u_11908275/6380979

相关文章

  • leetcode 191. Number of 1 Bits
    Writeafunctionthattakesanunsignedintegerandreturnsthenumberof’1'bitsithas(alsoknownastheHammingweight).Forexample,the32-bitinteger’11'hasbinaryrepresentation00000000000000000000000000001011,sothefunctionshould......
  • GaussDB(DWS)迁移实践丨row_number输出结果不一致
    摘要:迁移前后结果集row_number字段值前后不一致,前在DWS上运行不一致。本文分享自华为云社区《GaussDB(DWS)迁移-oracle兼容--row_number输出结果不一致》,作者:譡里个檔。【问题表现】迁移前后结果集row_number字段值前后不一致,前在DWS上运行不一致。【问题分析】这种问题......
  • 大整数 Bignumber
    #include<bits/stdc++.h>usingnamespacestd;structbig_number{boolis_postive;strings;big_number(){}big_number(stringss){if(ss[0]!='-'){this->s=ss;this->is_postive=true;}else{this->s=ss.substr(1......
  • 2023年国际大学生程序设计竞赛(ACM-ICPC)新疆赛区 A.The Number Of Black Edges
    传送门大致题意:  爱丽丝得到一棵树,树上有n个节点,索引从1到n。树上的每条边可以是黑色或白色,所有的边最初都是白色的。有三种操作:1.将一条边的颜色改为黑色。2.将一条边的颜色改为白色。3.3.给定一个节点索引,计算从所有奇数节点到该节点的简单路径上的黑色边的数量之和。请......
  • 1342. Number of Steps to Reduce a Number to Zero刷题笔记
    easy题,按照逻辑写就行了classSolution:defnumberOfSteps(self,num:int)->int:step=0whilenum:ifnum%2==0:num=num/2else:num-=1step+=1returnste......
  • el-input的maxlength属性在number类型时需要特殊处理
    maxlength在开发中,输入框一定要限制长度,之前在开发中都没注意过输入字符串的时候直接使用maxlength就可以了但是type是number的时候,maxlength就不起作用了.number默认情况下,不管用户输入字符串还是数字,在获取的值都是字符串.number可以将字符串转换成数字但是我自己测试......
  • P1747 单调不降序列中与x最接近元素
    #include<iostream>usingnamespacestd;intarr[100010];intmain(){intn;cin>>n;inti;for(i=1;i<=n;i++){cin>>arr[i];//输入非降序列}intm;cin>>m;while(m--)......
  • 关于Excel表格中对多个General或者Number数值格式的单元转换为Text文本时-值包含E+的
    对于单元格的值为默认的General或者Number数字时,我们可以直接选中,单元格,或者某个范围,可以直接将上面的格式处选择为Text这样就完成了一次格式转换,如下默认是General普通格式,我们直接就将其转换成Text文本格式不过只有当我们将鼠标双击一下单元格式,才会看到左上角看到熟悉的Text......
  • 2017第二届广东省强网杯线上赛--------phone number
    ================================个人收获:1.sql语句里面也可以直接用database()2.跟数据库有联系的地方都可能存在注入 ================================题目: 开始前对源码,http请求,路径。。这些都找过没什么有用的信息。就只有这个还有点用,再检查手机号使用人数页面的源码有这......
  • js number format All In One
    jsnumberformatAllInOne金融数据表示法千分位符号//1,000,000demos(......