当前位置:网站首页>Leetcode 1351. Negative numbers in statistical ordered matrices
Leetcode 1351. Negative numbers in statistical ordered matrices
2022-04-23 20:26:00 【Die in a trap】
1351、 Count the negative numbers in an ordered matrix
1) Title Description
To give you one m * n
Matrix grid
, The elements of a matrix, whether by row or column , All in non increasing order . Please count and return grid
in negative Number of .
Example 1:
Input :grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output :8
explain : Common matrix 8 Negative numbers .
Example 2:
Input :grid = [[3,2],[1,0]]
Output :0
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 100
-100 <= grid[i][j] <= 100
2) analysis
Traverse .
- Traverse to the first element of each line , Judge positive and negative , If negative , The result directly adds the product of the length of one line and the number of remaining lines , Exit traversal ;
- If the first element of the current line is nonnegative , Traverse the row , Judge the positive and negative of each element , If negative , The result is added with the number of elements remaining in the current line , Exit the traversal of the current line .
Traveled through , Intra bank dichotomy .
- Traverse to the first element of each line , Judge positive and negative , If negative , The result directly adds the product of the length of one line and the number of remaining lines , Exit traversal ;
- If the first element of the current line is nonnegative , Find the position of the first negative number by two points , The result is added with the number of elements remaining in the current line , Exit the search of the current line .
3)C++
Code
Traverse
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int res=0;
for(int i=0;i<grid.size();i++){
if(grid[i][0]>=0){
for(int j=0;j<grid[0].size();j++){
if(grid[i][j]<0){
res+=grid[0].size()-j;
break;
}
}
}
else{
res+=grid[0].size()*(grid.size()-i);
break;
}
}
return res;
}
};
Traveled through , Intra bank dichotomy .
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int res=0;
int m=grid.size();
int n=grid[0].size();
for(int i=0;i<m;i++){
if(grid[i][0]>=0){
if(grid[i][n-1]>=0)
continue;
else{
int left=0;
int right=n-1;
while(left<=right){
int mid=(left+right)/2;
if(grid[i][mid]>=0)
left=mid+1;
else
right=mid-1;
}
res+=grid[i][left]<0?n-left:0;
}
}
else{
res+=n*(m-i);
break;
}
}
return res;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107752.html
边栏推荐
- LeetCode 20、有效的括号
- LeetCode 1351、统计有序矩阵中的负数
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
- 【栈和队列专题】—— 滑动窗口
- Installation and use of NVM
- Matlab analytic hierarchy process to quickly calculate the weight
- Mysql database and table building: the difference between utf8 and utf8mb4
- Numpy sort search count set
- PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
- Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
猜你喜欢
A useless confession artifact
. Ren -- the intimate artifact in the field of vertical Recruitment!
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]
Automatically fill in body temperature and win10 task plan
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
Commit and ROLLBACK in DCL of 16mysql
Installation and use of NVM
Building the tide, building the foundation and winning the future -- the successful holding of zdns Partner Conference
[stack and queue topics] - sliding window
随机推荐
Unity 模型整体更改材质
Installation and use of NVM
【PTA】L1-002 打印沙漏
How does onlyoffice solve no route to host
16MySQL之DCL 中 COMMIT和ROllBACK
[graph theory brush question-4] force deduction 778 Swimming in a rising pool
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Investigate why close is required after sqlsession is used in mybatties
Plato Farm元宇宙IEO上线四大,链上交易颇高
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Paper writing 19: the difference between conference papers and journal papers
An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
Is the wechat CICC wealth high-end zone safe? How to open an account for securities
[PTA] l2-011 play with binary tree
【PTA】整除光棍
PCL点云处理之直线与平面的交点计算(五十三)
Modeling based on catiav6
Mysql database backup scheme
Numpy Index & slice & iteration