Skip to content

Commit d16f7b5

Browse files
committed
Add solution and test-cases for problem 3115
1 parent a337ff8 commit d16f7b5

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

leetcode/3101-3200/3115.Maximum-Prime-Difference/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [3115.Maximum Prime Difference][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given an integer array `nums`.
5+
6+
Return an integer that is the **maximum** distance between the **indices** of two (not necessarily different) prime numbers in `nums`.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
11+
Input: nums = [4,2,9,5,3]
1412
15-
## 题意
16-
> ...
13+
Output: 3
1714
18-
## 题解
15+
Explanation: nums[1], nums[3], and nums[4] are prime. So the answer is |4 - 1| = 3.
16+
```
17+
18+
**Example 2:**
1919

20-
### 思路1
21-
> ...
22-
Maximum Prime Difference
23-
```go
2420
```
21+
Input: nums = [4,8,2,8]
22+
23+
Output: 0
2524
25+
Explanation: nums[2] is prime. Because there is just one prime number, the answer is |2 - 2| = 0.
26+
```
2627

2728
## 结语
2829

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
notPrimes := [101]bool{}
5+
notPrimes[1] = true
6+
for i := 2; i < 101; i++ {
7+
if !notPrimes[i] {
8+
for j := i + i; j < 101; j += i {
9+
notPrimes[j] = true
10+
}
11+
}
12+
}
13+
m1, m2 := -1, len(nums)+1
14+
for i, n := range nums {
15+
if !notPrimes[n] {
16+
m1 = max(m1, i)
17+
m2 = min(m2, i)
18+
}
19+
}
20+
return m1 - m2
521
}

leetcode/3101-3200/3115.Maximum-Prime-Difference/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 2, 9, 5, 3}, 3},
17+
{"TestCase2", []int{4, 8, 2, 8}, 0},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)