Skip to content

Commit 95f6645

Browse files
committed
update
1 parent 81063f6 commit 95f6645

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

004.单调栈.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright (C) 2022 lazywhite <[email protected]>
3+
//
4+
// Distributed under terms of the MIT license.
5+
//
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
func main() {
14+
nums := []int{10, 3, 7, 4, 12}
15+
ans := monoStack(nums)
16+
fmt.Println(nums)
17+
fmt.Println(ans)
18+
}
19+
20+
/*
21+
1. 下一个, 从前向后遍历
22+
上一个, 从后向前遍历
23+
2. smarller 弹出比v大的
24+
2. larger 弹出比v小的
25+
26+
*/
27+
28+
func monoStack(nums []int) []int {
29+
// stack存储num的index
30+
stack := make([]int, 0)
31+
ans := make([]int, len(nums))
32+
33+
for i, v := range nums {
34+
if len(stack) == 0 {
35+
stack = append(stack, i)
36+
} else {
37+
for len(stack) > 0 && nums[stack[len(stack)-1]] > v {
38+
// 更新结果
39+
peek := stack[len(stack)-1]
40+
ans[peek] = v
41+
stack = stack[:len(stack)-1]
42+
}
43+
// 入栈
44+
stack = append(stack, i)
45+
}
46+
}
47+
48+
// stack不为空
49+
if len(stack) > 0 {
50+
for _, idx := range stack {
51+
ans[idx] = -1
52+
}
53+
}
54+
return ans
55+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* @lc app=leetcode.cn id=1475 lang=golang
3+
*
4+
* [1475] 商品折扣后的最终价格
5+
*
6+
* https://leetcode-cn.com/problems/final-prices-with-a-special-discount-in-a-shop/description/
7+
*
8+
* algorithms
9+
* Easy (71.50%)
10+
* Likes: 61
11+
* Dislikes: 0
12+
* Total Accepted: 19.3K
13+
* Total Submissions: 27K
14+
* Testcase Example: '[8,4,6,2,3]'
15+
*
16+
* 给你一个数组 prices ,其中 prices[i] 是商店里第 i 件商品的价格。
17+
*
18+
* 商店里正在进行促销活动,如果你要买第 i 件商品,那么你可以得到与 prices[j] 相等的折扣,其中 j 是满足 j > i 且 prices[j]
19+
* <= prices[i] 的 最小下标 ,如果没有满足条件的 j ,你将没有任何折扣。
20+
*
21+
* 请你返回一个数组,数组中第 i 个元素是折扣后你购买商品 i 最终需要支付的价格。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
* 输入:prices = [8,4,6,2,3]
28+
* 输出:[4,2,4,2,3]
29+
* 解释:
30+
* 商品 0 的价格为 price[0]=8 ,你将得到 prices[1]=4 的折扣,所以最终价格为 8 - 4 = 4 。
31+
* 商品 1 的价格为 price[1]=4 ,你将得到 prices[3]=2 的折扣,所以最终价格为 4 - 2 = 2 。
32+
* 商品 2 的价格为 price[2]=6 ,你将得到 prices[3]=2 的折扣,所以最终价格为 6 - 2 = 4 。
33+
* 商品 3 和 4 都没有折扣。
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
* 输入:prices = [1,2,3,4,5]
39+
* 输出:[1,2,3,4,5]
40+
* 解释:在这个例子中,所有商品都没有折扣。
41+
*
42+
*
43+
* 示例 3:
44+
*
45+
* 输入:prices = [10,1,1,6]
46+
* 输出:[9,0,1,6]
47+
*
48+
*
49+
*
50+
*
51+
* 提示:
52+
*
53+
*
54+
* 1 <= prices.length <= 500
55+
* 1 <= prices[i] <= 10^3
56+
*
57+
*
58+
*/
59+
60+
// @lc code=start
61+
func finalPrices(prices []int) []int {
62+
stack := make([]int, 0)
63+
ans := make([]int, len(prices))
64+
for i, v := range prices {
65+
if len(stack) == 0 {
66+
stack = append(stack, i)
67+
} else {
68+
for len(stack) > 0 && prices[stack[len(stack)-1]] >= v {
69+
peekIdx := stack[len(stack)-1]
70+
ans[peekIdx] = prices[peekIdx] - v
71+
stack = stack[:len(stack)-1]
72+
}
73+
stack = append(stack, i)
74+
}
75+
}
76+
77+
for _, idx := range stack {
78+
ans[idx] = prices[idx]
79+
}
80+
return ans
81+
}
82+
83+
// @lc code=end
84+

0 commit comments

Comments
 (0)