Skip to content

Commit 8de7366

Browse files
authored
Merge pull request #934 from 0xff-dev/1475
Add solution and test-cases for problem 1475
2 parents 44ff323 + c491689 commit 8de7366

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# [1475.Final Prices With a Special Discount in a Shop][title]
2+
3+
## Description
4+
You are given an integer array `prices` where `prices[i]` is the price of the i<sup>th</sup> item in a shop.
5+
6+
There is a special discount for items in the shop. If you buy the i<sup>th</sup> item, then you will receive a discount equivalent to `prices[j]` where `j` is the minimum index such that `j > i` and `prices[j] <= prices[i]`. Otherwise, you will not receive any discount at all.
7+
8+
Return an integer array `answer` where answer[i] is the final price you will pay for the i<sup>th</sup> item of the shop, considering the special discount.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: prices = [8,4,6,2,3]
14+
Output: [4,2,4,2,3]
15+
Explanation:
16+
For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
17+
For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
18+
For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
19+
For items 3 and 4 you will not receive any discount at all.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: prices = [1,2,3,4,5]
26+
Output: [1,2,3,4,5]
27+
Explanation: In this case, for all items, you will not receive any discount at all.
28+
```
29+
30+
**Example 3:**
31+
32+
```
33+
Input: prices = [10,1,1,6]
34+
Output: [9,0,1,6]
35+
```
36+
37+
## 结语
38+
39+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
40+
41+
[title]: https://leetcode.com/problems/final-prices-with-a-special-discount-in-a-shop
42+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(prices []int) []int {
4+
l := len(prices)
5+
dst := make([]int, l)
6+
copy(dst, prices)
7+
for i := l - 2; i >= 0; i-- {
8+
for j := i + 1; j < l; j++ {
9+
if prices[j] <= prices[i] {
10+
dst[i] -= prices[j]
11+
break
12+
}
13+
}
14+
}
15+
return dst
516
}

leetcode/1401-1500/1475.Final-Prices-With-a-Special-Discount-in-a-Shop/Solution_test.go

Lines changed: 7 additions & 7 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
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{8, 4, 6, 2, 3}, []int{4, 2, 4, 2, 3}},
17+
{"TestCase2", []int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}},
18+
{"TestCase3", []int{10, 1, 1, 6}, []int{9, 0, 1, 6}},
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)