How to Find the Dominant Index in Array (Largest Number At Least

  • 时间:2020-10-11 16:01:36
  • 分类:网络文摘
  • 阅读:122 次

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

Given an array of integers, find the largest one and if it at least twice than the second-largest, return its index, otherwise return -1.

C++ of Finding the Dominant Index of Array

The edge cases need to be dealt when the array is empty or containing only one element. In case of empty array, -1 needs to be returned as there is no dominant number. In case of only 1 element, the answer is 0 as it’s the index of the dominant number.

On other cases, we need to maintain the largest and the second largest number, so we can check if the largest number is at least twice of the second largest number. Of course, we need to remember the index of the largest number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        if (nums.size() == 0) return -1;
        if (nums.size() == 1) return 0;
        int max1 = nums[0];
        int max2 = INT_MIN;
        int index = 0;
        for (int i = 1; i < nums.size(); ++ i) {
            if (nums[i] > max1) {
                max2 = max1;
                max1 = nums[i];
                index = i;
            } else {
                if (nums[i] > max2) {
                    max2 = nums[i];
                }
            }
        }
        return (max1 &t;= 2 * max2) ? index : -1;
    }
};
class Solution {
public:
    int dominantIndex(vector<int>& nums) {
        if (nums.size() == 0) return -1;
        if (nums.size() == 1) return 0;
        int max1 = nums[0];
        int max2 = INT_MIN;
        int index = 0;
        for (int i = 1; i < nums.size(); ++ i) {
            if (nums[i] > max1) {
                max2 = max1;
                max1 = nums[i];
                index = i;
            } else {
                if (nums[i] > max2) {
                    max2 = nums[i];
                }
            }
        }
        return (max1 &t;= 2 * max2) ? index : -1;
    }
};

The above C++ code of finding the dominant index/number of the array runs at O(N) complexity and O(1) space.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
饮食与健康:维生素B2的补充无需刻意  女性急躁易发怒可能与维生素缺乏有关系  冬天吃水果,究竟应该注意什么问题?  萝卜的保健功能和萝卜的食疗吃法  红枣美味营养可养生但是禁忌亦不少  不同包装的牛奶,你知道该如何选择吗  冬天吃羊肉如何去掉羊膻味及饮食禁忌  适度喝啤酒预防骨质疏松保持关节弹性  关于鸡蛋营养及其食用方法的十大误区  腊肉的风味和特点及腊肉的制作全过程 
评论列表
添加评论