Skip to content

Commit

Permalink
feat: add solutions to lcci problems: No.03.03,03.04 (#2532)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Apr 3, 2024
1 parent 024a09d commit 9391bf2
Show file tree
Hide file tree
Showing 14 changed files with 604 additions and 365 deletions.
20 changes: 15 additions & 5 deletions lcci/03.03.Stack of Plates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@

### 方法一:模拟

用列表模拟栈的集合,每个栈的容量为 `cap`,当栈满时,新建一个栈。
我们可以使用一个栈列表 $stk$ 来模拟这个过程,初始时 $stk$ 为空。

- 当调用 $push$ 方法时,如果 $cap$ 为 0,直接返回。否则,如果 $stk$ 为空或者 $stk$ 的最后一个栈的长度大于等于 $cap$,则新建一个栈。然后将元素 $val$ 加入到 $stk$ 的最后一个栈中。时间复杂度为 $O(1)$。
- 当调用 $pop$ 方法时,返回 $popAt(|stk| - 1)$ 的结果。时间复杂度 $O(1)$。
- 当调用 $popAt$ 方法时,如果 $index$ 不在 $[0, |stk|)$ 范围内,返回 -1。否则,返回 $stk[index]$ 的栈顶元素,并将其弹出。如果弹出后 $stk[index]$ 为空,将其从 $stk$ 中删除。时间复杂度 $O(1)$。

空间复杂度为 $O(n)$,其中 $n$ 为元素个数。

<!-- tabs:start -->

Expand Down Expand Up @@ -114,9 +120,13 @@ public:
}

void push(int val) {
if (!cap) return;
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
stk[stk.size() - 1].push(val);
if (!cap) {
return;
}
if (stk.empty() || stk.back().size() >= cap) {
stk.emplace_back(stack<int>());
}
stk.back().push(val);
}

int pop() {
Expand All @@ -136,8 +146,8 @@ public:
}

private:
vector<stack<int>> stk;
int cap;
vector<stack<int>> stk;
};

/**
Expand Down
22 changes: 17 additions & 5 deletions lcci/03.03.Stack of Plates/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@

## Solutions

### Solution 1
### Solution 1: Simulation

We can use a list of stacks $stk$ to simulate this process, initially $stk$ is empty.

- When the `push` method is called, if $cap$ is 0, return directly. Otherwise, if $stk$ is empty or the length of the last stack in $stk$ is greater than or equal to $cap$, then create a new stack. Then add the element $val$ to the last stack in $stk$. The time complexity is $O(1)$.
- When the `pop` method is called, return the result of `popAt(|stk| - 1)`. The time complexity is $O(1)$.
- When the `popAt` method is called, if $index$ is not in the range $[0, |stk|)$, return -1. Otherwise, return the top element of $stk[index]$ and pop it out. If $stk[index]$ is empty after popping, remove it from $stk$. The time complexity is $O(1)$.

The space complexity is $O(n)$, where $n$ is the number of elements.

<!-- tabs:start -->

Expand Down Expand Up @@ -127,9 +135,13 @@ public:
}

void push(int val) {
if (!cap) return;
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
stk[stk.size() - 1].push(val);
if (!cap) {
return;
}
if (stk.empty() || stk.back().size() >= cap) {
stk.emplace_back(stack<int>());
}
stk.back().push(val);
}

int pop() {
Expand All @@ -149,8 +161,8 @@ public:
}

private:
vector<stack<int>> stk;
int cap;
vector<stack<int>> stk;
};

/**
Expand Down
12 changes: 8 additions & 4 deletions lcci/03.03.Stack of Plates/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ class StackOfPlates {
}

void push(int val) {
if (!cap) return;
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
stk[stk.size() - 1].push(val);
if (!cap) {
return;
}
if (stk.empty() || stk.back().size() >= cap) {
stk.emplace_back(stack<int>());
}
stk.back().push(val);
}

int pop() {
Expand All @@ -27,8 +31,8 @@ class StackOfPlates {
}

private:
vector<stack<int>> stk;
int cap;
vector<stack<int>> stk;
};

/**
Expand Down
Loading

0 comments on commit 9391bf2

Please sign in to comment.