Skip to content

[pull] main from doocs:main #147

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 5 commits into from
Jun 24, 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
2 changes: 1 addition & 1 deletion solution/0400-0499/0419.Battleships in a Board/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tags:
<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0419.Battleships%20in%20a%20Board/images/battelship-grid.jpg" style="width: 333px; height: 333px;" />
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0400-0499/0419.Battleships%20in%20a%20Board/images/1719200420-KKnzye-image.png" style="width: 333px; height: 333px;" />
<pre>
<strong>输入:</strong>board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
<strong>输出:</strong>2
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
169 changes: 37 additions & 132 deletions solution/0500-0599/0503.Next Greater Element II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,135 +56,15 @@ tags:

<!-- solution:start -->

### 方法一:单调栈 + 循环数组
### 方法一:单调栈

<!-- tabs:start -->

#### Python3

```python
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = [-1] * n
stk = []
for i in range(n << 1):
while stk and nums[stk[-1]] < nums[i % n]:
ans[stk.pop()] = nums[i % n]
stk.append(i % n)
return ans
```

#### Java

```java
class Solution {
public int[] nextGreaterElements(int[] nums) {
int n = nums.length;
int[] ans = new int[n];
Arrays.fill(ans, -1);
Deque<Integer> stk = new ArrayDeque<>();
for (int i = 0; i < (n << 1); ++i) {
while (!stk.isEmpty() && nums[stk.peek()] < nums[i % n]) {
ans[stk.pop()] = nums[i % n];
}
stk.push(i % n);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> ans(n, -1);
stack<int> stk;
for (int i = 0; i < (n << 1); ++i) {
while (!stk.empty() && nums[stk.top()] < nums[i % n]) {
ans[stk.top()] = nums[i % n];
stk.pop();
}
stk.push(i % n);
}
return ans;
}
};
```

#### Go

```go
func nextGreaterElements(nums []int) []int {
n := len(nums)
ans := make([]int, n)
for i := range ans {
ans[i] = -1
}
var stk []int
for i := 0; i < (n << 1); i++ {
for len(stk) > 0 && nums[stk[len(stk)-1]] < nums[i%n] {
ans[stk[len(stk)-1]] = nums[i%n]
stk = stk[:len(stk)-1]
}
stk = append(stk, i%n)
}
return ans
}
```

#### TypeScript
题目需要我们找到每个元素的下一个更大元素,那么我们可以从后往前遍历数组,这样可以将问题为求上一个更大元素。另外,由于数组是循环的,我们可以将数组遍历两次。

```ts
function nextGreaterElements(nums: number[]): number[] {
const stack: number[] = [],
len = nums.length;
const res: number[] = new Array(len).fill(-1);
for (let i = 0; i < 2 * len - 1; i++) {
const j = i % len;
while (stack.length !== 0 && nums[stack[stack.length - 1]] < nums[j]) {
res[stack[stack.length - 1]] = nums[j];
stack.pop();
}
stack.push(j);
}
return res;
}
```
具体地,我们从下标 $n \times 2 - 1$ 开始向前遍历数组,其中 $n$ 是数组的长度。然后,我们记 $j = i \bmod n$,其中 $\bmod$ 表示取模运算。如果栈不为空且栈顶元素小于等于 $nums[j]$,那么我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素大于 $nums[j]$。此时,栈顶元素就是 $nums[j]$ 的上一个更大元素,我们将其赋给 $ans[j]$。最后,我们将 $nums[j]$ 入栈。继续遍历下一个元素。

#### JavaScript
遍历结束后,我们就可以得到数组 $ans$,它是数组 $nums$ 中每个元素的下一个更大元素。

```js
/**
* @param {number[]} nums
* @return {number[]}
*/
var nextGreaterElements = function (nums) {
const n = nums.length;
let stk = [];
let ans = new Array(n).fill(-1);
for (let i = 0; i < n << 1; i++) {
const j = i % n;
while (stk.length && nums[stk[stk.length - 1]] < nums[j]) {
ans[stk.pop()] = nums[j];
}
stk.push(j);
}
return ans;
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $nums$ 的长度。

<!-- tabs:start -->

Expand Down Expand Up @@ -241,8 +121,12 @@ public:
stack<int> stk;
for (int i = n * 2 - 1; ~i; --i) {
int j = i % n;
while (!stk.empty() && stk.top() <= nums[j]) stk.pop();
if (!stk.empty()) ans[j] = stk.top();
while (stk.size() && stk.top() <= nums[j]) {
stk.pop();
}
if (stk.size()) {
ans[j] = stk.top();
}
stk.push(nums[j]);
}
return ans;
Expand All @@ -259,7 +143,7 @@ func nextGreaterElements(nums []int) []int {
for i := range ans {
ans[i] = -1
}
var stk []int
stk := []int{}
for i := n*2 - 1; i >= 0; i-- {
j := i % n
for len(stk) > 0 && stk[len(stk)-1] <= nums[j] {
Expand All @@ -274,6 +158,27 @@ func nextGreaterElements(nums []int) []int {
}
```

#### TypeScript

```ts
function nextGreaterElements(nums: number[]): number[] {
const n = nums.length;
const stk: number[] = [];
const ans: number[] = Array(n).fill(-1);
for (let i = n * 2 - 1; ~i; --i) {
const j = i % n;
while (stk.length && stk.at(-1)! <= nums[j]) {
stk.pop();
}
if (stk.length) {
ans[j] = stk.at(-1)!;
}
stk.push(nums[j]);
}
return ans;
}
```

#### JavaScript

```js
Expand All @@ -283,15 +188,15 @@ func nextGreaterElements(nums []int) []int {
*/
var nextGreaterElements = function (nums) {
const n = nums.length;
let stk = [];
let ans = new Array(n).fill(-1);
const stk = [];
const ans = Array(n).fill(-1);
for (let i = n * 2 - 1; ~i; --i) {
const j = i % n;
while (stk.length && stk[stk.length - 1] <= nums[j]) {
while (stk.length && stk.at(-1) <= nums[j]) {
stk.pop();
}
if (stk.length) {
ans[j] = stk[stk.length - 1];
ans[j] = stk.at(-1);
}
stk.push(nums[j]);
}
Expand Down
Loading