Skip to content

Commit feaa022

Browse files
committed
feat: add solutions to lc problem: No.0120
No.0120.Triangle
1 parent 10f0718 commit feaa022

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

solution/0100-0199/0120.Triangle/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,39 @@ func min(a, b int) int {
144144
}
145145
```
146146

147+
### **TypeScript**
148+
149+
```ts
150+
function minimumTotal(triangle: number[][]): number {
151+
const n = triangle.length;
152+
for (let i = n - 2; i >= 0; i--) {
153+
for (let j = 0; j < i + 1; j++) {
154+
triangle[i][j] += Math.min(
155+
triangle[i + 1][j],
156+
triangle[i + 1][j + 1],
157+
);
158+
}
159+
}
160+
return triangle[0][0];
161+
}
162+
```
163+
164+
### **Rust**
165+
166+
```rust
167+
impl Solution {
168+
pub fn minimum_total(mut triangle: Vec<Vec<i32>>) -> i32 {
169+
let n = triangle.len();
170+
for i in (0..n - 1).rev() {
171+
for j in 0..i + 1 {
172+
triangle[i][j] += triangle[i + 1][j].min(triangle[i + 1][j + 1]);
173+
}
174+
}
175+
triangle[0][0]
176+
}
177+
}
178+
```
179+
147180
### **...**
148181

149182
```

solution/0100-0199/0120.Triangle/README_EN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,39 @@ func min(a, b int) int {
127127
}
128128
```
129129

130+
### **TypeScript**
131+
132+
```ts
133+
function minimumTotal(triangle: number[][]): number {
134+
const n = triangle.length;
135+
for (let i = n - 2; i >= 0; i--) {
136+
for (let j = 0; j < i + 1; j++) {
137+
triangle[i][j] += Math.min(
138+
triangle[i + 1][j],
139+
triangle[i + 1][j + 1],
140+
);
141+
}
142+
}
143+
return triangle[0][0];
144+
}
145+
```
146+
147+
### **Rust**
148+
149+
```rust
150+
impl Solution {
151+
pub fn minimum_total(mut triangle: Vec<Vec<i32>>) -> i32 {
152+
let n = triangle.len();
153+
for i in (0..n - 1).rev() {
154+
for j in 0..i + 1 {
155+
triangle[i][j] += triangle[i + 1][j].min(triangle[i + 1][j + 1]);
156+
}
157+
}
158+
triangle[0][0]
159+
}
160+
}
161+
```
162+
130163
### **...**
131164

132165
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn minimum_total(mut triangle: Vec<Vec<i32>>) -> i32 {
3+
let n = triangle.len();
4+
for i in (0..n - 1).rev() {
5+
for j in 0..i + 1 {
6+
triangle[i][j] += triangle[i + 1][j].min(triangle[i + 1][j + 1]);
7+
}
8+
}
9+
triangle[0][0]
10+
}
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minimumTotal(triangle: number[][]): number {
2+
const n = triangle.length;
3+
for (let i = n - 2; i >= 0; i--) {
4+
for (let j = 0; j < i + 1; j++) {
5+
triangle[i][j] += Math.min(
6+
triangle[i + 1][j],
7+
triangle[i + 1][j + 1],
8+
);
9+
}
10+
}
11+
return triangle[0][0];
12+
}

0 commit comments

Comments
 (0)