Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

103
Visualizações
Time complexity of searching for target value in 2D matrix?

The actual problem is pretty simple, implement an algorithm that returns true if the target value is contained within the matrix. Here are the two solutions I came up with. I'm not sure which one would be preferable? I believe solution 1 is faster since we don't need to build a new array, however would this be significantly faster?

Solution 1:

var searchMatrix = function(matrix, target) {
    let cols = matrix[0].length;
    let rows = matrix.length;
    let left = 0; 
    let right = cols*rows - 1;
    
    while(left <= right) {
        let midIndex = left + Math.floor((right-left)/2);
        let midValue = matrix[Math.floor(midIndex/cols)][Math.floor(midIndex%cols)];
        console.log(midValue);
        if(midValue === target) {
            return true;
        }
        else if(midValue < target) {
            left = midIndex + 1;
        } else {
            right = midIndex - 1;
        }
    }
    return false;
};

Solution 2:

var searchMatrix = function(matrix, target) {
    let arr = []
    for(let row of matrix) {
        arr = [...arr,...row];
    }
    let left = 0;
    let right = arr.length - 1;
    
    while(left <= right) {
        let middle = left + Math.floor((right-left)/2);
        if(arr[middle] === target) {
            return true;
        } else if(arr[middle] < target) {
            left = middle + 1;
        } else {
            right = middle - 1;
        }
    }
        
    return false;
};

Based on my understanding, the main step we're adding is converting the matrix into a regular Array. Would this make the algorithm O(n) since we have to add every element into the new array?

For solution 1, we don't have to create a new array so we'd have constant space correct? I'm not particularly sure how to explain the first solution is preferable in terms of time/space.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Based on the comments added to the post, readability and the ability to analyze the time complexity would yield the best answer. Perhaps the first option would be most ideal if we can make it more readable via a separate function used to calculate the matrix value.

Option 2 does take more space since we're creating an additional array to search through.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda