[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 说明:最大线 ...
随机推荐
- Spring 3整合Quartz 2实现定时任务:动态添加任务
先展示一下后台管理定时任务效果图: 1.新增任务页面: 2.列表页(实现任务的禁用启用) 3.数据库脚本: -- ------------------------------ Table struct ...
- HDU 4617 Weapon(三维几何)
Problem Description Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a c ...
- 自测之Lesson9:时钟与信号
题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...
- 第四课——MFC应用程序框架
一.MFC应用程序类型 上篇文章的彩蛋:可通过使用MFC应用程序向导(MFC AppWizard)的功能来创建所需要的应用程序,这意味着不需要输入任何代码.MFC除了应用程序向导,还对应用程序项目有着 ...
- 20172332 实验一《Java开发环境的熟悉》实验报告
20172332 2017-2018-2 <程序设计与数据结构>实验一报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 于欣月 学号:20172332 实验教师:王 ...
- POJ 1995 (快速幂)
这道题普通做法会发生溢出且会超时,应当用快速幂来求解. 快速幂讲解 #include <cstdio> #include <cmath> using namespace std ...
- P4编程环境搭建遇到的问题与解决方法
在经历了无数的折腾之后,算是折腾,最后采用的是陈翔学长的脚本加上可爱的shell调整装好的. 链接:p4Install 也许是ubuntu18.04的问题,也有可能是我自己把这个系统折腾的有点杂乱的原 ...
- Uncaught Error: Syntax error, unrecognized expression: |117的js错误
117指的是js代码在浏览器运行时的出错的行号 var a="117|117" 前面的错误是由于有特殊符号“|”,用$("txtId"+a).val();去取 ...
- Ubuntu16.04修改IP
首先用root用户登陆,然后输入你root的密码.如下图: 然后编辑interfaces文件,该文件位于/etc/network/下,执行如下命令: vim /etc/network/interf ...
- LintCode-204.单例
单例 单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计> 模式为单例.例如,对于 class Mouse (不是动物的mouse哦), ...