Skip to content

Commit

Permalink
feat: add solutions to lcci problems
Browse files Browse the repository at this point in the history
- No.03.02.Min Stack
- No.03.03.Stack of Plates
- No.03.04.Implement Queue using Stacks
- No.03.05.Sort of Stacks
  • Loading branch information
YangFong committed May 23, 2022
1 parent 41760dc commit 3d6128e
Show file tree
Hide file tree
Showing 13 changed files with 845 additions and 0 deletions.
99 changes: 99 additions & 0 deletions lcci/03.02.Min Stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,51 @@ class MinStack {
*/
```

### **C++**

```cpp
class MinStack {
private:
stack<int> stk;
stack<int> minStk;
public:
/** initialize your data structure here. */
MinStack() = default;

void push(int x) {
if (minStk.empty() || minStk.top() >= x) {
minStk.push(x);
}
stk.push(x);
}

void pop() {
int val = stk.top();
stk.pop();
if (val == minStk.top()) {
minStk.pop();
}
}

int top() {
return stk.top();
}

int getMin() {
return minStk.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
```

### **TypeScript**

```ts
Expand Down Expand Up @@ -188,6 +233,60 @@ func (this *MinStack) GetMin() int {
*/
```

### **Rust**

```rust
use std::collections::VecDeque;
struct MinStack {
stack: VecDeque<i32>,
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
}

fn push(&mut self, x: i32) {
self.stack.push_back(x);
if self.min_stack.is_empty() || *self.min_stack.back().unwrap() >= x {
self.min_stack.push_back(x);
}
}

fn pop(&mut self) {
let val = self.stack.pop_back().unwrap();
if *self.min_stack.back().unwrap() == val {
self.min_stack.pop_back();
}
}

fn top(&self) -> i32 {
*self.stack.back().unwrap()
}

fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/
```

### **...**

```
Expand Down
99 changes: 99 additions & 0 deletions lcci/03.02.Min Stack/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,51 @@ class MinStack {
*/
```

### **C++**

```cpp
class MinStack {
private:
stack<int> stk;
stack<int> minStk;
public:
/** initialize your data structure here. */
MinStack() = default;

void push(int x) {
if (minStk.empty() || minStk.top() >= x) {
minStk.push(x);
}
stk.push(x);
}

void pop() {
int val = stk.top();
stk.pop();
if (val == minStk.top()) {
minStk.pop();
}
}

int top() {
return stk.top();
}

int getMin() {
return minStk.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
```

### **TypeScript**

```ts
Expand Down Expand Up @@ -199,6 +244,60 @@ func (this *MinStack) GetMin() int {
*/
```

### **Rust**

```rust
use std::collections::VecDeque;
struct MinStack {
stack: VecDeque<i32>,
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
}

fn push(&mut self, x: i32) {
self.stack.push_back(x);
if self.min_stack.is_empty() || *self.min_stack.back().unwrap() >= x {
self.min_stack.push_back(x);
}
}

fn pop(&mut self) {
let val = self.stack.pop_back().unwrap();
if *self.min_stack.back().unwrap() == val {
self.min_stack.pop_back();
}
}

fn top(&self) -> i32 {
*self.stack.back().unwrap()
}

fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/
```

### **...**

```
Expand Down
40 changes: 40 additions & 0 deletions lcci/03.02.Min Stack/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class MinStack {
private:
stack<int> stk;
stack<int> minStk;
public:
/** initialize your data structure here. */
MinStack() = default;

void push(int x) {
if (minStk.empty() || minStk.top() >= x) {
minStk.push(x);
}
stk.push(x);
}

void pop() {
int val = stk.top();
stk.pop();
if (val == minStk.top()) {
minStk.pop();
}
}

int top() {
return stk.top();
}

int getMin() {
return minStk.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(x);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
49 changes: 49 additions & 0 deletions lcci/03.02.Min Stack/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::collections::VecDeque;
struct MinStack {
stack: VecDeque<i32>,
min_stack: VecDeque<i32>,
}


/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MinStack {

/** initialize your data structure here. */
fn new() -> Self {
Self { stack: VecDeque::new(), min_stack: VecDeque::new() }
}

fn push(&mut self, x: i32) {
self.stack.push_back(x);
if self.min_stack.is_empty() || *self.min_stack.back().unwrap() >= x {
self.min_stack.push_back(x);
}
}

fn pop(&mut self) {
let val = self.stack.pop_back().unwrap();
if *self.min_stack.back().unwrap() == val {
self.min_stack.pop_back();
}
}

fn top(&self) -> i32 {
*self.stack.back().unwrap()
}

fn get_min(&self) -> i32 {
*self.min_stack.back().unwrap()
}
}

/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack::new();
* obj.push(x);
* obj.pop();
* let ret_3: i32 = obj.top();
* let ret_4: i32 = obj.get_min();
*/
60 changes: 60 additions & 0 deletions lcci/03.03.Stack of Plates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@

```

### **TypeScript**

```ts
class StackOfPlates {
private cap: number;
private stacks: number[][];

constructor(cap: number) {
this.cap = cap;
this.stacks = [];
}

push(val: number): void {
if (this.cap === 0) {
return;
}
const n = this.stacks.length;
const stack = this.stacks[n - 1];
if (stack == null || stack.length === this.cap) {
this.stacks.push([val]);
} else {
stack.push(val);
}
}

pop(): number {
const n = this.stacks.length;
if (n === 0) {
return -1;
}
const stack = this.stacks[n - 1];
const res = stack.pop();
if (stack.length === 0) {
this.stacks.pop();
}
return res;
}

popAt(index: number): number {
if (index >= this.stacks.length) {
return -1;
}
const stack = this.stacks[index];
const res = stack.pop();
if (stack.length === 0) {
this.stacks.splice(index, 1);
}
return res;
}
}

/**
* Your StackOfPlates object will be instantiated and called as such:
* var obj = new StackOfPlates(cap)
* obj.push(val)
* var param_2 = obj.pop()
* var param_3 = obj.popAt(index)
*/
```

### **...**

```
Expand Down
Loading

0 comments on commit 3d6128e

Please sign in to comment.