100444. 识别数组中的最大异常值
给你一个整数数组 nums
。该数组包含 n
个元素,其中 恰好 有 n - 2
个元素是 特殊数字 。剩下的 两个 元素中,一个是这些 特殊数字 的 和 ,另一个是 异常值 。
异常值 的定义是:既不是原始特殊数字之一,也不是表示这些数字元素和的数字。
注意,特殊数字、和 以及 异常值 的下标必须 不同 ,但可以共享 相同 的值。
返回 nums
中可能的 最大异常值。
示例 1:
输入: nums = [2,3,5,10]
输出: 10
解释:
特殊数字可以是 2 和 3,因此和为 5,异常值为 10。
示例 2:
输入: nums = [-2,-1,-3,-6,4]
输出: 4
解释:
特殊数字可以是 -2、-1 和 -3,因此和为 -6,异常值为 4。
示例 3:
输入: nums = [1,1,1,1,1,5,5]
输出: 5
解释:
特殊数字可以是 1、1、1、1 和 1,因此和为 5,另一个 5 为异常值。
提示:
3 <= nums.length <= 105
-1000 <= nums[i] <= 1000
nums
中至少存在 一个 可能的异常值。原站题解
javascript 解法, 执行用时: 207 ms, 内存消耗: 69.3 MB, 提交时间: 2024-12-02 10:07:24
/** * @param {number[]} nums * @return {number} */ var getLargestOutlier = function(nums) { const cnt = new Map(); let total = 0; // 统计每个数字的出现次数并计算总和 for (let x of nums) { cnt.set(x, (cnt.get(x) || 0) + 1); total += x; } let ans = Number.MIN_SAFE_INTEGER; // 遍历每个数字,寻找可能的异常值 for (let y of nums) { let t = total - 2 * y; if (cnt.has(t) && (cnt.get(t) > 1 || (cnt.get(t) > 0 && t !== y))) { ans = Math.max(ans, t); } } return ans; };
rust 解法, 执行用时: 75 ms, 内存消耗: 3.2 MB, 提交时间: 2024-12-02 10:06:10
use std::collections::HashMap; use std::i32; impl Solution { pub fn get_largest_outlier(nums: Vec<i32>) -> i32 { let mut cnt: HashMap<i32, usize> = HashMap::new(); let mut total: i32 = 0; // 统计每个数字的出现次数,并计算总和 for &x in &nums { *cnt.entry(x).or_insert(0) += 1; total += x; } let mut ans = i32::MIN; // 遍历每个数字,寻找可能的异常值 for &y in &nums { let t = total - y * 2; if cnt.contains_key(&t) && (cnt[&t] > 1 || (cnt[&t] == 1 && t != y)) { ans = ans.max(t); } } ans } }
php 解法, 执行用时: 594 ms, 内存消耗: 27.3 MB, 提交时间: 2024-12-02 10:01:42
class Solution { /** * @param Integer[] $nums * @return Integer */ function getLargestOutlier($nums) { $cnt = []; $total = 0; foreach ( $nums as $x ) { $cnt[$x] = isset($cnt[$x]) ? $cnt[$x]+1 : 1; $total += $x; } $ans = PHP_INT_MIN; foreach ( $nums as $x ) { $cnt[$x]--; if ( ($total-$x)%2 == 0 && $cnt[intval(($total-$x)/2)] > 0 ) { $ans = max($ans, $x); } $cnt[$x]++; } return $ans; } }
cpp 解法, 执行用时: 286 ms, 内存消耗: 177.5 MB, 提交时间: 2024-12-02 09:54:26
class Solution { public: // 枚举元素和 int getLargestOutlier1(vector<int>& nums) { unordered_map<int, int> cnt; int total = 0; for (int x : nums) { cnt[x]++; total += x; } int ans = INT_MIN; for (int y : nums) { int t = total - y * 2; auto it = cnt.find(t); if (it != cnt.end() && (t != y || it->second > 1)) { ans = max(ans, t); } } return ans; } // 枚举异常值 int getLargestOutlier2(vector<int>& nums) { unordered_map<int, int> cnt; int total = 0; for (int x : nums) { cnt[x]++; total += x; } int ans = INT_MIN; for (int x : nums) { cnt[x]--; if ((total - x) % 2 == 0 && cnt[(total - x) / 2] > 0) { ans = max(ans, x); } cnt[x]++; } return ans; } // 枚举异常值 int getLargestOutlier(vector<int>& nums) { unordered_map<int, int> cnt; int total = 0; for (int x : nums) { cnt[x]++; total += x; } int ans = INT_MIN; for (int x : nums) { if ((total - x) % 2 == 0) { int y = (total - x) / 2; auto it = cnt.find(y); if (it != cnt.end() && (y != x || it->second > 1)) { ans = max(ans, x); } } } return ans; } };
java 解法, 执行用时: 88 ms, 内存消耗: 57.1 MB, 提交时间: 2024-12-02 09:53:11
class Solution { // 枚举异常值1 public int getLargestOutlier1(int[] nums) { Map<Integer, Integer> cnt = new HashMap<>(); int total = 0; for (int x : nums) { cnt.merge(x, 1, Integer::sum); // cnt[x]++ total += x; } int ans = Integer.MIN_VALUE; for (int x : nums) { cnt.merge(x, -1, Integer::sum); if ((total - x) % 2 == 0 && cnt.getOrDefault((total - x) / 2, 0) > 0) { ans = Math.max(ans, x); } cnt.merge(x, 1, Integer::sum); } return ans; } // 枚举异常值2 public int getLargestOutlier2(int[] nums) { Map<Integer, Integer> cnt = new HashMap<>(); int total = 0; for (int x : nums) { cnt.merge(x, 1, Integer::sum); // cnt[x]++ total += x; } int ans = Integer.MIN_VALUE; for (int x : nums) { if ((total - x) % 2 == 0) { int y = (total - x) / 2; if (cnt.containsKey(y) && (y != x || cnt.get(y) > 1)) { ans = Math.max(ans, x); } } } return ans; } // 枚举元素和 public int getLargestOutlier(int[] nums) { Map<Integer, Integer> cnt = new HashMap<>(); int total = 0; for (int x : nums) { cnt.merge(x, 1, Integer::sum); // cnt[x]++ total += x; } int ans = Integer.MIN_VALUE; for (int y : nums) { int t = total - y * 2; if (cnt.containsKey(t) && (t != y || cnt.get(t) > 1)) { ans = Math.max(ans, t); } } return ans; } }
golang 解法, 执行用时: 296 ms, 内存消耗: 10.6 MB, 提交时间: 2024-12-02 09:52:15
// 枚举元素和 func getLargestOutlier1(nums []int) int { cnt := map[int]int{} total := 0 for _, x := range nums { cnt[x]++ total += x } ans := math.MinInt for _, y := range nums { t := total - y*2 if cnt[t] > 1 || cnt[t] > 0 && t != y { ans = max(ans, t) } } return ans } // 枚举异常值 func getLargestOutlier2(nums []int) int { cnt := map[int]int{} total := 0 for _, x := range nums { cnt[x]++ total += x } ans := math.MinInt for _, x := range nums { if (total-x)%2 == 0 { y := (total - x) / 2 if cnt[y] > 1 || cnt[y] > 0 && y != x { ans = max(ans, x) } } } return ans } // 枚举异常值2 func getLargestOutlier(nums []int) int { cnt := map[int]int{} total := 0 for _, x := range nums { cnt[x]++ total += x } ans := math.MinInt for _, x := range nums { cnt[x]-- if (total-x)%2 == 0 && cnt[(total-x)/2] > 0 { ans = max(ans, x) } cnt[x]++ } return ans }
python3 解法, 执行用时: 1591 ms, 内存消耗: 29.7 MB, 提交时间: 2024-12-02 09:51:05
class Solution: # 枚举异常值 def getLargestOutlier1(self, nums: List[int]) -> int: cnt = Counter(nums) total = sum(nums) ans = -inf for x in nums: cnt[x] -= 1 if (total - x) % 2 == 0 and cnt[(total - x) // 2] > 0: ans = max(ans, x) cnt[x] += 1 return ans # # 枚举异常值2 def getLargestOutlier2(self, nums: List[int]) -> int: cnt = Counter(nums) total = sum(nums) ans = -inf for x in nums: y, rem = divmod(total - x, 2) if rem == 0 and y in cnt and (y != x or cnt[y] > 1): ans = max(ans, x) return ans # 枚举元素和 def getLargestOutlier(self, nums: List[int]) -> int: cnt = Counter(nums) total = sum(nums) ans = -inf for y in nums: t = total - y * 2 if t in cnt and (t != y or cnt[t] > 1): ans = max(ans, t) return ans