[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 说明:最大线 ...
随机推荐
- 关于wcf服务编译平台是x86, 运行平台是x64时,如何调试
关于调试CTDC项目中的的 wcf服务时注意事项: 因为wcf项目引用的的 x86的程序集,所以wcf生成的目标平台为x86.故在64系统上调试需要执行下面的脚本 具体操作步骤: 1. 必须使用201 ...
- openstack如何整合vmare最佳方案
OpenStack中国社区编者按:通过多年的发展,VMWare在虚拟化市场处于领军地位,很多企业部署了VMWare虚拟化方案,随着OpenStack云计算平台的快速崛起,很多企业都面临一个问题:能否. ...
- Python中除法:/和//
在Python中,除法有两种:/和//. X / Y 对于Python2.X来说,如果两个操作数都是整数,那么结果将向下取整(这个和C里面的不同,C里面是向0取整),也就是说,如果结果本来是-2.5, ...
- Coins and Queries(map迭代器+贪心)
题意 n个硬币,q次询问.第二行给你n个硬币的面值(保证都是2的次幂!).每次询问组成b块钱,最少需要多少个硬币? Example Input 5 42 4 8 2 4851410 Output 1- ...
- Beta阶段项目展示博客
Beta阶段项目展示 团队成员的简介 详细见团队简介 角色 姓名 照片 项目经理,策划 游心 策划 王子铭 策划 蔡帜 美工 赵晓宇 美工 王辰昱 开发.架构师 解小锐 开发 陈鑫 开发 李金奇 开发 ...
- LintCode-105.复制带随机指针的链表
复制带随机指针的链表 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点. 返回一个深拷贝的链表. 挑战 可否使用O(1)的空间 标签 哈希表 链表 优步 code / ...
- 华为oj----iNOC产品部-杨辉三角的变形 .
此题提供三种方法,第一种,一开始就能想到的,设置一个足够大的数组存储生成的杨辉三角,然后进行判断就行,此方法参见:华为oj iNOC产品部-杨辉三角的变形 另一种方法是采用递归: 三角形的每行的个数为 ...
- matlab的二维卷积操作(转)
MATLAB的conv2函数实现步骤(conv2(A,B)): 其中,矩阵A和B的尺寸分别为ma*na即mb*nb ① 对矩阵A补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都 ...
- pythoh使用 xpath去除空格空格
html_str = """ <!DOCTYPE html> <html lang="en"> <head> &l ...
- QT分析之调试跟踪系统
原文地址:http://blog.163.com/net_worm/blog/static/127702419201002004518944/ 在我们前面的分析中,经常看到qWarning()和qDe ...