Binary Search on 2D matrix in Ruby


  • 0
    M
    # @param {Integer[][]} matrix
    # @param {Integer} target
    # @return {Boolean}
    def search_matrix(matrix, target)
        left = 0
        right = matrix.length - 1
        
        while left <= right 
            mid = (left + right)/2
            
      
            if matrix[mid].include?(target)
                return true
            end
            
            unless matrix[mid][0].nil? || matrix[mid][-1].nil?
                if target < matrix[mid][0] 
                    right = mid - 1
                elsif target > matrix[mid][-1]
                    left = mid + 1
                end
            end
        end
        
        return false
    end```

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.