File tree Expand file tree Collapse file tree 4 files changed +89
-0
lines changed
solution/0100-0199/0120.Triangle Expand file tree Collapse file tree 4 files changed +89
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,39 @@ func min(a, b int) int {
144
144
}
145
145
```
146
146
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
+
147
180
### ** ...**
148
181
149
182
```
Original file line number Diff line number Diff line change @@ -127,6 +127,39 @@ func min(a, b int) int {
127
127
}
128
128
```
129
129
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
+
130
163
### ** ...**
131
164
132
165
```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments