From d16f7b5bfc348134f3c30f5636a2406f4a2a306f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 10 Jan 2025 09:04:09 +0800 Subject: [PATCH] Add solution and test-cases for problem 3115 --- .../3115.Maximum-Prime-Difference/README.md | 27 ++++++++++--------- .../3115.Maximum-Prime-Difference/Solution.go | 20 ++++++++++++-- .../Solution_test.go | 13 +++++---- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/leetcode/3101-3200/3115.Maximum-Prime-Difference/README.md b/leetcode/3101-3200/3115.Maximum-Prime-Difference/README.md index f5f58aa7c..defd94186 100755 --- a/leetcode/3101-3200/3115.Maximum-Prime-Difference/README.md +++ b/leetcode/3101-3200/3115.Maximum-Prime-Difference/README.md @@ -1,28 +1,29 @@ # [3115.Maximum Prime Difference][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 +You are given an integer array `nums`. + +Return an integer that is the **maximum** distance between the **indices** of two (not necessarily different) prime numbers in `nums`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: nums = [4,2,9,5,3] -## 题意 -> ... +Output: 3 -## 题解 +Explanation: nums[1], nums[3], and nums[4] are prime. So the answer is |4 - 1| = 3. +``` + +**Example 2:** -### 思路1 -> ... -Maximum Prime Difference -```go ``` +Input: nums = [4,8,2,8] + +Output: 0 +Explanation: nums[2] is prime. Because there is just one prime number, the answer is |2 - 2| = 0. +``` ## 结语 diff --git a/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution.go b/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution.go index d115ccf5e..1db7c5db4 100644 --- a/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution.go +++ b/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution.go @@ -1,5 +1,21 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + notPrimes := [101]bool{} + notPrimes[1] = true + for i := 2; i < 101; i++ { + if !notPrimes[i] { + for j := i + i; j < 101; j += i { + notPrimes[j] = true + } + } + } + m1, m2 := -1, len(nums)+1 + for i, n := range nums { + if !notPrimes[n] { + m1 = max(m1, i) + m2 = min(m2, i) + } + } + return m1 - m2 } diff --git a/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution_test.go b/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution_test.go index 14ff50eb4..ba22d68c1 100644 --- a/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution_test.go +++ b/leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution_test.go @@ -10,12 +10,11 @@ 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{4, 2, 9, 5, 3}, 3}, + {"TestCase2", []int{4, 8, 2, 8}, 0}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }