Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 65caa23

Browse files
committed
964 added
1 parent fe31e50 commit 65caa23

File tree

4 files changed

+123
-25
lines changed

4 files changed

+123
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# [964. Least Operators to Express Number](https://leetcode.com/problems/least-operators-to-express-number/)
2+
3+
Given a single positive integer `x`, we will write an expression of the form `x (op1) x (op2) x (op3) x ...` where each operator `op1`, `op2`, etc. is either addition, subtraction, multiplication, or division `(+, -, *, or /)`. For example, with `x = 3`, we might write `3 * 3 / 3 + 3 - 3` which is a value of 3.
4+
5+
When writing such an expression, we adhere to the following conventions:
6+
7+
1. The division operator (/) returns rational numbers.
8+
1. There are no parentheses placed anywhere.
9+
1. We use the usual order of operations: multiplication and division happens before addition and subtraction.
10+
1. It's not allowed to use the unary negation operator (-). For example, "x - x" is a valid expression as it only uses subtraction, but "-x + x" is not because it uses negation.
11+
12+
We would like to write an expression with the least number of operators such that the expression equals the given target. Return the least number of operators used.
13+
14+
Example 1:
15+
16+
```text
17+
Input: x = 3, target = 19
18+
Output: 5
19+
Explanation: 3 * 3 + 3 * 3 + 3 / 3. The expression contains 5 operations.
20+
```
21+
22+
Example 2:
23+
24+
```text
25+
Input: x = 5, target = 501
26+
Output: 8
27+
Explanation: 5 * 5 * 5 * 5 - 5 * 5 * 5 + 5 / 5. The expression contains 8 operations.
28+
```
29+
30+
Example 3:
31+
32+
```text
33+
Input: x = 100, target = 100000000
34+
Output: 3
35+
Explanation: 100 * 100 * 100 * 100. The expression contains 3 operations.
36+
```
37+
38+
Note:
39+
40+
- 2 <= x <= 100
41+
- 1 <= target <= 2 * 10^8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package problem0964
2+
3+
func leastOpsExpressTarget(x int, target int) int {
4+
5+
return 0
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package problem0964
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
x int
12+
target int
13+
ans int
14+
}{
15+
16+
{
17+
3,
18+
19,
19+
5,
20+
},
21+
22+
{
23+
5,
24+
501,
25+
8,
26+
},
27+
28+
{
29+
100,
30+
100000000,
31+
3,
32+
},
33+
34+
// 可以有多个 testcase
35+
}
36+
37+
func Test_leastOpsExpressTarget(t *testing.T) {
38+
ast := assert.New(t)
39+
40+
for _, tc := range tcs {
41+
ast.Equal(tc.ans, leastOpsExpressTarget(tc.x, tc.target), "输入:%v", tc)
42+
}
43+
}
44+
45+
func Benchmark_leastOpsExpressTarget(b *testing.B) {
46+
for i := 0; i < b.N; i++ {
47+
for _, tc := range tcs {
48+
leastOpsExpressTarget(tc.x, tc.target)
49+
}
50+
}
51+
}

leetcode.json

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 890,
4-
"Updated": "2019-03-25T21:30:04.280505817+08:00",
3+
"Ranking": 881,
4+
"Updated": "2019-03-26T21:06:02.431826249+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 215,
@@ -2281,7 +2281,7 @@
22812281
"ID": 188,
22822282
"Title": "Best Time to Buy and Sell Stock IV",
22832283
"TitleSlug": "best-time-to-buy-and-sell-stock-iv",
2284-
"PassRate": "25%",
2284+
"PassRate": "26%",
22852285
"Difficulty": "Hard",
22862286
"IsAccepted": true,
22872287
"IsPaid": false,
@@ -2545,7 +2545,7 @@
25452545
"ID": 210,
25462546
"Title": "Course Schedule II",
25472547
"TitleSlug": "course-schedule-ii",
2548-
"PassRate": "33%",
2548+
"PassRate": "34%",
25492549
"Difficulty": "Medium",
25502550
"IsAccepted": true,
25512551
"IsPaid": false,
@@ -2689,7 +2689,7 @@
26892689
"ID": 222,
26902690
"Title": "Count Complete Tree Nodes",
26912691
"TitleSlug": "count-complete-tree-nodes",
2692-
"PassRate": "31%",
2692+
"PassRate": "32%",
26932693
"Difficulty": "Medium",
26942694
"IsAccepted": true,
26952695
"IsPaid": false,
@@ -3073,7 +3073,7 @@
30733073
"ID": 254,
30743074
"Title": "Factor Combinations",
30753075
"TitleSlug": "factor-combinations",
3076-
"PassRate": "43%",
3076+
"PassRate": "44%",
30773077
"Difficulty": "Medium",
30783078
"IsAccepted": false,
30793079
"IsPaid": true,
@@ -3301,7 +3301,7 @@
33013301
"ID": 273,
33023302
"Title": "Integer to English Words",
33033303
"TitleSlug": "integer-to-english-words",
3304-
"PassRate": "23%",
3304+
"PassRate": "24%",
33053305
"Difficulty": "Hard",
33063306
"IsAccepted": true,
33073307
"IsPaid": false,
@@ -3661,7 +3661,7 @@
36613661
"ID": 303,
36623662
"Title": "Range Sum Query - Immutable",
36633663
"TitleSlug": "range-sum-query-immutable",
3664-
"PassRate": "36%",
3664+
"PassRate": "37%",
36653665
"Difficulty": "Easy",
36663666
"IsAccepted": true,
36673667
"IsPaid": false,
@@ -4165,7 +4165,7 @@
41654165
"ID": 345,
41664166
"Title": "Reverse Vowels of a String",
41674167
"TitleSlug": "reverse-vowels-of-a-string",
4168-
"PassRate": "40%",
4168+
"PassRate": "41%",
41694169
"Difficulty": "Easy",
41704170
"IsAccepted": true,
41714171
"IsPaid": false,
@@ -4261,7 +4261,7 @@
42614261
"ID": 353,
42624262
"Title": "Design Snake Game",
42634263
"TitleSlug": "design-snake-game",
4264-
"PassRate": "29%",
4264+
"PassRate": "30%",
42654265
"Difficulty": "Medium",
42664266
"IsAccepted": false,
42674267
"IsPaid": true,
@@ -6121,7 +6121,7 @@
61216121
"ID": 508,
61226122
"Title": "Most Frequent Subtree Sum",
61236123
"TitleSlug": "most-frequent-subtree-sum",
6124-
"PassRate": "53%",
6124+
"PassRate": "54%",
61256125
"Difficulty": "Medium",
61266126
"IsAccepted": true,
61276127
"IsPaid": false,
@@ -8473,7 +8473,7 @@
84738473
"ID": 704,
84748474
"Title": "Binary Search",
84758475
"TitleSlug": "binary-search",
8476-
"PassRate": "45%",
8476+
"PassRate": "46%",
84778477
"Difficulty": "Easy",
84788478
"IsAccepted": true,
84798479
"IsPaid": false,
@@ -8497,7 +8497,7 @@
84978497
"ID": 706,
84988498
"Title": "Design HashMap",
84998499
"TitleSlug": "design-hashmap",
8500-
"PassRate": "54%",
8500+
"PassRate": "55%",
85018501
"Difficulty": "Easy",
85028502
"IsAccepted": true,
85038503
"IsPaid": false,
@@ -8629,7 +8629,7 @@
86298629
"ID": 717,
86308630
"Title": "1-bit and 2-bit Characters",
86318631
"TitleSlug": "1-bit-and-2-bit-characters",
8632-
"PassRate": "48%",
8632+
"PassRate": "49%",
86338633
"Difficulty": "Easy",
86348634
"IsAccepted": true,
86358635
"IsPaid": false,
@@ -8665,7 +8665,7 @@
86658665
"ID": 720,
86668666
"Title": "Longest Word in Dictionary",
86678667
"TitleSlug": "longest-word-in-dictionary",
8668-
"PassRate": "43%",
8668+
"PassRate": "44%",
86698669
"Difficulty": "Easy",
86708670
"IsAccepted": true,
86718671
"IsPaid": false,
@@ -8785,7 +8785,7 @@
87858785
"ID": 730,
87868786
"Title": "Count Different Palindromic Subsequences",
87878787
"TitleSlug": "count-different-palindromic-subsequences",
8788-
"PassRate": "37%",
8788+
"PassRate": "38%",
87898789
"Difficulty": "Hard",
87908790
"IsAccepted": true,
87918791
"IsPaid": false,
@@ -8869,7 +8869,7 @@
88698869
"ID": 737,
88708870
"Title": "Sentence Similarity II",
88718871
"TitleSlug": "sentence-similarity-ii",
8872-
"PassRate": "42%",
8872+
"PassRate": "43%",
88738873
"Difficulty": "Medium",
88748874
"IsAccepted": false,
88758875
"IsPaid": true,
@@ -10861,7 +10861,7 @@
1086110861
"ID": 903,
1086210862
"Title": "Valid Permutations for DI Sequence",
1086310863
"TitleSlug": "valid-permutations-for-di-sequence",
10864-
"PassRate": "42%",
10864+
"PassRate": "41%",
1086510865
"Difficulty": "Hard",
1086610866
"IsAccepted": true,
1086710867
"IsPaid": false,
@@ -11065,7 +11065,7 @@
1106511065
"ID": 920,
1106611066
"Title": "Number of Music Playlists",
1106711067
"TitleSlug": "number-of-music-playlists",
11068-
"PassRate": "43%",
11068+
"PassRate": "42%",
1106911069
"Difficulty": "Hard",
1107011070
"IsAccepted": true,
1107111071
"IsPaid": false,
@@ -11185,7 +11185,7 @@
1118511185
"ID": 930,
1118611186
"Title": "Binary Subarrays With Sum",
1118711187
"TitleSlug": "binary-subarrays-with-sum",
11188-
"PassRate": "37%",
11188+
"PassRate": "36%",
1118911189
"Difficulty": "Medium",
1119011190
"IsAccepted": true,
1119111191
"IsPaid": false,
@@ -11725,7 +11725,7 @@
1172511725
"ID": 975,
1172611726
"Title": "Odd Even Jump",
1172711727
"TitleSlug": "odd-even-jump",
11728-
"PassRate": "50%",
11728+
"PassRate": "49%",
1172911729
"Difficulty": "Hard",
1173011730
"IsAccepted": false,
1173111731
"IsPaid": false,
@@ -12097,7 +12097,7 @@
1209712097
"ID": 1006,
1209812098
"Title": "Clumsy Factorial",
1209912099
"TitleSlug": "clumsy-factorial",
12100-
"PassRate": "55%",
12100+
"PassRate": "54%",
1210112101
"Difficulty": "Medium",
1210212102
"IsAccepted": false,
1210312103
"IsPaid": false,
@@ -12265,7 +12265,7 @@
1226512265
"ID": 1020,
1226612266
"Title": "Partition Array Into Three Parts With Equal Sum",
1226712267
"TitleSlug": "partition-array-into-three-parts-with-equal-sum",
12268-
"PassRate": "51%",
12268+
"PassRate": "52%",
1226912269
"Difficulty": "Easy",
1227012270
"IsAccepted": false,
1227112271
"IsPaid": false,
@@ -12277,7 +12277,7 @@
1227712277
"ID": 1021,
1227812278
"Title": "Best Sightseeing Pair",
1227912279
"TitleSlug": "best-sightseeing-pair",
12280-
"PassRate": "41%",
12280+
"PassRate": "43%",
1228112281
"Difficulty": "Medium",
1228212282
"IsAccepted": false,
1228312283
"IsPaid": false,
@@ -12289,7 +12289,7 @@
1228912289
"ID": 1022,
1229012290
"Title": "Smallest Integer Divisible by K",
1229112291
"TitleSlug": "smallest-integer-divisible-by-k",
12292-
"PassRate": "22%",
12292+
"PassRate": "23%",
1229312293
"Difficulty": "Medium",
1229412294
"IsAccepted": false,
1229512295
"IsPaid": false,

0 commit comments

Comments
 (0)