Skip to content

Commit 873b996

Browse files
authored
Merge pull request #1028 from 0xff-dev/2601
Add solution and test-cases for problem 2601
2 parents ed636d2 + 7a003e1 commit 873b996

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [2601.Prime Subtraction Operation][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums` of length `n`.
5+
6+
You can perform the following operation as many times as you want:
7+
8+
- Pick an index `i` that you haven’t picked before, and pick a prime `p` **strictly less than** `nums[i]`, then subtract `p` from `nums[i]`.
9+
10+
Return true if you can make `nums` a strictly increasing array using the above operation and false otherwise.
11+
12+
A **strictly increasing array** is an array whose each element is strictly greater than its preceding element.
13+
14+
**Example 1:**
15+
16+
```
17+
Input: nums = [4,9,6,10]
18+
Output: true
19+
Explanation: In the first operation: Pick i = 0 and p = 3, and then subtract 3 from nums[0], so that nums becomes [1,9,6,10].
20+
In the second operation: i = 1, p = 7, subtract 7 from nums[1], so nums becomes equal to [1,2,6,10].
21+
After the second operation, nums is sorted in strictly increasing order, so the answer is true.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: nums = [6,8,11,12]
28+
Output: true
29+
Explanation: Initially nums is sorted in strictly increasing order, so we don't need to make any operations.
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: nums = [5,8,3]
36+
Output: false
37+
Explanation: It can be proven that there is no way to perform operations to make nums sorted in strictly increasing order, so the answer is false.
38+
```
39+
40+
## 结语
41+
42+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
43+
44+
[title]: https://leetcode.com/problems/prime-subtraction-operation
45+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func checkPrime(x int) bool {
6+
for i := 2; i <= int(math.Sqrt(float64(x))); i++ {
7+
if x%i == 0 {
8+
return false
9+
}
10+
}
11+
return true
12+
}
13+
14+
func Solution(nums []int) bool {
15+
for i := 0; i < len(nums); i++ {
16+
var bound int
17+
if i == 0 {
18+
bound = nums[0]
19+
} else {
20+
bound = nums[i] - nums[i-1]
21+
}
22+
23+
if bound <= 0 {
24+
return false
25+
}
26+
27+
largestPrime := 0
28+
for j := bound - 1; j >= 2; j-- {
29+
if checkPrime(j) {
30+
largestPrime = j
31+
break
32+
}
33+
}
34+
35+
nums[i] = nums[i] - largestPrime
36+
}
37+
return true
538
}

leetcode/2601-2700/2601.Prime-Subtraction-Operation/Solution_test.go

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

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)