Skip to content

Commit e968d90

Browse files
authored
feat: add solutions to lc problem: No.2297 (doocs#1284)
No.2297.Jump Game VIII
1 parent 4230bc2 commit e968d90

File tree

11 files changed

+545
-13
lines changed

11 files changed

+545
-13
lines changed

solution/0700-0799/0771.Jewels and Stones/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
我们可以先用一个哈希表或数组 $s$ 记录所有宝石的类型。然后遍历所有石头,如果当前石头是宝石,就将答案加一。
4646

47-
时间复杂度 $O(m+n)$其中 $m$ 和 $n$ 分别是字符串 $jewels$ 和 $stones$ 的长度。
47+
时间复杂度 $O(m+n)$,空间复杂度 $O(|\Sigma|)$,其中 $m$ 和 $n$ 分别是字符串 $jewels$ 和 $stones$ 的长度,而 $\Sigma$ 是字符集,本题中字符集为所有大小写英文字母的集合
4848

4949
<!-- tabs:start -->
5050

@@ -98,7 +98,7 @@ public:
9898
9999
```go
100100
func numJewelsInStones(jewels string, stones string) (ans int) {
101-
s := make([]int, 128)
101+
s := [128]int{}
102102
for _, c := range jewels {
103103
s[c] = 1
104104
}
@@ -127,10 +127,10 @@ var numJewelsInStones = function (jewels, stones) {
127127

128128
```ts
129129
function numJewelsInStones(jewels: string, stones: string): number {
130-
const set = new Set([...jewels]);
130+
const s = new Set([...jewels]);
131131
let ans = 0;
132132
for (const c of stones) {
133-
set.has(c) && ans++;
133+
s.has(c) && ans++;
134134
}
135135
return ans;
136136
}

solution/0700-0799/0771.Jewels and Stones/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public:
7575
7676
```go
7777
func numJewelsInStones(jewels string, stones string) (ans int) {
78-
s := make([]int, 128)
78+
s := [128]int{}
7979
for _, c := range jewels {
8080
s[c] = 1
8181
}
@@ -104,10 +104,10 @@ var numJewelsInStones = function (jewels, stones) {
104104

105105
```ts
106106
function numJewelsInStones(jewels: string, stones: string): number {
107-
const set = new Set([...jewels]);
107+
const s = new Set([...jewels]);
108108
let ans = 0;
109109
for (const c of stones) {
110-
set.has(c) && ans++;
110+
s.has(c) && ans++;
111111
}
112112
return ans;
113113
}

solution/0700-0799/0771.Jewels and Stones/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
func numJewelsInStones(jewels string, stones string) (ans int) {
2-
s := make([]int, 128)
2+
s := [128]int{}
33
for _, c := range jewels {
44
s[c] = 1
55
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function numJewelsInStones(jewels: string, stones: string): number {
2-
const set = new Set([...jewels]);
2+
const s = new Set([...jewels]);
33
let ans = 0;
44
for (const c of stones) {
5-
set.has(c) && ans++;
5+
s.has(c) && ans++;
66
}
77
return ans;
88
}

solution/2200-2299/2297.Jump Game VIII/README.md

+187-2
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,213 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:单调栈 + 动态规划**
59+
60+
根据题目描述,我们实际上需要找到 $nums[i]$ 的下一个大于等于 $nums[i]$ 的位置 $j$,以及下一个小于 $nums[i]$ 的位置 $j$。我们利用单调栈可以在 $O(n)$ 的时间内找到这两个位置,然后构建邻接表 $g$,其中 $g[i]$ 表示下标 $i$ 可以跳转到的下标。
61+
62+
然后我们使用动态规划求解最小代价。设 $f[i]$ 表示跳转到下标 $i$ 的最小代价,初始时 $f[0] = 0$,其余 $f[i] = \infty$。我们从小到大枚举下标 $i$,对于每个 $i$,我们枚举 $g[i]$ 中的每个下标 $j$,进行状态转移 $f[j] = \min(f[j], f[i] + costs[j])$。答案为 $f[n - 1]$。
63+
64+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组长度。
65+
5866
<!-- tabs:start -->
5967

6068
### **Python3**
6169

6270
<!-- 这里可写当前语言的特殊实现逻辑 -->
6371

6472
```python
65-
73+
class Solution:
74+
def minCost(self, nums: List[int], costs: List[int]) -> int:
75+
n = len(nums)
76+
g = defaultdict(list)
77+
stk = []
78+
for i in range(n - 1, -1, -1):
79+
while stk and nums[stk[-1]] < nums[i]:
80+
stk.pop()
81+
if stk:
82+
g[i].append(stk[-1])
83+
stk.append(i)
84+
85+
stk = []
86+
for i in range(n - 1, -1, -1):
87+
while stk and nums[stk[-1]] >= nums[i]:
88+
stk.pop()
89+
if stk:
90+
g[i].append(stk[-1])
91+
stk.append(i)
92+
93+
f = [inf] * n
94+
f[0] = 0
95+
for i in range(n):
96+
for j in g[i]:
97+
f[j] = min(f[j], f[i] + costs[j])
98+
return f[n - 1]
6699
```
67100

68101
### **Java**
69102

70103
<!-- 这里可写当前语言的特殊实现逻辑 -->
71104

72105
```java
106+
class Solution {
107+
public long minCost(int[] nums, int[] costs) {
108+
int n = nums.length;
109+
List<Integer>[] g = new List[n];
110+
Arrays.setAll(g, k -> new ArrayList<>());
111+
Deque<Integer> stk = new ArrayDeque<>();
112+
for (int i = n - 1; i >= 0; --i) {
113+
while (!stk.isEmpty() && nums[stk.peek()] < nums[i]) {
114+
stk.pop();
115+
}
116+
if (!stk.isEmpty()) {
117+
g[i].add(stk.peek());
118+
}
119+
stk.push(i);
120+
}
121+
stk.clear();
122+
for (int i = n - 1; i >= 0; --i) {
123+
while (!stk.isEmpty() && nums[stk.peek()] >= nums[i]) {
124+
stk.pop();
125+
}
126+
if (!stk.isEmpty()) {
127+
g[i].add(stk.peek());
128+
}
129+
stk.push(i);
130+
}
131+
long[] f = new long[n];
132+
Arrays.fill(f, 1L << 60);
133+
f[0] = 0;
134+
for (int i = 0; i < n; ++i) {
135+
for (int j : g[i]) {
136+
f[j] = Math.min(f[j], f[i] + costs[j]);
137+
}
138+
}
139+
return f[n - 1];
140+
}
141+
}
142+
```
143+
144+
### **C++**
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
long long minCost(vector<int>& nums, vector<int>& costs) {
150+
int n = nums.size();
151+
vector<int> g[n];
152+
stack<int> stk;
153+
for (int i = n - 1; ~i; --i) {
154+
while (!stk.empty() && nums[stk.top()] < nums[i]) {
155+
stk.pop();
156+
}
157+
if (!stk.empty()) {
158+
g[i].push_back(stk.top());
159+
}
160+
stk.push(i);
161+
}
162+
stk = stack<int>();
163+
for (int i = n - 1; ~i; --i) {
164+
while (!stk.empty() && nums[stk.top()] >= nums[i]) {
165+
stk.pop();
166+
}
167+
if (!stk.empty()) {
168+
g[i].push_back(stk.top());
169+
}
170+
stk.push(i);
171+
}
172+
vector<long long> f(n, 1e18);
173+
f[0] = 0;
174+
for (int i = 0; i < n; ++i) {
175+
for (int j : g[i]) {
176+
f[j] = min(f[j], f[i] + costs[j]);
177+
}
178+
}
179+
return f[n - 1];
180+
}
181+
};
182+
```
73183
184+
### **Go**
185+
186+
```go
187+
func minCost(nums []int, costs []int) int64 {
188+
n := len(nums)
189+
g := make([][]int, n)
190+
stk := []int{}
191+
for i := n - 1; i >= 0; i-- {
192+
for len(stk) > 0 && nums[stk[len(stk)-1]] < nums[i] {
193+
stk = stk[:len(stk)-1]
194+
}
195+
if len(stk) > 0 {
196+
g[i] = append(g[i], stk[len(stk)-1])
197+
}
198+
stk = append(stk, i)
199+
}
200+
stk = []int{}
201+
for i := n - 1; i >= 0; i-- {
202+
for len(stk) > 0 && nums[stk[len(stk)-1]] >= nums[i] {
203+
stk = stk[:len(stk)-1]
204+
}
205+
if len(stk) > 0 {
206+
g[i] = append(g[i], stk[len(stk)-1])
207+
}
208+
stk = append(stk, i)
209+
}
210+
f := make([]int64, n)
211+
for i := 1; i < n; i++ {
212+
f[i] = math.MaxInt64
213+
}
214+
for i := 0; i < n; i++ {
215+
for _, j := range g[i] {
216+
f[j] = min(f[j], f[i]+int64(costs[j]))
217+
}
218+
}
219+
return f[n-1]
220+
}
221+
222+
func min(a, b int64) int64 {
223+
if a < b {
224+
return a
225+
}
226+
return b
227+
}
74228
```
75229

76230
### **TypeScript**
77231

78232
```ts
79-
233+
function minCost(nums: number[], costs: number[]): number {
234+
const n = nums.length;
235+
const g: number[][] = Array.from({ length: n }, () => []);
236+
const stk: number[] = [];
237+
for (let i = n - 1; i >= 0; --i) {
238+
while (stk.length && nums[stk[stk.length - 1]] < nums[i]) {
239+
stk.pop();
240+
}
241+
if (stk.length) {
242+
g[i].push(stk[stk.length - 1]);
243+
}
244+
stk.push(i);
245+
}
246+
stk.length = 0;
247+
for (let i = n - 1; i >= 0; --i) {
248+
while (stk.length && nums[stk[stk.length - 1]] >= nums[i]) {
249+
stk.pop();
250+
}
251+
if (stk.length) {
252+
g[i].push(stk[stk.length - 1]);
253+
}
254+
stk.push(i);
255+
}
256+
const f: number[] = Array.from({ length: n }, () => Infinity);
257+
f[0] = 0;
258+
for (let i = 0; i < n; ++i) {
259+
for (const j of g[i]) {
260+
f[j] = Math.min(f[j], f[i] + costs[j]);
261+
}
262+
}
263+
return f[n - 1];
264+
}
80265
```
81266

82267
### **...**

0 commit comments

Comments
 (0)