From 2c36a836803311904003db36c130c7855830c143 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 2 May 2024 12:02:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 2441 --- .../README.md | 31 +++++++++++-------- .../Solution.go | 16 ++++++++-- .../Solution_test.go | 14 ++++----- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/README.md b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/README.md index bb3914d48..97024886a 100755 --- a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/README.md +++ b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/README.md @@ -1,28 +1,33 @@ # [2441.Largest Positive Integer That Exists With Its Negative][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## 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: a = "11", b = "1" -Output: "100" +Input: nums = [-1,2,-3,3] +Output: 3 +Explanation: 3 is the only valid k we can find in the array. ``` -## 题意 -> ... +**Exists 2:** -## 题解 - -### 思路1 -> ... -Largest Positive Integer That Exists With Its Negative -```go +``` +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. ``` +**Exists 3:** + +``` +Input: nums = [-10,8,6,7,-2,-3] +Output: -1 +Explanation: There is no a single valid k, we return -1. +``` ## 结语 diff --git a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution.go b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution.go index d115ccf5e..c455a47d3 100644 --- a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution.go +++ b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution.go @@ -1,5 +1,17 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + e := make(map[int]struct{}) + ans := -1 + for _, n := range nums { + if _, ok := e[-n]; ok { + if n < 0 { + n = -n + } + ans = max(ans, n) + continue + } + e[n] = struct{}{} + } + return ans } diff --git a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution_test.go b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution_test.go index 14ff50eb4..ac0d145b7 100644 --- a/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution_test.go +++ b/leetcode/2401-2500/2441.Largest-Positive-Integer-That-Exists-With-Its-Negative/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{-1, 2, -3, 3}, 3}, + {"TestCase2", []int{-1, 10, 6, 7, -7, 1}, 7}, + {"TestCase3", []int{-10, 8, 6, 7, -2, -3}, -1}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }