File tree Expand file tree Collapse file tree 6 files changed +131
-0
lines changed
2303.Calculate Amount Paid in Taxes
2304.Minimum Path Cost in a Grid Expand file tree Collapse file tree 6 files changed +131
-0
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,25 @@ func min(a, b int) int {
170
170
}
171
171
```
172
172
173
+ ### ** Rust**
174
+
175
+ ``` rust
176
+ impl Solution {
177
+ pub fn calculate_tax (brackets : Vec <Vec <i32 >>, income : i32 ) -> f64 {
178
+ let mut res = 0f64 ;
179
+ let mut pre = 0i32 ;
180
+ for bracket in brackets . iter () {
181
+ res += f64 :: from (income . min (bracket [0 ]) - pre ) * f64 :: from (bracket [1 ]) * 0.01 ;
182
+ if income <= bracket [0 ] {
183
+ break ;
184
+ }
185
+ pre = bracket [0 ];
186
+ }
187
+ res
188
+ }
189
+ }
190
+ ```
191
+
173
192
### ** TypeScript**
174
193
175
194
``` ts
Original file line number Diff line number Diff line change @@ -161,6 +161,25 @@ func min(a, b int) int {
161
161
}
162
162
```
163
163
164
+ ### ** Rust**
165
+
166
+ ``` rust
167
+ impl Solution {
168
+ pub fn calculate_tax (brackets : Vec <Vec <i32 >>, income : i32 ) -> f64 {
169
+ let mut res = 0f64 ;
170
+ let mut pre = 0i32 ;
171
+ for bracket in brackets . iter () {
172
+ res += f64 :: from (income . min (bracket [0 ]) - pre ) * f64 :: from (bracket [1 ]) * 0.01 ;
173
+ if income <= bracket [0 ] {
174
+ break ;
175
+ }
176
+ pre = bracket [0 ];
177
+ }
178
+ res
179
+ }
180
+ }
181
+ ```
182
+
164
183
### ** TypeScript**
165
184
166
185
``` ts
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn calculate_tax ( brackets : Vec < Vec < i32 > > , income : i32 ) -> f64 {
3
+ let mut res = 0f64 ;
4
+ let mut pre = 0i32 ;
5
+ for bracket in brackets. iter ( ) {
6
+ res += f64:: from ( income. min ( bracket[ 0 ] ) - pre) * f64:: from ( bracket[ 1 ] ) * 0.01 ;
7
+ if income <= bracket[ 0 ] {
8
+ break ;
9
+ }
10
+ pre = bracket[ 0 ] ;
11
+ }
12
+ res
13
+ }
14
+ }
Original file line number Diff line number Diff line change @@ -190,6 +190,34 @@ func min(a, b int) int {
190
190
}
191
191
```
192
192
193
+ ### ** Rust**
194
+
195
+ ``` rust
196
+ impl Solution {
197
+ pub fn min_path_cost (grid : Vec <Vec <i32 >>, move_cost : Vec <Vec <i32 >>) -> i32 {
198
+ let (m , n ) = (grid . len (), grid [0 ]. len ());
199
+ let mut dp = vec! [0 ; n ];
200
+ for i in 0 .. m - 1 {
201
+ let mut counter = vec! [i32 :: MAX ; n ];
202
+ for j in 0 .. n {
203
+ let val = grid [i ][j ];
204
+ for k in 0 .. n {
205
+ counter [k ] = counter [k ]. min (val + move_cost [val as usize ][k ] + dp [j ]);
206
+ }
207
+ }
208
+ for j in 0 .. n {
209
+ dp [j ] = counter [j ];
210
+ }
211
+ }
212
+ let mut res = i32 :: MAX ;
213
+ for i in 0 .. n {
214
+ res = res . min (dp [i ] + grid [m - 1 ][i ]);
215
+ }
216
+ res
217
+ }
218
+ }
219
+ ```
220
+
193
221
### ** TypeScript**
194
222
195
223
``` ts
Original file line number Diff line number Diff line change @@ -176,6 +176,34 @@ func min(a, b int) int {
176
176
}
177
177
```
178
178
179
+ ### ** Rust**
180
+
181
+ ``` rust
182
+ impl Solution {
183
+ pub fn min_path_cost (grid : Vec <Vec <i32 >>, move_cost : Vec <Vec <i32 >>) -> i32 {
184
+ let (m , n ) = (grid . len (), grid [0 ]. len ());
185
+ let mut dp = vec! [0 ; n ];
186
+ for i in 0 .. m - 1 {
187
+ let mut counter = vec! [i32 :: MAX ; n ];
188
+ for j in 0 .. n {
189
+ let val = grid [i ][j ];
190
+ for k in 0 .. n {
191
+ counter [k ] = counter [k ]. min (val + move_cost [val as usize ][k ] + dp [j ]);
192
+ }
193
+ }
194
+ for j in 0 .. n {
195
+ dp [j ] = counter [j ];
196
+ }
197
+ }
198
+ let mut res = i32 :: MAX ;
199
+ for i in 0 .. n {
200
+ res = res . min (dp [i ] + grid [m - 1 ][i ]);
201
+ }
202
+ res
203
+ }
204
+ }
205
+ ```
206
+
179
207
### ** TypeScript**
180
208
181
209
``` ts
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn min_path_cost ( grid : Vec < Vec < i32 > > , move_cost : Vec < Vec < i32 > > ) -> i32 {
3
+ let ( m, n) = ( grid. len ( ) , grid[ 0 ] . len ( ) ) ;
4
+ let mut dp = vec ! [ 0 ; n] ;
5
+ for i in 0 ..m - 1 {
6
+ let mut counter = vec ! [ i32 :: MAX ; n] ;
7
+ for j in 0 ..n {
8
+ let val = grid[ i] [ j] ;
9
+ for k in 0 ..n {
10
+ counter[ k] = counter[ k] . min ( val + move_cost[ val as usize ] [ k] + dp[ j] ) ;
11
+ }
12
+ }
13
+ for j in 0 ..n {
14
+ dp[ j] = counter[ j] ;
15
+ }
16
+ }
17
+ let mut res = i32:: MAX ;
18
+ for i in 0 ..n {
19
+ res = res. min ( dp[ i] + grid[ m - 1 ] [ i] ) ;
20
+ }
21
+ res
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments