Skip to content

feat: add solutions to lc problems: No.2951,2952 #2057

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
Dec 3, 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
54 changes: 51 additions & 3 deletions solution/2900-2999/2951.Find the Peaks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,75 @@ mountain[2] 也不可能是峰值,因为它不严格大于 mountain[3] 和 mou
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def findPeaks(self, mountain: List[int]) -> List[int]:
return [
i
for i in range(1, len(mountain) - 1)
if mountain[i - 1] < mountain[i] > mountain[i + 1]
]
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public List<Integer> findPeaks(int[] mountain) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.add(i);
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findPeaks(vector<int>& mountain) {
vector<int> ans;
for (int i = 1; i < mountain.size() - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push_back(i);
}
}
return ans;
}
};
```

### **Go**

```go
func findPeaks(mountain []int) (ans []int) {
for i := 1; i < len(mountain)-1; i++ {
if mountain[i-1] < mountain[i] && mountain[i+1] < mountain[i] {
ans = append(ans, i)
}
}
return
}
```

### **TypeScript**

```ts
function findPeaks(mountain: number[]): number[] {
const ans: number[] = [];
for (let i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push(i);
}
}
return ans;
}
```

### **...**
Expand Down
54 changes: 51 additions & 3 deletions solution/2900-2999/2951.Find the Peaks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,73 @@ So the answer is [1,3].
### **Python3**

```python

class Solution:
def findPeaks(self, mountain: List[int]) -> List[int]:
return [
i
for i in range(1, len(mountain) - 1)
if mountain[i - 1] < mountain[i] > mountain[i + 1]
]
```

### **Java**

```java

class Solution {
public List<Integer> findPeaks(int[] mountain) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.add(i);
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
vector<int> findPeaks(vector<int>& mountain) {
vector<int> ans;
for (int i = 1; i < mountain.size() - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push_back(i);
}
}
return ans;
}
};
```

### **Go**

```go
func findPeaks(mountain []int) (ans []int) {
for i := 1; i < len(mountain)-1; i++ {
if mountain[i-1] < mountain[i] && mountain[i+1] < mountain[i] {
ans = append(ans, i)
}
}
return
}
```

### **TypeScript**

```ts
function findPeaks(mountain: number[]): number[] {
const ans: number[] = [];
for (let i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push(i);
}
}
return ans;
}
```

### **...**
Expand Down
12 changes: 12 additions & 0 deletions solution/2900-2999/2951.Find the Peaks/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<int> findPeaks(vector<int>& mountain) {
vector<int> ans;
for (int i = 1; i < mountain.size() - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push_back(i);
}
}
return ans;
}
};
8 changes: 8 additions & 0 deletions solution/2900-2999/2951.Find the Peaks/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
func findPeaks(mountain []int) (ans []int) {
for i := 1; i < len(mountain)-1; i++ {
if mountain[i-1] < mountain[i] && mountain[i+1] < mountain[i] {
ans = append(ans, i)
}
}
return
}
11 changes: 11 additions & 0 deletions solution/2900-2999/2951.Find the Peaks/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public List<Integer> findPeaks(int[] mountain) {
List<Integer> ans = new ArrayList<>();
for (int i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.add(i);
}
}
return ans;
}
}
7 changes: 7 additions & 0 deletions solution/2900-2999/2951.Find the Peaks/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Solution:
def findPeaks(self, mountain: List[int]) -> List[int]:
return [
i
for i in range(1, len(mountain) - 1)
if mountain[i - 1] < mountain[i] > mountain[i + 1]
]
9 changes: 9 additions & 0 deletions solution/2900-2999/2951.Find the Peaks/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function findPeaks(mountain: number[]): number[] {
const ans: number[] = [];
for (let i = 1; i < mountain.length - 1; ++i) {
if (mountain[i - 1] < mountain[i] && mountain[i + 1] < mountain[i]) {
ans.push(i);
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,98 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def minimumAddedCoins(self, coins: List[int], target: int) -> int:
coins.sort()
s = 1
ans = i = 0
while s <= target:
if i < len(coins) and coins[i] <= s:
s += coins[i]
i += 1
else:
s <<= 1
ans += 1
return ans
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public int minimumAddedCoins(int[] coins, int target) {
Arrays.sort(coins);
int ans = 0;
for (int i = 0, s = 1; s <= target;) {
if (i < coins.length && coins[i] <= s) {
s += coins[i++];
} else {
s <<= 1;
++ans;
}
}
return ans;
}
}
```

### **C++**

```cpp

class Solution {
public:
int minimumAddedCoins(vector<int>& coins, int target) {
sort(coins.begin(), coins.end());
int ans = 0;
for (int i = 0, s = 1; s <= target;) {
if (i < coins.size() && coins[i] <= s) {
s += coins[i++];
} else {
s <<= 1;
++ans;
}
}
return ans;
}
};
```

### **Go**

```go
func minimumAddedCoins(coins []int, target int) (ans int) {
slices.Sort(coins)
for i, s := 0, 1; s <= target; {
if i < len(coins) && coins[i] <= s {
s += coins[i]
i++
} else {
s <<= 1
ans++
}
}
return
}
```

### **TypeScript**

```ts
function minimumAddedCoins(coins: number[], target: number): number {
coins.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, s = 1; s <= target; ) {
if (i < coins.length && coins[i] <= s) {
s += coins[i++];
} else {
s <<= 1;
++ans;
}
}
return ans;
}
```

### **...**
Expand Down
Loading