返回介绍

solution / 2400-2499 / 2441.Largest Positive Integer That Exists With Its Negative / README_EN

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

2441. Largest Positive Integer That Exists With Its Negative

中文文档

Description

Given an integer array nums that does not contain any zeros, find the largest positive integer k such that -k also exists in the array.

Return _the positive integer _k. If there is no such integer, return -1.

 

Example 1:

Input: nums = [-1,2,-3,3]
Output: 3
Explanation: 3 is the only valid k we can find in the array.

Example 2:

Input: nums = [-1,10,6,7,-7,1]
Output: 7
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.

Example 3:

Input: nums = [-10,8,6,7,-2,-3]
Output: -1
Explanation: There is no a single valid k, we return -1.

 

Constraints:

  • 1 <= nums.length <= 1000
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0

Solutions

Solution 1: Hash Table

We can use a hash table $s$ to record all elements that appear in the array, and a variable $ans$ to record the maximum positive integer that satisfies the problem requirements, initially $ans = -1$.

Next, we traverse each element $x$ in the hash table $s$. If $-x$ exists in $s$, then we update $ans = \max(ans, x)$.

After the traversal ends, return $ans$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.

class Solution:
  def findMaxK(self, nums: List[int]) -> int:
    s = set(nums)
    return max((x for x in s if -x in s), default=-1)
class Solution {
  public int findMaxK(int[] nums) {
    int ans = -1;
    Set<Integer> s = new HashSet<>();
    for (int x : nums) {
      s.add(x);
    }
    for (int x : s) {
      if (s.contains(-x)) {
        ans = Math.max(ans, x);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int findMaxK(vector<int>& nums) {
    unordered_set<int> s(nums.begin(), nums.end());
    int ans = -1;
    for (int x : s) {
      if (s.count(-x)) {
        ans = max(ans, x);
      }
    }
    return ans;
  }
};
func findMaxK(nums []int) int {
  ans := -1
  s := map[int]bool{}
  for _, x := range nums {
    s[x] = true
  }
  for x := range s {
    if s[-x] && ans < x {
      ans = x
    }
  }
  return ans
}
function findMaxK(nums: number[]): number {
  let ans = -1;
  const s = new Set(nums);
  for (const x of s) {
    if (s.has(-x)) {
      ans = Math.max(ans, x);
    }
  }
  return ans;
}
use std::collections::HashSet;
impl Solution {
  pub fn find_max_k(nums: Vec<i32>) -> i32 {
    let s = nums.into_iter().collect::<HashSet<i32>>();
    let mut ans = -1;
    for &x in s.iter() {
      if s.contains(&-x) {
        ans = ans.max(x);
      }
    }
    ans
  }
}

Solution 2

use std::collections::HashSet;

impl Solution {
  pub fn find_max_k(nums: Vec<i32>) -> i32 {
    let mut ans = -1;
    let mut h = HashSet::new();

    for &n in &nums {
      h.insert(n);
    }

    for &n in &nums {
      if h.contains(&-n) && n > ans {
        ans = n;
      }
    }

    ans
  }
}

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

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

发布评论

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