Skip to content

Commit d8d118b

Browse files
committed
feat: add solutions to lc problems: No.0155,1249,1823
- No.0155.Min Stack - No.1249.Minimum Remove to Make Valid Parentheses - No.1823.Find the Winner of the Circular Game
1 parent f221274 commit d8d118b

File tree

12 files changed

+740
-1
lines changed

12 files changed

+740
-1
lines changed

solution/0100-0199/0155.Min Stack/README.md

+135
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,36 @@ public:
184184
*/
185185
```
186186

187+
### **C#**
188+
189+
```cs
190+
using System.Collections.Generic;
191+
192+
public class MinStack {
193+
private Stack<int> _stack = new Stack<int>();
194+
private Stack<int> _minStack = new Stack<int>();
195+
196+
public void Push(int x) {
197+
_stack.Push(x);
198+
if (GetMin() >= x) _minStack.Push(x);
199+
}
200+
201+
public void Pop() {
202+
var x = _stack.Pop();
203+
if (GetMin() == x) _minStack.Pop();
204+
}
205+
206+
public int Top() {
207+
return _stack.Peek();
208+
}
209+
210+
public int GetMin() {
211+
if (_minStack.Count == 0) return int.MaxValue;
212+
return _minStack.Peek();
213+
}
214+
}
215+
```
216+
187217
### **JavaScript**
188218

189219
```js
@@ -236,6 +266,111 @@ MinStack.prototype.getMin = function () {
236266
*/
237267
```
238268

269+
### **TypeScript**
270+
271+
```ts
272+
class MinStack {
273+
private stack: number[];
274+
private min: number[];
275+
276+
constructor() {
277+
this.stack = [];
278+
this.min = [];
279+
}
280+
281+
push(val: number): void {
282+
this.stack.push(val);
283+
if (val <= (this.min[this.min.length - 1] ?? Infinity)) {
284+
this.min.push(val);
285+
}
286+
}
287+
288+
pop(): void {
289+
if (this.stack.pop() === this.min[this.min.length - 1]) {
290+
this.min.pop();
291+
}
292+
}
293+
294+
top(): number {
295+
return this.stack[this.stack.length - 1];
296+
}
297+
298+
getMin(): number {
299+
return this.min[this.min.length - 1];
300+
}
301+
}
302+
303+
/**
304+
* Your MinStack object will be instantiated and called as such:
305+
* var obj = new MinStack()
306+
* obj.push(val)
307+
* obj.pop()
308+
* var param_3 = obj.top()
309+
* var param_4 = obj.getMin()
310+
*/
311+
```
312+
313+
### **Rust**
314+
315+
```rust
316+
struct MinStack {
317+
items: Vec<i32>,
318+
min: Vec<i32>,
319+
}
320+
321+
322+
/**
323+
* `&self` means the method takes an immutable reference.
324+
* If you need a mutable reference, change it to `&mut self` instead.
325+
*/
326+
impl MinStack {
327+
328+
/** initialize your data structure here. */
329+
fn new() -> Self {
330+
MinStack {
331+
items: Vec::new(),
332+
min: Vec::new(),
333+
}
334+
}
335+
336+
fn push(&mut self, x: i32) {
337+
self.items.push(x);
338+
match self.min.last() {
339+
Some(min) => {
340+
if min >= &x {
341+
self.min.push(x);
342+
}
343+
},
344+
None => self.min.push(x),
345+
}
346+
}
347+
348+
fn pop(&mut self) {
349+
if &self.items.pop().unwrap() == self.min.last().unwrap() {
350+
self.min.pop();
351+
}
352+
}
353+
354+
fn top(&self) -> i32 {
355+
*self.items.last().unwrap()
356+
}
357+
358+
fn get_min(&self) -> i32 {
359+
*self.min.last().unwrap()
360+
}
361+
}
362+
363+
364+
/**
365+
* Your MinStack object will be instantiated and called as such:
366+
* let obj = MinStack::new();
367+
* obj.push(val);
368+
* obj.pop();
369+
* let ret_3: i32 = obj.top();
370+
* let ret_4: i32 = obj.get_min();
371+
*/
372+
```
373+
239374
### **...**
240375

241376
```

solution/0100-0199/0155.Min Stack/README_EN.md

+135
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ public:
172172
*/
173173
```
174174

175+
### **C#**
176+
177+
```cs
178+
using System.Collections.Generic;
179+
180+
public class MinStack {
181+
private Stack<int> _stack = new Stack<int>();
182+
private Stack<int> _minStack = new Stack<int>();
183+
184+
public void Push(int x) {
185+
_stack.Push(x);
186+
if (GetMin() >= x) _minStack.Push(x);
187+
}
188+
189+
public void Pop() {
190+
var x = _stack.Pop();
191+
if (GetMin() == x) _minStack.Pop();
192+
}
193+
194+
public int Top() {
195+
return _stack.Peek();
196+
}
197+
198+
public int GetMin() {
199+
if (_minStack.Count == 0) return int.MaxValue;
200+
return _minStack.Peek();
201+
}
202+
}
203+
```
204+
175205
### **JavaScript**
176206

177207
```js
@@ -224,6 +254,111 @@ MinStack.prototype.getMin = function () {
224254
*/
225255
```
226256

257+
### **TypeScript**
258+
259+
```ts
260+
class MinStack {
261+
private stack: number[];
262+
private min: number[];
263+
264+
constructor() {
265+
this.stack = [];
266+
this.min = [];
267+
}
268+
269+
push(val: number): void {
270+
this.stack.push(val);
271+
if (val <= (this.min[this.min.length - 1] ?? Infinity)) {
272+
this.min.push(val);
273+
}
274+
}
275+
276+
pop(): void {
277+
if (this.stack.pop() === this.min[this.min.length - 1]) {
278+
this.min.pop();
279+
}
280+
}
281+
282+
top(): number {
283+
return this.stack[this.stack.length - 1];
284+
}
285+
286+
getMin(): number {
287+
return this.min[this.min.length - 1];
288+
}
289+
}
290+
291+
/**
292+
* Your MinStack object will be instantiated and called as such:
293+
* var obj = new MinStack()
294+
* obj.push(val)
295+
* obj.pop()
296+
* var param_3 = obj.top()
297+
* var param_4 = obj.getMin()
298+
*/
299+
```
300+
301+
### **Rust**
302+
303+
```rust
304+
struct MinStack {
305+
items: Vec<i32>,
306+
min: Vec<i32>,
307+
}
308+
309+
310+
/**
311+
* `&self` means the method takes an immutable reference.
312+
* If you need a mutable reference, change it to `&mut self` instead.
313+
*/
314+
impl MinStack {
315+
316+
/** initialize your data structure here. */
317+
fn new() -> Self {
318+
MinStack {
319+
items: Vec::new(),
320+
min: Vec::new(),
321+
}
322+
}
323+
324+
fn push(&mut self, x: i32) {
325+
self.items.push(x);
326+
match self.min.last() {
327+
Some(min) => {
328+
if min >= &x {
329+
self.min.push(x);
330+
}
331+
},
332+
None => self.min.push(x),
333+
}
334+
}
335+
336+
fn pop(&mut self) {
337+
if &self.items.pop().unwrap() == self.min.last().unwrap() {
338+
self.min.pop();
339+
}
340+
}
341+
342+
fn top(&self) -> i32 {
343+
*self.items.last().unwrap()
344+
}
345+
346+
fn get_min(&self) -> i32 {
347+
*self.min.last().unwrap()
348+
}
349+
}
350+
351+
352+
/**
353+
* Your MinStack object will be instantiated and called as such:
354+
* let obj = MinStack::new();
355+
* obj.push(val);
356+
* obj.pop();
357+
* let ret_3: i32 = obj.top();
358+
* let ret_4: i32 = obj.get_min();
359+
*/
360+
```
361+
227362
### **...**
228363

229364
```

solution/0100-0199/0155.Min Stack/Solution.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class MinStack {
44
private Stack<int> _stack = new Stack<int>();
55
private Stack<int> _minStack = new Stack<int>();
6-
6+
77
public void Push(int x) {
88
_stack.Push(x);
99
if (GetMin() >= x) _minStack.Push(x);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
struct MinStack {
2+
items: Vec<i32>,
3+
min: Vec<i32>,
4+
}
5+
6+
7+
/**
8+
* `&self` means the method takes an immutable reference.
9+
* If you need a mutable reference, change it to `&mut self` instead.
10+
*/
11+
impl MinStack {
12+
13+
/** initialize your data structure here. */
14+
fn new() -> Self {
15+
MinStack {
16+
items: Vec::new(),
17+
min: Vec::new(),
18+
}
19+
}
20+
21+
fn push(&mut self, x: i32) {
22+
self.items.push(x);
23+
match self.min.last() {
24+
Some(min) => {
25+
if min >= &x {
26+
self.min.push(x);
27+
}
28+
},
29+
None => self.min.push(x),
30+
}
31+
}
32+
33+
fn pop(&mut self) {
34+
if &self.items.pop().unwrap() == self.min.last().unwrap() {
35+
self.min.pop();
36+
}
37+
}
38+
39+
fn top(&self) -> i32 {
40+
*self.items.last().unwrap()
41+
}
42+
43+
fn get_min(&self) -> i32 {
44+
*self.min.last().unwrap()
45+
}
46+
}
47+
48+
49+
/**
50+
* Your MinStack object will be instantiated and called as such:
51+
* let obj = MinStack::new();
52+
* obj.push(val);
53+
* obj.pop();
54+
* let ret_3: i32 = obj.top();
55+
* let ret_4: i32 = obj.get_min();
56+
*/

0 commit comments

Comments
 (0)