3122. Minimum Number of Operations to Satisfy Conditions Medium
1/**
2 * [3122] Minimum Number of Operations to Satisfy Conditions
3 *
4 * You are given a 2D matrix grid of size m x n. In one operation, you can change the value of any cell to any non-negative number. You need to perform some operations such that each cell grid[i][j] is:
5 *
6 * Equal to the cell below it, i.e. grid[i][j] == grid[i + 1][j] (if it exists).
7 * Different from the cell to its right, i.e. grid[i][j] != grid[i][j + 1] (if it exists).
8 *
9 * Return the minimum number of operations needed.
10 *
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">grid = [[1,0,2],[1,0,2]]</span>
14 * Output: 0
15 * Explanation:
16 * <img alt="" src="https://assets.leetcode.com/uploads/2024/04/15/examplechanged.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" />
17 * All the cells in the matrix already satisfy the properties.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">grid = [[1,1,1],[0,0,0]]</span>
22 * Output: 3
23 * Explanation:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2024/03/27/example21.png" style="width: 254px; height: 186px;padding: 10px; background: #fff; border-radius: .5rem;" />
25 * The matrix becomes [[1,0,1],[1,0,1]] which satisfies the properties, by doing these 3 operations:
26 *
27 * Change grid[1][0] to 1.
28 * Change grid[0][1] to 0.
29 * Change grid[1][2] to 1.
30 * </div>
31 * <strong class="example">Example 3:
32 * <div class="example-block">
33 * Input: <span class="example-io">grid = [[1],[2],[3]]</span>
34 * Output: 2
35 * Explanation:
36 * <img alt="" src="https://assets.leetcode.com/uploads/2024/03/31/changed.png" style="width: 86px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" />
37 * There is a single column. We can change the value to 1 in each cell using 2 operations.
38 * </div>
39 *
40 * Constraints:
41 *
42 * 1 <= n, m <= 1000
43 * 0 <= grid[i][j] <= 9
44 *
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/minimum-number-of-operations-to-satisfy-conditions/
49// discuss: https://leetcode.com/problems/minimum-number-of-operations-to-satisfy-conditions/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54 pub fn minimum_operations(grid: Vec<Vec<i32>>) -> i32 {
55 0
56 }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_3122() {
67 }
68}
69
Back
© 2025 bowen.ge All Rights Reserved.