Skip to content

[pull] main from doocs:main #490

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 3 commits into from
Mar 22, 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,77 @@

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

**方法一:动态规划**

我们定义 $f[i]$ 表示得到 $target[0,..i]$ 的最少操作次数,初始时 $f[0] = target[0]$。

对于 $target[i]$,如果 $target[i] \leq target[i-1]$,则 $f[i] = f[i-1]$;否则 $f[i] = f[i-1] + target[i] - target[i-1]$。

最终答案即为 $f[n-1]$。

我们注意到 $f[i]$ 只与 $f[i-1]$ 有关,因此可以只用一个变量来维护操作次数。

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

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def minNumberOperations(self, target: List[int]) -> int:
return target[0] + sum(max(0, b - a) for a, b in pairwise(target))
```

### **Java**

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

```java
class Solution {
public int minNumberOperations(int[] target) {
int f = target[0];
for (int i = 1; i < target.length; ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minNumberOperations(vector<int>& target) {
int f = target[0];
for (int i = 1; i < target.size(); ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
};
```

### **Go**

```go
func minNumberOperations(target []int) int {
f := target[0]
for i, x := range target[1:] {
if x > target[i] {
f += x - target[i]
}
}
return f
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,56 @@
### **Python3**

```python

class Solution:
def minNumberOperations(self, target: List[int]) -> int:
return target[0] + sum(max(0, b - a) for a, b in pairwise(target))
```

### **Java**

```java
class Solution {
public int minNumberOperations(int[] target) {
int f = target[0];
for (int i = 1; i < target.length; ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
}
```

### **C++**

```cpp
class Solution {
public:
int minNumberOperations(vector<int>& target) {
int f = target[0];
for (int i = 1; i < target.size(); ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
};
```

### **Go**

```go
func minNumberOperations(target []int) int {
f := target[0]
for i, x := range target[1:] {
if x > target[i] {
f += x - target[i]
}
}
return f
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
int minNumberOperations(vector<int>& target) {
int f = target[0];
for (int i = 1; i < target.size(); ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func minNumberOperations(target []int) int {
f := target[0]
for i, x := range target[1:] {
if x > target[i] {
f += x - target[i]
}
}
return f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int minNumberOperations(int[] target) {
int f = target[0];
for (int i = 1; i < target.length; ++i) {
if (target[i] > target[i - 1]) {
f += target[i] - target[i - 1];
}
}
return f;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def minNumberOperations(self, target: List[int]) -> int:
return target[0] + sum(max(0, b - a) for a, b in pairwise(target))
Loading