0
点赞
收藏
分享

微信扫一扫

3607、至少是其他数字两倍的最大数

给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。


请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。



示例 1:


输入:nums = [3,6,1,0]

输出:1

解释:6 是最大的整数,对于数组中的其他整数,6 至少是数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。

示例 2:


输入:nums = [1,2,3,4]

输出:-1

解释:4 没有超过 3 的两倍大,所以返回 -1 。

示例 3:


输入:nums = [1]

输出:0

解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。


提示:


1 <= nums.length <= 50

0 <= nums[i] <= 100

nums 中的最大元素是唯一的


链接:https://leetcode-cn.com/problems/largest-number-at-least-twice-of-others

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

package cn.fansunion.leecode.number;

/**

* 747. 至少是其他数字两倍的最大数 <br/>给你一个整数数组 nums ,其中总是存在 唯一的 一个最大整数 。

*

* 请你找出数组中的最大元素并检查它是否 至少是数组中每个其他数字的两倍 。如果是,则返回 最大元素的下标 ,否则返回 -1 。

*

*

*

* 来源:力扣(LeetCode) 链接:力扣

* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

*

* @author wen.lei@brgroup.com

*

* 2022-2-26

*/

public class LargestNumberAtLeastTwiceOfOthers {

/*示例 1:



输入:nums = [3,6,1,0]

输出:1

解释:6 是最大的整数,对于数组中的其他整数,6 至少是数组中其他元素的两倍。6 的下标是 1 ,所以返回 1 。

示例 2:



输入:nums = [1,2,3,4]

输出:-1

解释:4 没有超过 3 的两倍大,所以返回 -1 。

示例 3:



输入:nums = [1]

输出:0

解释:因为不存在其他数字,所以认为现有数字 1 至少是其他数字的两倍。





提示:



1 <= nums.length <= 50

0 <= nums[i] <= 100

nums 中的最大元素是唯一的*/

/**

* 遍历数组,找出最大值max和第2大值secondMax,判断最大值max>=2*secondMax;其它特殊情况判断

* 一道简单题,错了2次

* @param nums

* @return

*/

public int dominantIndex(int[] nums) {

if (nums == null) {

return -1;

}

if (nums.length == 1) {

return 0;

}

int maxIndex = -1;

int max = -1;

int secondMax = -1;

int num = 0;

for (int index = 0; index < nums.length; index++) {

num = nums[index];

// 最大值

final boolean findMax = num > max && num > secondMax;

final boolean findSecondMax = num < max && num > secondMax;

if (findMax) {

//当前num为最大值时,之前的最大值就成了第2大值

//先更新secondMax

secondMax=max;

max = num;

maxIndex = index;

} else if (findSecondMax) {

secondMax = num;

}

}

if (max >= 2 * secondMax) {

return maxIndex;

}

return -1;

}

// 解法错误

// [2,3,4,7,11],遍历完成时,最大值是11,最小值还是-1

//忽略了第1个if对第2个if的影响,

public int dominantIndexError(int[] nums) {

if (nums == null) {

return -1;

}

if (nums.length == 1) {

return 0;

}

int maxIndex = -1;

int max = -1;

int secondMax = -1;

int num = 0;

for (int index = 0; index < nums.length; index++) {

num = nums[index];

// 最大值

if (num > max && num > secondMax) {

max = num;

maxIndex = index;

}

// 第2大值

if (num < max && num > secondMax) {

secondMax = num;

}

}

if (max >= 2 * secondMax) {

return maxIndex;

}

return -1;

}

}

package test.leecode.math;

import org.junit.Assert;

import org.junit.Test;

import cn.fansunion.leecode.number.LargestNumberAtLeastTwiceOfOthers;

/**

* @author wen.lei@brgroup.com

*

* 2022-2-25

*/

public class LargestNumberAtLeastTwiceOfOthersTest {

@Test

public void test() {

LargestNumberAtLeastTwiceOfOthers test = new LargestNumberAtLeastTwiceOfOthers();



//-1

int[] num14=new int[] {2,3,4,7,11};

Assert.assertEquals(-1,test.dominantIndex(num14));



int[] nums13=new int[] {1,2,3,4,5,6,7,8,9,10,11};

Assert.assertEquals(-1,test.dominantIndex(nums13));



int[] nums12=new int[] {1,2,3,4,5,16,17,8,9,20,21};

Assert.assertEquals(-1,test.dominantIndex(nums12));



int[] nums11=new int[] {1,3,5};

Assert.assertEquals(-1,test.dominantIndex(nums11));

//有值

int[] nums0=new int[] {1};

Assert.assertEquals(0,test.dominantIndex(nums0));



int[] nums1=new int[] {0,1};

Assert.assertEquals(1,test.dominantIndex(nums1));



int[] nums5=new int[] {1,2,4,8,8,17};

Assert.assertEquals(5,test.dominantIndex(nums5));

}

}

举报

相关推荐

0 条评论