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的更多相关文章

  1. 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 ...

  2. 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.免费应用程序调试最 ...

  3. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  4. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  5. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  6. Failed to connect to database. Maximum number of conections to instance exceeded

    我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...

  7. POJ2699 The Maximum Number of Strong Kings

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tour ...

  8. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. 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 说明:最大线 ...

随机推荐

  1. [C++] Class (part 2)

    Members that are const or reference must be initialized. Similary, members that are of a class type ...

  2. 开启假期JAVA之路

    . 从最基础的JAVA开始学起,已经上了三节课啦!希望在课程结束后能完成一个令自己满意的连连看项目,期待ing~ 慢慢的从简单的代码上手了~ . 用循环输出等腰三角形的效果 import java.u ...

  3. LintCode-381.螺旋矩阵 II

    螺旋矩阵 II 给你一个数n生成一个包含1-n^2的螺旋形矩阵 样例 n = 3 矩阵为 [     [ 1, 2, 3 ],     [ 8, 9, 4 ],     [ 7, 6, 5 ] ] 标 ...

  4. LR脚本编写时的几个小技巧

    参数化空值 如上图所示,当参数化时某个值需要为空值(非空格),直接在参数化文件中空一行/格即可,虽然Parameter List界面上没有显示空的那一行,但并不影响取值. 手工日志跟踪 lr_set_ ...

  5. xpath教程一---简单的标签搜索

    工具 Python3版本 lxml库[优点是解析快] HTML代码块[从网络中获取或者自己杜撰一个] requests[推荐安装,从网页上获取网页代码练手,再好不过了] 讲解 网页代码都是成对的标签, ...

  6. ubuntu 安装xdebug

    Add XDebug to Ubuntu 14.04 Submitted by Wilbur on Tue, 06/17/2014 - 12:49pm It's pretty easy to add ...

  7. 显示系统中所有的socket信息

    netstat -aon /proc/net/tcp /proc/net/udp /proc/net/unix 相关的代码是:tcp4_seq_show(struct seq_file *file, ...

  8. 【Python】爬虫与反爬虫大战

    爬虫与发爬虫的厮杀,一方为了拿到数据,一方为了防止爬虫拿到数据,谁是最后的赢家? 重新理解爬虫中的一些概念 爬虫:自动获取网站数据的程序反爬虫:使用技术手段防止爬虫程序爬取数据误伤:反爬虫技术将普通用 ...

  9. 【bzoj2073】[POI2004]PRZ 状态压缩dp

    题目描述 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时只能分批 ...

  10. NOIP2002 提高组

    [NOIP2002] 提高组 T1.均分纸牌 算法:贪心(模拟) [分析]: 1.简化 2.过滤 3.辩证法  详见课件的例7 还有一种类似的思路是:求出平均值后,i←1 to n-1扫描,若a[i] ...