Leftmost Column with at Least a One | LeetCode 30 day Challenge | Day 21 | (C++, Java, Python)

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

LeetCode 1428,
LeetCode 30 day Challenge | Problem 21 | Leftmost Column with at Least a One | 21 April,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Leftmost Column with at Least a One,
lefmost column with 1,
leftmost non-zero column,

#Amazon #Facebook #CodingInterview #LeetCode #30DayChallenge #Google #LeftMostColumn
Рекомендации по теме
Комментарии
Автор

Don't forget to suggest other approaches in the comments section below. Thanks.

KnowledgeCenter
Автор

My C++ Solution:
int &binaryMatrix) {
vector<int> dim = binaryMatrix.dimensions();
int rows = dim[0];
int cols = dim[1];
for(int i = 0; i < rows; i++){
for(int j = cols-1; j>=0; j--){
if(binaryMatrix.get(i, j) == 1){
cols = j;
}
else break;
}
}
return cols==dim[1]?-1:cols;
}

Anand-wiyb
Автор

Solution in swift

class Solution {
func leftMostColumnWithOne(_ binaryMatrix: BinaryMatrix) -> Int {
var dim : [Int] = binaryMatrix.dimensions()
var rows = dim[0]
var cols = dim[1]

if (rows == 0 || cols == 0){
return -1
}

var result = -1
var r = 0
var c = cols - 1
while(r < rows && c >= 0){
if (binaryMatrix.get(r, c) == 1){
result = c;
c -= 1 ;
} else {
r += 1
}
}
return result
}
}

mayureshrao
Автор

Similar Bottom-Up Approach
Accepted Solution

class Solution
{
public int binaryMatrix)
{
List<Integer> list = binaryMatrix.dimensions();

int rows = list.get(0) - 1;
int cols = list.get(1) - 1;

int result = -1;

if(rows == 0 || cols == 0)
{
return -1;
}

while(rows >= 0 && cols >= 0)
{
if(binaryMatrix.get(rows, cols) == 1)
{
result = cols;
cols--;
}
else
{
rows--;
}
}

return result;
}
}

triveninaik
Автор

Always liking the way you have an open mind to such problems and simplify then into an understandable way. Nice work as usual.

williamwambua
Автор

Awesome solution. Subscribed to your channel.

indranilthakur
Автор

# """
# This is BinaryMatrix's API interface.
# You should not implement it, or speculate about its implementation
# """
#class BinaryMatrix(object):
# def get(self, x: int, y: int) -> int:
# def dimensions(self) -> list[]:

class Solution:
def leftMostColumnWithOne(self, binaryMatrix: 'BinaryMatrix') -> int:
if not binaryMatrix:
return -1
m, n=binaryMatrix.dimensions()
res=-1
i, j=0, n-1
while(i<m and j>=0):
if (binaryMatrix.get(i, j)==1):
res=j
j-=1
else:
i+=1
return (res)

shubhamsagar
Автор

Thank you very much for this clear explanation, I was having a hard time understanding it, liked and subscribed to you.

officialspock
Автор

Even I did in 2nd Hint approach... I haven't seen the binary search approach. But will the usual binary search work with duplicates present?

adarshmv
Автор

would it be faster if you can do an extra binary search for each row rather then just "c--"?

vincent-uhuo
join shbcf.ru