Skip to content

feat: add solutions to lc problems: No.3069,3070 #2406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ myQueue.empty(); // return false

### 方法一:双栈

使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。
我们使用两个栈,其中栈 `stk1`用于入队,另一个栈 `stk2` 用于出队。

入队时,直接将元素入栈 `stk1`。时间复杂度 $O(1)$。

Expand Down Expand Up @@ -282,7 +282,7 @@ class MyQueue {

peek(): number {
this.move();
return this.stk2[this.stk2.length - 1];
return this.stk2.at(-1);
}

empty(): boolean {
Expand All @@ -292,7 +292,7 @@ class MyQueue {
move(): void {
if (!this.stk2.length) {
while (this.stk1.length) {
this.stk2.push(this.stk1.pop());
this.stk2.push(this.stk1.pop()!);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class MyQueue {

peek(): number {
this.move();
return this.stk2[this.stk2.length - 1];
return this.stk2.at(-1);
}

empty(): boolean {
Expand All @@ -270,7 +270,7 @@ class MyQueue {
move(): void {
if (!this.stk2.length) {
while (this.stk1.length) {
this.stk2.push(this.stk1.pop());
this.stk2.push(this.stk1.pop()!);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MyQueue {

peek(): number {
this.move();
return this.stk2[this.stk2.length - 1];
return this.stk2.at(-1);
}

empty(): boolean {
Expand All @@ -28,7 +28,7 @@ class MyQueue {
move(): void {
if (!this.stk2.length) {
while (this.stk1.length) {
this.stk2.push(this.stk1.pop());
this.stk2.push(this.stk1.pop()!);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,103 @@

## 解法

### 方法一
### 方法一:模拟

我们创建两个数组 `arr1` 和 `arr2`,分别存放 `nums` 中的元素,初始时 `arr1` 中只有 `nums[0]`,`arr2` 中只有 `nums[1]`。

然后遍历 `nums` 下标从 $2$ 开始的元素,如果 `arr1` 的最后一个元素大于 `arr2` 的最后一个元素,就将当前元素追加到 `arr1`,否则追加到 `arr2`。

最后将 `arr2` 中的元素追加到 `arr1` 中,返回 `arr1`。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。

<!-- tabs:start -->

```python

class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
arr1 = [nums[0]]
arr2 = [nums[1]]
for x in nums[2:]:
if arr1[-1] > arr2[-1]:
arr1.append(x)
else:
arr2.append(x)
return arr1 + arr2
```

```java

class Solution {
public int[] resultArray(int[] nums) {
int n = nums.length;
int[] arr1 = new int[n];
int[] arr2 = new int[n];
arr1[0] = nums[0];
arr2[0] = nums[1];
int i = 0, j = 0;
for (int k = 2; k < n; ++k) {
if (arr1[i] > arr2[j]) {
arr1[++i] = nums[k];
} else {
arr2[++j] = nums[k];
}
}
for (int k = 0; k <= j; ++k) {
arr1[++i] = arr2[k];
}
return arr1;
}
}
```

```cpp

class Solution {
public:
vector<int> resultArray(vector<int>& nums) {
int n = nums.size();
vector<int> arr1 = {nums[0]};
vector<int> arr2 = {nums[1]};
for (int k = 2; k < n; ++k) {
if (arr1.back() > arr2.back()) {
arr1.push_back(nums[k]);
} else {
arr2.push_back(nums[k]);
}
}
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
return arr1;
}
};
```

```go
func resultArray(nums []int) []int {
arr1 := []int{nums[0]}
arr2 := []int{nums[1]}
for _, x := range nums[2:] {
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
arr1 = append(arr1, x)
} else {
arr2 = append(arr2, x)
}
}
return append(arr1, arr2...)
}
```

```ts
function resultArray(nums: number[]): number[] {
const arr1: number[] = [nums[0]];
const arr2: number[] = [nums[1]];
for (const x of nums.slice(2)) {
if (arr1.at(-1)! > arr2.at(-1)!) {
arr1.push(x);
} else {
arr2.push(x);
}
}
return arr1.concat(arr2);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,103 @@ Hence, the array result formed by concatenation is [5,3,4,8].

## Solutions

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

We create two arrays `arr1` and `arr2`, which store the elements in `nums`. Initially, `arr1` only contains `nums[0]`, and `arr2` only contains `nums[1]`.

Then we traverse the elements of `nums` starting from index 2. If the last element of `arr1` is greater than the last element of `arr2`, we append the current element to `arr1`, otherwise we append it to `arr2`.

Finally, we append the elements in `arr2` to `arr1` and return `arr1`.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the array `nums`.

<!-- tabs:start -->

```python

class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
arr1 = [nums[0]]
arr2 = [nums[1]]
for x in nums[2:]:
if arr1[-1] > arr2[-1]:
arr1.append(x)
else:
arr2.append(x)
return arr1 + arr2
```

```java

class Solution {
public int[] resultArray(int[] nums) {
int n = nums.length;
int[] arr1 = new int[n];
int[] arr2 = new int[n];
arr1[0] = nums[0];
arr2[0] = nums[1];
int i = 0, j = 0;
for (int k = 2; k < n; ++k) {
if (arr1[i] > arr2[j]) {
arr1[++i] = nums[k];
} else {
arr2[++j] = nums[k];
}
}
for (int k = 0; k <= j; ++k) {
arr1[++i] = arr2[k];
}
return arr1;
}
}
```

```cpp

class Solution {
public:
vector<int> resultArray(vector<int>& nums) {
int n = nums.size();
vector<int> arr1 = {nums[0]};
vector<int> arr2 = {nums[1]};
for (int k = 2; k < n; ++k) {
if (arr1.back() > arr2.back()) {
arr1.push_back(nums[k]);
} else {
arr2.push_back(nums[k]);
}
}
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
return arr1;
}
};
```

```go
func resultArray(nums []int) []int {
arr1 := []int{nums[0]}
arr2 := []int{nums[1]}
for _, x := range nums[2:] {
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
arr1 = append(arr1, x)
} else {
arr2 = append(arr2, x)
}
}
return append(arr1, arr2...)
}
```

```ts
function resultArray(nums: number[]): number[] {
const arr1: number[] = [nums[0]];
const arr2: number[] = [nums[1]];
for (const x of nums.slice(2)) {
if (arr1.at(-1)! > arr2.at(-1)!) {
arr1.push(x);
} else {
arr2.push(x);
}
}
return arr1.concat(arr2);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
vector<int> resultArray(vector<int>& nums) {
int n = nums.size();
vector<int> arr1 = {nums[0]};
vector<int> arr2 = {nums[1]};
for (int k = 2; k < n; ++k) {
if (arr1.back() > arr2.back()) {
arr1.push_back(nums[k]);
} else {
arr2.push_back(nums[k]);
}
}
arr1.insert(arr1.end(), arr2.begin(), arr2.end());
return arr1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
func resultArray(nums []int) []int {
arr1 := []int{nums[0]}
arr2 := []int{nums[1]}
for _, x := range nums[2:] {
if arr1[len(arr1)-1] > arr2[len(arr2)-1] {
arr1 = append(arr1, x)
} else {
arr2 = append(arr2, x)
}
}
return append(arr1, arr2...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int[] resultArray(int[] nums) {
int n = nums.length;
int[] arr1 = new int[n];
int[] arr2 = new int[n];
arr1[0] = nums[0];
arr2[0] = nums[1];
int i = 0, j = 0;
for (int k = 2; k < n; ++k) {
if (arr1[i] > arr2[j]) {
arr1[++i] = nums[k];
} else {
arr2[++j] = nums[k];
}
}
for (int k = 0; k <= j; ++k) {
arr1[++i] = arr2[k];
}
return arr1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def resultArray(self, nums: List[int]) -> List[int]:
arr1 = [nums[0]]
arr2 = [nums[1]]
for x in nums[2:]:
if arr1[-1] > arr2[-1]:
arr1.append(x)
else:
arr2.append(x)
return arr1 + arr2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function resultArray(nums: number[]): number[] {
const arr1: number[] = [nums[0]];
const arr2: number[] = [nums[1]];
for (const x of nums.slice(2)) {
if (arr1.at(-1)! > arr2.at(-1)!) {
arr1.push(x);
} else {
arr2.push(x);
}
}
return arr1.concat(arr2);
}
Loading