Skip to content
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

feat: add solutions to lc problems: No.0162,0163 #1771

Merged
merged 1 commit into from
Oct 9, 2023
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
65 changes: 36 additions & 29 deletions solution/0100-0199/0162.Find Peak Element/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@

<!-- 这里可写通用的实现逻辑 -->

二分查找。
**方法一:二分查找**

我们定义二分查找的左边界 $left=0$,右边界 $right=n-1$,其中 $n$ 是数组的长度。在每一步二分查找中,我们找到当前区间的中间元素 $mid$,然后比较 $mid$ 与其右边元素 $mid+1$ 的值:

- 如果 $mid$ 的值大于 $mid+1$ 的值,则左侧存在峰值元素,我们将右边界 $right$ 更新为 $mid$;
- 否则,右侧存在峰值元素,我们将左边界 $left$ 更新为 $mid+1$。
- 最后,当左边界 $left$ 与右边界 $right$ 相等时,我们就找到了数组的峰值元素。

时间复杂度 $O(\log n)$,其中 $n$ 是数组 $nums$ 的长度。每一步二分查找可以将搜索区间减少一半,因此时间复杂度为 $O(\log n)$。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -88,22 +96,24 @@ class Solution {
}
```

### **TypeScript**
### **C++**

```ts
function findPeakElement(nums: number[]): number {
let left = 0,
right = nums.length - 1;
while (left < right) {
let mid: number = (left + right) >> 1;
if (nums[mid] <= nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
```cpp
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + right >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
return left;
}
};
```

### **Go**
Expand All @@ -123,24 +133,21 @@ func findPeakElement(nums []int) int {
}
```

### **C++**
### **TypeScript**

```cpp
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + right >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
```ts
function findPeakElement(nums: number[]): number {
let [left, right] = [0, nums.length - 1];
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
return left;
}
};
return left;
}
```

### **...**
Expand Down
65 changes: 36 additions & 29 deletions solution/0100-0199/0162.Find Peak Element/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@

## Solutions

Binary search.
**Solution 1: Binary Search**

We define the left boundary of binary search as $left=0$ and the right boundary as $right=n-1$, where $n$ is the length of the array. In each step of binary search, we find the middle element $mid$ of the current interval, and compare the values of $mid$ and its right neighbor $mid+1$:

- If the value of $mid$ is greater than the value of $mid+1$, there exists a peak element on the left side, and we update the right boundary $right$ to $mid$.
- Otherwise, there exists a peak element on the right side, and we update the left boundary $left$ to $mid+1$.
- Finally, when the left boundary $left$ is equal to the right boundary $right$, we have found the peak element of the array.

The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. Each step of binary search can reduce the search interval by half, so the time complexity is $O(\log n)$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -76,22 +84,24 @@ class Solution {
}
```

### **TypeScript**
### **C++**

```ts
function findPeakElement(nums: number[]): number {
let left = 0,
right = nums.length - 1;
while (left < right) {
let mid: number = (left + right) >> 1;
if (nums[mid] <= nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
```cpp
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + right >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
return left;
}
};
```

### **Go**
Expand All @@ -111,24 +121,21 @@ func findPeakElement(nums []int) int {
}
```

### **C++**
### **TypeScript**

```cpp
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while (left < right) {
int mid = left + right >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
```ts
function findPeakElement(nums: number[]): number {
let [left, right] = [0, nums.length - 1];
while (left < right) {
const mid = (left + right) >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
return left;
}
};
return left;
}
```

### **...**
Expand Down
11 changes: 5 additions & 6 deletions solution/0100-0199/0162.Find Peak Element/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
function findPeakElement(nums: number[]): number {
let left = 0,
right = nums.length - 1;
let [left, right] = [0, nums.length - 1];
while (left < right) {
let mid: number = (left + right) >> 1;
if (nums[mid] <= nums[mid + 1]) {
left = mid + 1;
} else {
const mid = (left + right) >> 1;
if (nums[mid] > nums[mid + 1]) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
Expand Down
106 changes: 55 additions & 51 deletions solution/0100-0199/0163.Missing Ranges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@

**方法一:模拟**

按照题意模拟即可
我们直接按照题意模拟即可

时间复杂度 $O(n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$

<!-- tabs:start -->

Expand All @@ -60,21 +60,20 @@

```python
class Solution:
def findMissingRanges(self, nums: List[int], lower: int, upper: int) -> List[str]:
def f(a, b):
return str(a) if a == b else f'{a}->{b}'

def findMissingRanges(
self, nums: List[int], lower: int, upper: int
) -> List[List[int]]:
n = len(nums)
if n == 0:
return [f(lower, upper)]
return [[lower, upper]]
ans = []
if nums[0] > lower:
ans.append(f(lower, nums[0] - 1))
ans.append([lower, nums[0] - 1])
for a, b in pairwise(nums):
if b - a > 1:
ans.append(f(a + 1, b - 1))
ans.append([a + 1, b - 1])
if nums[-1] < upper:
ans.append(f(nums[-1] + 1, upper))
ans.append([nums[-1] + 1, upper])
return ans
```

Expand All @@ -84,31 +83,25 @@ class Solution:

```java
class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
public List<List<Integer>> findMissingRanges(int[] nums, int lower, int upper) {
int n = nums.length;
List<String> ans = new ArrayList<>();
if (n == 0) {
ans.add(f(lower, upper));
return ans;
return List.of(List.of(lower, upper));
}
List<List<Integer>> ans = new ArrayList<>();
if (nums[0] > lower) {
ans.add(f(lower, nums[0] - 1));
ans.add(List.of(lower, nums[0] - 1));
}
for (int i = 1; i < n; ++i) {
int a = nums[i - 1], b = nums[i];
if (b - a > 1) {
ans.add(f(a + 1, b - 1));
if (nums[i] - nums[i - 1] > 1) {
ans.add(List.of(nums[i - 1] + 1, nums[i] - 1));
}
}
if (nums[n - 1] < upper) {
ans.add(f(nums[n - 1] + 1, upper));
ans.add(List.of(nums[n - 1] + 1, upper));
}
return ans;
}

private String f(int a, int b) {
return a == b ? a + "" : a + "->" + b;
}
}
```

Expand All @@ -117,27 +110,22 @@ class Solution {
```cpp
class Solution {
public:
vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
auto f = [](int a, int b) {
return a == b ? to_string(a) : to_string(a) + "->" + to_string(b);
};
vector<vector<int>> findMissingRanges(vector<int>& nums, int lower, int upper) {
int n = nums.size();
vector<string> ans;
if (n == 0) {
ans.emplace_back(f(lower, upper));
return ans;
return {{lower, upper}};
}
vector<vector<int>> ans;
if (nums[0] > lower) {
ans.emplace_back(f(lower, nums[0] - 1));
ans.push_back({lower, nums[0] - 1});
}
for (int i = 1; i < n; ++i) {
int a = nums[i - 1], b = nums[i];
if (b - a > 1) {
ans.emplace_back(f(a + 1, b - 1));
for (int i = 1; i < nums.size(); ++i) {
if (nums[i] - nums[i - 1] > 1) {
ans.push_back({nums[i - 1] + 1, nums[i] - 1});
}
}
if (nums[n - 1] < upper) {
ans.emplace_back(f(nums[n - 1] + 1, upper));
ans.push_back({nums[n - 1] + 1, upper});
}
return ans;
}
Expand All @@ -147,34 +135,50 @@ public:
### **Go**

```go
func findMissingRanges(nums []int, lower int, upper int) (ans []string) {
f := func(a, b int) string {
if a == b {
return strconv.Itoa(a)
}
return strconv.Itoa(a) + "->" + strconv.Itoa(b)
}
func findMissingRanges(nums []int, lower int, upper int) (ans [][]int) {
n := len(nums)
if n == 0 {
ans = append(ans, f(lower, upper))
return
return [][]int{{lower, upper}}
}
if nums[0] > lower {
ans = append(ans, f(lower, nums[0]-1))
ans = append(ans, []int{lower, nums[0] - 1})
}
for i := 1; i < n; i++ {
a, b := nums[i-1], nums[i]
if b-a > 1 {
ans = append(ans, f(a+1, b-1))
for i, b := range nums[1:] {
if a := nums[i]; b-a > 1 {
ans = append(ans, []int{a + 1, b - 1})
}
}
if nums[n-1] < upper {
ans = append(ans, f(nums[n-1]+1, upper))
ans = append(ans, []int{nums[n-1] + 1, upper})
}
return
}
```

### **TypeScript**

```ts
function findMissingRanges(nums: number[], lower: number, upper: number): number[][] {
const n = nums.length;
if (n === 0) {
return [[lower, upper]];
}
const ans: number[][] = [];
if (nums[0] > lower) {
ans.push([lower, nums[0] - 1]);
}
for (let i = 1; i < n; ++i) {
if (nums[i] - nums[i - 1] > 1) {
ans.push([nums[i - 1] + 1, nums[i] - 1]);
}
}
if (nums[n - 1] < upper) {
ans.push([nums[n - 1] + 1, upper]);
}
return ans;
}
```

### **...**

```
Expand Down
Loading