Skip to content

Commit 9391bf2

Browse files
authored
feat: add solutions to lcci problems: No.03.03,03.04 (doocs#2532)
1 parent 024a09d commit 9391bf2

File tree

14 files changed

+604
-365
lines changed

14 files changed

+604
-365
lines changed

lcci/03.03.Stack of Plates/README.md

+15-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626

2727
### 方法一:模拟
2828

29-
用列表模拟栈的集合,每个栈的容量为 `cap`,当栈满时,新建一个栈。
29+
我们可以使用一个栈列表 $stk$ 来模拟这个过程,初始时 $stk$ 为空。
30+
31+
- 当调用 $push$ 方法时,如果 $cap$ 为 0,直接返回。否则,如果 $stk$ 为空或者 $stk$ 的最后一个栈的长度大于等于 $cap$,则新建一个栈。然后将元素 $val$ 加入到 $stk$ 的最后一个栈中。时间复杂度为 $O(1)$。
32+
- 当调用 $pop$ 方法时,返回 $popAt(|stk| - 1)$ 的结果。时间复杂度 $O(1)$。
33+
- 当调用 $popAt$ 方法时,如果 $index$ 不在 $[0, |stk|)$ 范围内,返回 -1。否则,返回 $stk[index]$ 的栈顶元素,并将其弹出。如果弹出后 $stk[index]$ 为空,将其从 $stk$ 中删除。时间复杂度 $O(1)$。
34+
35+
空间复杂度为 $O(n)$,其中 $n$ 为元素个数。
3036

3137
<!-- tabs:start -->
3238

@@ -114,9 +120,13 @@ public:
114120
}
115121

116122
void push(int val) {
117-
if (!cap) return;
118-
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
119-
stk[stk.size() - 1].push(val);
123+
if (!cap) {
124+
return;
125+
}
126+
if (stk.empty() || stk.back().size() >= cap) {
127+
stk.emplace_back(stack<int>());
128+
}
129+
stk.back().push(val);
120130
}
121131

122132
int pop() {
@@ -136,8 +146,8 @@ public:
136146
}
137147

138148
private:
139-
vector<stack<int>> stk;
140149
int cap;
150+
vector<stack<int>> stk;
141151
};
142152

143153
/**

lcci/03.03.Stack of Plates/README_EN.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@
3939

4040
## Solutions
4141

42-
### Solution 1
42+
### Solution 1: Simulation
43+
44+
We can use a list of stacks $stk$ to simulate this process, initially $stk$ is empty.
45+
46+
- 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)$.
47+
- When the `pop` method is called, return the result of `popAt(|stk| - 1)`. The time complexity is $O(1)$.
48+
- 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)$.
49+
50+
The space complexity is $O(n)$, where $n$ is the number of elements.
4351

4452
<!-- tabs:start -->
4553

@@ -127,9 +135,13 @@ public:
127135
}
128136

129137
void push(int val) {
130-
if (!cap) return;
131-
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
132-
stk[stk.size() - 1].push(val);
138+
if (!cap) {
139+
return;
140+
}
141+
if (stk.empty() || stk.back().size() >= cap) {
142+
stk.emplace_back(stack<int>());
143+
}
144+
stk.back().push(val);
133145
}
134146

135147
int pop() {
@@ -149,8 +161,8 @@ public:
149161
}
150162

151163
private:
152-
vector<stack<int>> stk;
153164
int cap;
165+
vector<stack<int>> stk;
154166
};
155167

156168
/**

lcci/03.03.Stack of Plates/Solution.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ class StackOfPlates {
55
}
66

77
void push(int val) {
8-
if (!cap) return;
9-
if (stk.empty() || stk[stk.size() - 1].size() >= cap) stk.emplace_back(stack<int>());
10-
stk[stk.size() - 1].push(val);
8+
if (!cap) {
9+
return;
10+
}
11+
if (stk.empty() || stk.back().size() >= cap) {
12+
stk.emplace_back(stack<int>());
13+
}
14+
stk.back().push(val);
1115
}
1216

1317
int pop() {
@@ -27,8 +31,8 @@ class StackOfPlates {
2731
}
2832

2933
private:
30-
vector<stack<int>> stk;
3134
int cap;
35+
vector<stack<int>> stk;
3236
};
3337

3438
/**

0 commit comments

Comments
 (0)