C#LeetCode刷题之#598-范围求和 II(Range Addition II)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- 【leetcode刷题笔记】Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode刷题笔记】Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【leetcode刷题笔记】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【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 ...
- [Swift]LeetCode598. 范围求和 II | Range Addition II
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
随机推荐
- IDEA JRebel热部署( IDEA版本是2020.1.2)
1.安装JRebel插件 在IDEA->Settings->plugins先安装JRebel插件: 2.下载工具 安装好JRebel后,找到lanyus大神文章中写的git地址:http: ...
- 树形dp 之 小胖守皇宫
题目描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:有边相连的宫殿间可以互相望见.大内保卫森严,三步一岗,五步一 ...
- 我们通常这样使用Linux弱口令检测!
在Internet环境中,过于简单的口令是服务器面临的最大风险,对于管理员来说,即使找出这些弱口令账号是非常必要的,这样便于采取进一步的安全措施. 这里的话,弱口令检测需要用到一款密码破译软件--Jo ...
- mysql 联合唯一
CREATE TABLE `NewTable` ( `id` int NOT NULL , `name` varchar(255) NULL , `phone` varchar(255) NULL , ...
- 【Unity3D】简单常用的功能实现1——鼠标点击实现移动
[鼠标点击实现移动] 第一步,在要移动的GameObject的Inspector面板中添加角色控制器组件并调整参数. 参数的调整可以依照Scene面板,如下图的绿色线框(角色控制器组件是一种胶囊形状的 ...
- 选择排序的实现以及如何编写测试 #CS61B-sp18-3.1
Selection Sort的思想: 就是在一系列数字中先找到一个最小的放在所有数字的第一个位置上,然后再从余下的数字里面找最小个放在余下的数字里的第一个位置上. 例如: 在这段数据里面我们找到最小的 ...
- dubbo泛化调用 小demo
前两天刚好有个同事来问是否用过 dubbo泛化 调用,不需要通过指定配置.第一次听到的时候,还是有点懵,但觉得有意思,可以学点东西. 立马百度了,找了demo,这篇比较容易上手(http://www. ...
- The Google File System(论文阅读笔记)
概述 GFS:一个可扩展的分布式文件系统,用于大型分布式数据相关应用,TB级的数据,成千上万的并发请求. 设计概览 假设 组件的失效比异常更加常见 多数的文件修改操作是追加数据而不是重写原来的数据 ...
- Button基本用语
1.self.btn2 = Button(root,image = photo,command = self.login) 使用 image 图片作为按钮,command 作为响应 2.self.bt ...
- Excel绘制动态图表 之 极品offset、多种控件动态动图
1.案例1:辅助区域动态图 动态按钮“投资金额”的制作: "数据"菜单下"数据工作”组中的“数据验证”,选择"序列". 2. OFFSET ——函数中 ...