|
| 1 | +/** |
| 2 | + * [2087] Minimum Cost Homecoming of a Robot in a Grid |
| 3 | + * |
| 4 | + * There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol). |
| 5 | + * The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n. |
| 6 | + * |
| 7 | + * If the robot moves up or down into a cell whose row is r, then this move costs rowCosts[r]. |
| 8 | + * If the robot moves left or right into a cell whose column is c, then this move costs colCosts[c]. |
| 9 | + * |
| 10 | + * Return the minimum total cost for this robot to return home. |
| 11 | + * |
| 12 | + * Example 1: |
| 13 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/10/11/eg-1.png" style="width: 282px; height: 217px;" /> |
| 14 | + * Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7] |
| 15 | + * Output: 18 |
| 16 | + * Explanation: One optimal path is that: |
| 17 | + * Starting from (1, 0) |
| 18 | + * -> It goes down to (<u>2</u>, 0). This move costs rowCosts[2] = 3. |
| 19 | + * -> It goes right to (2, <u>1</u>). This move costs colCosts[1] = 2. |
| 20 | + * -> It goes right to (2, <u>2</u>). This move costs colCosts[2] = 6. |
| 21 | + * -> It goes right to (2, <u>3</u>). This move costs colCosts[3] = 7. |
| 22 | + * The total cost is 3 + 2 + 6 + 7 = 18 |
| 23 | + * Example 2: |
| 24 | + * |
| 25 | + * Input: startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26] |
| 26 | + * Output: 0 |
| 27 | + * Explanation: The robot is already at its home. Since no moves occur, the total cost is 0. |
| 28 | + * |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * |
| 32 | + * m == rowCosts.length |
| 33 | + * n == colCosts.length |
| 34 | + * 1 <= m, n <= 10^5 |
| 35 | + * 0 <= rowCosts[r], colCosts[c] <= 10^4 |
| 36 | + * startPos.length == 2 |
| 37 | + * homePos.length == 2 |
| 38 | + * 0 <= startrow, homerow < m |
| 39 | + * 0 <= startcol, homecol < n |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/ |
| 45 | +// discuss: https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + pub fn min_cost( |
| 51 | + start_pos: Vec<i32>, |
| 52 | + home_pos: Vec<i32>, |
| 53 | + row_costs: Vec<i32>, |
| 54 | + col_costs: Vec<i32>, |
| 55 | + ) -> i32 { |
| 56 | + 0 |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// submission codes end |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use super::*; |
| 65 | + |
| 66 | + #[test] |
| 67 | + #[ignore] |
| 68 | + fn test_2087_example_1() { |
| 69 | + let start_pos = vec![0, 0]; |
| 70 | + let home_pos = vec![2, 3]; |
| 71 | + let row_costs = vec![5, 4, 3]; |
| 72 | + let col_costs = vec![8, 2, 6, 7]; |
| 73 | + |
| 74 | + let result = 2; |
| 75 | + |
| 76 | + assert_eq!( |
| 77 | + Solution::min_cost(start_pos, home_pos, row_costs, col_costs), |
| 78 | + result |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + #[ignore] |
| 84 | + fn test_2087_example_2() { |
| 85 | + let start_pos = vec![1, 0]; |
| 86 | + let home_pos = vec![0, 0]; |
| 87 | + let row_costs = vec![5]; |
| 88 | + let col_costs = vec![26]; |
| 89 | + |
| 90 | + let result = 18; |
| 91 | + |
| 92 | + assert_eq!( |
| 93 | + Solution::min_cost(start_pos, home_pos, row_costs, col_costs), |
| 94 | + result |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments