[Coding Practice] Maximum number of zeros in NxN matrix
Question:
Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in a row after 0. Find the index of the row which contains maximum number of zeros.
Example: lets say 5×5 matrix
1 0 0 0 0
1 1 0 0 0
1 1 1 1 1
0 0 0 0 0
1 1 1 0 0
For this input answer is 4th row.
solution should have time complexity less than N^2
http://www.geeksforgeeks.org/forums/topic/amazon-interview-question-for-software-engineerdeveloper-about-algorithms-24/
Solutions:
1. O(n2)Solution:
Start from a11 to a1n (go through the column) once 0 is met, return current index of row. If no 0 is met, go through the second column (a21 to a2n).
在最坏情况下需要遍历矩阵中的每个元素。
2. O(nlogn)Solution:
Search each row from top to down, in each row, use binary search to get the index of first 0. Use a variable to save the max index of 0. In worst case, O(nlogn) in time complexity.
3. O(n)Solution:
Start from the top right element a1n, if 0 is met, move left, if 1 is met or left corner is reached, record current index of row to variable T, then move down until 0 is met. Repeat these steps until move down to the bottom of matrix (anx) or left corner of matrix (ax1). The value of T is the answer. In worst case, time complexity O(2n) = O(n).
Code of Solution 3:
#include<stdio.h>
#include <ctime>
#include <cstdlib>
using namespace std; int MaximumZeroRow(int** matrix, int size){
if(matrix == NULL)
return ;
int i = , j = size - , T = ;
while(){
if(!matrix[i][j]){
j--;
}
if(matrix[i][j] || j < ){
T = i;
i++;
}
if( i > (size - ) || j < ) break;
}
return T;
} int main(){
srand(time());
int **array;
array = new int *[];
for(int i = ; i < ; i++){
array[i] = new int[];
for(int j = ; j < ; j++){
array[i][j] = (j == ? (rand() & ) : (rand() & ) & array[i][j-]);
printf("%d ", array[i][j]);
}
printf("\n");
}
printf("\n"); printf("Max 0 row index: %d\n", MaximumZeroRow(array, )); return ;
}
[Coding Practice] Maximum number of zeros in NxN matrix的更多相关文章
- The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.
[root@localhost ~]# vi /etc/security/limits.conf # /etc/security/limits.conf # #Each line describes ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- Failed to connect to database. Maximum number of conections to instance exceeded
我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...
- POJ2699 The Maximum Number of Strong Kings
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2102 Accepted: 975 Description A tour ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080
1.INFO: Maximum number of threads (200) created for connector with address null and port 8091 说明:最大线 ...
随机推荐
- LeetCode - 326, 342, 231 Power of Three, Four, and Two
1. 问题 231. Power of Two: 判断一个整数是否是2的n次方,其中n是非负整数 342. Power of Four: 判断一个整数是否是4的n次方,其中n是非负整数 326. Po ...
- [leetcode-667-Beautiful Arrangement II]
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- Android中的回调Callback
回调就是外部设置一个方法给一个对象, 这个对象可以执行外部设置的方法, 通常这个方法是定义在接口中的抽象方法, 外部设置的时候直接设置这个接口对象即可. 例如给安卓添加按钮点击事件, 我们创建了OnC ...
- oracle数据库之存储函数和过程
一.引言 ORACLE 提供可以把 PL/SQL 程序存储在数据库中,并可以在任何地方来运行它.这样就叫存储过程或函数.过程和函数统称为 PL/SQL 子程序,他们是被命名的 PL/SQL 块 ...
- 寒假作业end
开始写博客的个人体会 自己打的链表过不了,果然,心存侥幸是不行的,被揪出来也不错,很感谢畅畅酱. 学术诚信的重要性 爱因斯坦说过:"大多数人说是才智造就了伟大的科学家,他们错了,是人格.&q ...
- Unity3d学习日记(二)
跟着教程做让背景可以滚动起来并添加了背景的粒子特效,加入了敌机. ctrl攻击,↑↓←→移动,Game Over后按R重新开始游戏. Space Shooter游戏地址:http://ya ...
- 3ds Max学习日记(二)
搞了一天终于把第二章弄完了,虽然有点慢,但也是没有办法,毕竟中途学累了总得放松一下吧. 又get到了一个新知识点,是关于3ds max的捕捉工具的,详情见3DS MAX 学习笔记 一(捕捉工具 ...
- chrome扩展程序中以编程方式插入内容脚本不生效的问题
chrome扩展程序中内容脚本有两种插入方式:(https://crxdoc-zh.appspot.com/extensions/content_scripts) 1. 清单文件: 这种方式会在打开每 ...
- C# 7.0 新特性
先列一下相关的语法: 1.out-variables(Out变量) 2.Tuples(元组) 3.Pattern Matching(匹配模式) 4.ref locals and returns (局部 ...
- 织梦CMS建站入门学习(一)
一.下载与安装. 首先到织梦官网下载软件,可选择UTF8或GBK不同编码格式,如果电脑没有PHP环境,还要下载dede自带的PHP环境软件. 将软件中的upload文件夹内容复制到WWW文件夹下,然后 ...