2087. Minimum Cost Homecoming of a Robot in a Grid Medium
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 */
42pub 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
49impl Solution {
50 pub fn min_cost(start_pos: Vec<i32>, home_pos: Vec<i32>, row_costs: Vec<i32>, col_costs: Vec<i32>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_2087() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.