Skip to content

Commit e5f43d9

Browse files
committed
feat: add solutions to lc problem: No.1526
1 parent 2b32b0e commit e5f43d9

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ tags:
5656

5757
<pre><strong>输入:</strong>target = [3,1,5,4,2]
5858
<strong>输出:</strong>7
59-
<strong>解释:</strong>(initial)[0,0,0,0,0] -&gt; [1,1,1,1,1] -&gt; [2,1,1,1,1] -&gt; [3,1,1,1,1]
59+
<strong>解释:</strong>(initial)[0,0,0,0,0] -&gt; [1,1,1,1,1] -&gt; [2,1,1,1,1] -&gt; [3,1,1,1,1]
6060
-&gt; [3,1,2,2,2] -&gt; [3,1,3,3,2] -&gt; [3,1,4,4,2] -&gt; [3,1,5,4,2] (target)。
6161
</pre>
6262

@@ -168,6 +168,38 @@ function minNumberOperations(target: number[]): number {
168168
}
169169
```
170170

171+
#### Rust
172+
173+
```rust
174+
impl Solution {
175+
pub fn min_number_operations(target: Vec<i32>) -> i32 {
176+
let mut f = target[0];
177+
for i in 1..target.len() {
178+
if target[i] > target[i - 1] {
179+
f += target[i] - target[i - 1];
180+
}
181+
}
182+
f
183+
}
184+
}
185+
```
186+
187+
#### C#
188+
189+
```cs
190+
public class Solution {
191+
public int MinNumberOperations(int[] target) {
192+
int f = target[0];
193+
for (int i = 1; i < target.Length; ++i) {
194+
if (target[i] > target[i - 1]) {
195+
f += target[i] - target[i - 1];
196+
}
197+
}
198+
return f;
199+
}
200+
}
201+
```
202+
171203
<!-- tabs:end -->
172204

173205
<!-- solution:end -->

solution/1500-1599/1526.Minimum Number of Increments on Subarrays to Form a Target Array/README_EN.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ function minNumberOperations(target: number[]): number {
160160
}
161161
```
162162

163+
#### Rust
164+
165+
```rust
166+
impl Solution {
167+
pub fn min_number_operations(target: Vec<i32>) -> i32 {
168+
let mut f = target[0];
169+
for i in 1..target.len() {
170+
if target[i] > target[i - 1] {
171+
f += target[i] - target[i - 1];
172+
}
173+
}
174+
f
175+
}
176+
}
177+
```
178+
179+
#### C#
180+
181+
```cs
182+
public class Solution {
183+
public int MinNumberOperations(int[] target) {
184+
int f = target[0];
185+
for (int i = 1; i < target.Length; ++i) {
186+
if (target[i] > target[i - 1]) {
187+
f += target[i] - target[i - 1];
188+
}
189+
}
190+
return f;
191+
}
192+
}
193+
```
194+
163195
<!-- tabs:end -->
164196

165197
<!-- solution:end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Solution {
2+
public int MinNumberOperations(int[] target) {
3+
int f = target[0];
4+
for (int i = 1; i < target.Length; ++i) {
5+
if (target[i] > target[i - 1]) {
6+
f += target[i] - target[i - 1];
7+
}
8+
}
9+
return f;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn min_number_operations(target: Vec<i32>) -> i32 {
3+
let mut f = target[0];
4+
for i in 1..target.len() {
5+
if target[i] > target[i - 1] {
6+
f += target[i] - target[i - 1];
7+
}
8+
}
9+
f
10+
}
11+
}

0 commit comments

Comments
 (0)