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 ...
随机推荐
- SSRF漏洞简单分析
什么是SSRF漏洞 SSRF(服务器端请求伪造)是一种由攻击者构造请求,服务器端发起请求的安全漏洞,所以,一般情况下,SSRF攻击的目标是外网无法访问的内部系统. SSRF漏洞形成原理. SSRF的形 ...
- java 手机号码归属地查询
下面是利用第三方接口实现手机号码归属地查询 (复制请标明出处或留言) package com.test.yyc; import java.io.BufferedReader; import java. ...
- office 2016激活方法windows
安装office 2016 win+R,输入“cmd” 根据系统位数和安装的office位数选择相应的命令 OFFICE 64位 和 WINDOWS 64位 cscript "C:\Prog ...
- 项目总结,彻底掌握NodeJS中如何使用Sequelize
前言 sequelize是什么? sequelize是基于NodeJs的ORM框架,它适用于不同的数据库,如:Postgres.MySQL.SQLite.MariaDB,我们可以通过sequelize ...
- Ionic 移动端
<body ng-app="testApp"> <ion-header-bar align-title="left" class=" ...
- matplotlib示例
plt.plot 内只有一个列表示例 import matplotlib.pyplot as plt lst = [4.53,1.94,4.75,0.43,2.02,1.22,2.13,2.77] p ...
- matplotlib基础汇总_03
四图 直方图 [直方图的参数只有一个x!!!不像条形图需要传入x,y] hist()的参数 bins 可以是一个bin数量的整数值,也可以是表示bin的一个序列.默认值为10 normed 如果值为T ...
- numpy第三方库
# 导入numpy 并赋予别名 np import numpy as np # 创建数组的常用的几种方式(列表,元组,range,arange,linspace(创建的是等差数组),zeros(全为 ...
- 3-Pandas之什么是Panel?
一.什么是Panel Series:包含一维索引的一组数据 DataFrame:包含index和columns两个轴 Panel(面板):一种三维数据容器 一个Panel对象由3个轴构成: items ...
- PHP array_slice() 函数
实例 从数组的第二个元素开始取出,并返回直到数组末端的所有元素: <?php$a=array("red","green","blue" ...