问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3881 访问。

给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作。

操作用二维数组表示,其中的每个操作用一个含有两个正整数 a 和 b 的数组表示,含义是将所有符合 0 <= i < a 以及 0 <= j < b 的元素 M[i][j] 的值都增加 1。

在执行给定的一系列操作后,你需要返回矩阵中含有最大整数的元素个数。

输入: 

m = 3, n = 3

operations = [[2,2],[3,3]]

输出: 4

解释: 

初始状态, M = 

[[0, 0, 0],

 [0, 0, 0],

 [0, 0, 0]]

执行完操作 [2,2] 后, M = 

[[1, 1, 0],

 [1, 1, 0],

 [0, 0, 0]]

执行完操作 [3,3] 后, M = 

[[2, 2, 1],

 [2, 2, 1],

 [1, 1, 1]]

M 中最大的整数是 2, 而且 M 中有4个值为2的元素。因此返回 4。

注意:

m 和 n 的范围是 [1,40000]。

a 的范围是 [1,m],b 的范围是 [1,n]。

操作数目不超过 10000。


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 all 0 <= i < a and 0 <= j < b.

You need to count and return the number of maximum integers in the matrix after performing all the operations.

Input: 

m = 3, n = 3

operations = [[2,2],[3,3]]

Output: 4

Explanation: 

Initially, M = 

[[0, 0, 0],

 [0, 0, 0],

 [0, 0, 0]]

After performing [2,2], M = 

[[1, 1, 0],

 [1, 1, 0],

 [0, 0, 0]]

After performing [3,3], M = 

[[2, 2, 1],

 [2, 2, 1],

 [1, 1, 1]]

So the maximum integer in M is 2, and there are four of it in M. So return 4.

Note:

The range of m and n is [1,40000].

The range of a is [1,m], and the range of b is [1,n].

The range of operations size won't exceed 10,000.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3881 访问。

public class Program {

    public static void Main(string[] args) {
var m = 3;
var n = 3;
var ops = new int[,] { { 2, 2 }, { 3, 3 } }; var res = MaxCount(m, n, ops);
Console.WriteLine(res); Console.ReadKey();
} private static int MaxCount(int m, int n, int[,] ops) {
//该题千万不要使用暴力解法,肯定TLE
//如果操作为空,则直接返回所有0的数量
if(ops.Length == 0) return m * n;
//分别记录一维和二维第一个数
int minOne = ops[0, 0];
int minTwo = ops[0, 1];
//循环
for(var i = 0; i < ops.GetLength(0); i++) {
//找到最小值
minOne = Math.Min(ops[i, 0], minOne);
minTwo = Math.Min(ops[i, 1], minTwo);
}
//一维和二维的最小值的乘积即为该题的解
return minOne * minTwo;
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3881 访问。

4

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#598-范围求和 II​​​​​​​(Range Addition II)的更多相关文章

  1. 【leetcode刷题笔记】Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  2. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  3. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. [Swift]LeetCode598. 范围求和 II | Range Addition II

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  6. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

  7. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  9. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

随机推荐

  1. Python 爬取 42 年高考数据,告诉你高考为什么这么难?

    作者 | 徐麟 历年录取率 可能很多经历过高考的人都不知道高考的全称,高考实际上是普通高等学校招生全国统一考试的简称.从1977年国家恢复高考制度至今,高考经历了许多的改革,其中最为显著的变化就是录取 ...

  2. OSCP Learning Notes - File Transfers(2)

    Metasploit Target Server: Kioptrix Level 1 (1) Start the Metasploit on Kali Linux. (2) Set the modul ...

  3. OpenXml demo

    class OpenXmlDemo { /* * excel 对象结构 * SpreadsheetDocument * >WorkbookPart * >WorksheetPart * & ...

  4. SparkCore

    一.概述 1,定义 RDD(Resilient Distributed Dataset)叫做弹性分布式数据集,是Spark中最基本的数据抽象.代码中是一个抽象类,它代表一个不可变.可分区.里面的元素可 ...

  5. 《Python测试开发技术栈—巴哥职场进化记》—前言

    写在前面 今年从4月份开始写一本讲Python测试开发技术栈的书,主要有两个目的,第一是将自己掌握的一些内容分享给大家,第二是希望自己能系统的梳理和学习Python相关的技术栈.当时我本来打算以故事体 ...

  6. Java基础之Bridge method(桥接方法)

    1.什么是桥接方法 桥接方法是 JDK 1.5 引入泛型后,为了使Java的泛型方法生成的字节码和 1.5 版本前的字节码相兼容,由编译器自动生成的方法. 判断方法 我们可以通过 Method.isB ...

  7. 剑指offo记录

    一.二维数组中的查找 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是 ...

  8. 【Vue组件通信】props、$ref、$emit,组件传值

    1.什么是组件通信 组件间如何通信,也就成为了vue中重点知识,组件通信,涉及到组件之间数据的传递.类似NET POST/GET参数传递. Vue基本的三种传递方式** (props.\(ref.\) ...

  9. TSGCTF-web Beginner's Web (js内置方法__defineSetter__)

    const fastify = require('fastify'); const nunjucks = require('nunjucks'); const crypto = require('cr ...

  10. Java面试必问:ThreadLocal终极篇 淦!

    点赞再看,养成习惯,微信搜一搜[敖丙]关注这个互联网苟且偷生的程序员. 本文 GitHub https://github.com/JavaFamily 已收录,有一线大厂面试完整考点.资料以及我的系列 ...