Skip to content

Commit a9c409f

Browse files
authored
Added solution for 2162
1 parent b8bcc05 commit a9c409f

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::cmp::min;
2+
3+
impl Solution {
4+
// Create a direct solution with mins:secs and indirect solution with (mins - 1):(secs + 60) and calculate the cost for executing both. Return the minimum cost
5+
pub fn min_cost_set_time(start_at: i32, move_cost: i32, push_cost: i32, target_seconds: i32) -> i32 {
6+
let mut direct_mins = (target_seconds / 60).to_string();
7+
let mut direct_seconds = (target_seconds % 60).to_string();
8+
let mut indirect_mins = ((target_seconds / 60) - 1).to_string();
9+
let mut indirect_seconds = ((target_seconds % 60) + 60).to_string();
10+
// If direct mins are 0, indirect mins should be 0 (not -1) and don't add 60 to indirect secs
11+
if (target_seconds / 60) == 0 {
12+
indirect_mins = "0".to_owned();
13+
indirect_seconds = direct_seconds.to_string();
14+
}
15+
// If direct secs are less than 10, preprend a 0
16+
if (target_seconds % 60) < 10 {
17+
direct_seconds = "0".to_owned() + &direct_seconds.to_string();
18+
}
19+
20+
// Generate direct and indirect solutions as strings
21+
let direct_solution = direct_mins.to_string() + &direct_seconds.to_string();
22+
let indirect_solution = indirect_mins.to_string() + &indirect_seconds.to_string();
23+
// println!("Direct Solution: {}, Indirect Solution: {}", direct_solution, indirect_solution);
24+
let mut current_cost = i32::MAX;
25+
let mut min_cost = i32::MAX;
26+
let mut last_position = char::from_digit(start_at as u32, 10).unwrap();
27+
// Only execute if direct minutes is less than 100 since we can only enter max value of 99
28+
if (target_seconds / 60) < 100 {
29+
current_cost = 0;
30+
for current_position in direct_solution.chars().collect::<Vec<char>>() {
31+
// Don't start adding cost if nothing has been typed so far and the number to be typed is 0
32+
if !(current_cost == 0 && current_position == '0') {
33+
if last_position != current_position {
34+
// println!("Last Position: {}, Current Position: {}", last_position, current_position);
35+
current_cost += move_cost;
36+
last_position = current_position;
37+
}
38+
if !(current_cost == 0 && current_position == '0') {
39+
current_cost += push_cost;
40+
}
41+
}
42+
}
43+
min_cost = min(min_cost, current_cost);
44+
}
45+
// Only calculate this if indirect seconds isn't more than 100, because that can't be entered in microwave
46+
if (target_seconds % 60) < 50 {
47+
// Reset values for current cost and last position
48+
current_cost = 0;
49+
last_position = char::from_digit(start_at as u32, 10).unwrap();
50+
for current_position in indirect_solution.chars().collect::<Vec<char>>() {
51+
// Don't start adding cost if nothing has been typed so far and the number to be typed is 0
52+
if !(current_cost == 0 && current_position == '0') {
53+
if last_position != current_position {
54+
// println!("Last Position: {}, Current Position: {}", last_position, current_position);
55+
current_cost += move_cost;
56+
last_position = current_position;
57+
}
58+
current_cost += push_cost;
59+
}
60+
}
61+
// println!("Current cost: {}", current_cost);
62+
min_cost = min(min_cost, current_cost);
63+
}
64+
return min_cost;
65+
}
66+
}

0 commit comments

Comments
 (0)