[LeetCode] 598. Range Addition II

https://leetcode.com/problems/range-addition-ii

思路1: brute force - O(m * n * k), k = number of ops
思路2: 从 ops 中找到被操作次数最多的 row 和 column,这可以通过遍历 ops 数组,分别找到 row 和 column 的最小值来完成
参考: https://discuss.leetcode.com/topic/90547/java-solution-find-min

public class Solution {
    public int maxCount(int m, int n, int[][] ops) {
        if (ops == null || ops.length == 0) {
            return m * n;
        }
        int row = ops[0][0];
        int column = ops[0][1];
        for (int i = 1; i < ops.length; i++) {
            row = Math.min(row, ops[i][0]);
            column = Math.min(column, ops[i][1]);
        }
        return row * column;
    }
}
时间: 2024-10-26 01:16:16

[LeetCode] 598. Range Addition II的相关文章

LeetCode 598. Range Addition II (区域范围内加法)

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for

[LeetCode&amp;Python] Problem 598. Range Addition II

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for

598. Range Addition II

Problem statement: Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should

LeetCode Range Addition II

原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two 

[LeetCode] Range Addition II 范围相加之二

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for

LeetCode 370. Range Addition (范围加法)$

Assume you have an array of length n initialized with all 0's and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex

LeetCode Range Addition

原题链接在这里:https://leetcode.com/problems/range-addition/description/ 题目: Assume you have an array of length n initialized with all 0's and are given k update operations. Each operation is represented as a triplet: [startIndex, endIndex, inc] which incre

(LeetCode)Pascal&#39;s Triangle II --- 杨辉三角进阶(滚动数组思想)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Subscribe to see which companies asked this question 解题分析: 此处有空间的限制,因此不能正

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵: