返回介绍

solution / 0700-0799 / 0747.Largest Number At Least Twice of Others / README_EN

发布于 2024-06-17 01:03:34 字数 4265 浏览 0 评论 0 收藏 0

747. Largest Number At Least Twice of Others

中文文档

Description

You are given an integer array nums where the largest integer is unique.

Determine 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, or return _-1_ otherwise_.

 

Example 1:

Input: nums = [3,6,1,0]
Output: 1
Explanation: 6 is the largest integer.
For every other number in the array x, 6 is at least 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 is less than twice the value of 3, so we return -1.

 

Constraints:

  • 2 <= nums.length <= 50
  • 0 <= nums[i] <= 100
  • The largest element in nums is unique.

Solutions

Solution 1: Traversal

We can traverse the array $nums$ to find the maximum value $x$ and the second largest value $y$ in the array. If $x \ge 2y$, then return the index of $x$, otherwise return $-1$.

We can also first find the maximum value $x$ in the array and find the index $k$ of the maximum value $x$ at the same time. Then traverse the array again. If we find an element $y$ outside of $k$ that satisfies $x < 2y$, then return $-1$. Otherwise, return $k$ after the traversal ends.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

class Solution:
  def dominantIndex(self, nums: List[int]) -> int:
    x, y = nlargest(2, nums)
    return nums.index(x) if x >= 2 * y else -1
class Solution {
  public int dominantIndex(int[] nums) {
    int n = nums.length;
    int k = 0;
    for (int i = 0; i < n; ++i) {
      if (nums[k] < nums[i]) {
        k = i;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (k != i && nums[k] < nums[i] * 2) {
        return -1;
      }
    }
    return k;
  }
}
class Solution {
public:
  int dominantIndex(vector<int>& nums) {
    int n = nums.size();
    int k = 0;
    for (int i = 0; i < n; ++i) {
      if (nums[k] < nums[i]) {
        k = i;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (k != i && nums[k] < nums[i] * 2) {
        return -1;
      }
    }
    return k;
  }
};
func dominantIndex(nums []int) int {
  k := 0
  for i, x := range nums {
    if nums[k] < x {
      k = i
    }
  }
  for i, x := range nums {
    if k != i && nums[k] < x*2 {
      return -1
    }
  }
  return k
}
function dominantIndex(nums: number[]): number {
  let k = 0;
  for (let i = 0; i < nums.length; ++i) {
    if (nums[i] > nums[k]) {
      k = i;
    }
  }
  for (let i = 0; i < nums.length; ++i) {
    if (i !== k && nums[k] < nums[i] * 2) {
      return -1;
    }
  }
  return k;
}
/**
 * @param {number[]} nums
 * @return {number}
 */
var dominantIndex = function (nums) {
  let k = 0;
  for (let i = 0; i < nums.length; ++i) {
    if (nums[i] > nums[k]) {
      k = i;
    }
  }
  for (let i = 0; i < nums.length; ++i) {
    if (i !== k && nums[k] < nums[i] * 2) {
      return -1;
    }
  }
  return k;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文