zoukankan      html  css  js  c++  java
  • 1428. Leftmost Column with at Least a One

    package LeetCode_1428
    
    /**
     * 1428. Leftmost Column with at Least a One
     * (locked by leetcode)
    (This problem is an interactive problem.)
    
    A binary matrix means that all elements are 0 or 1. For each individual row of the matrix, this row is sorted in non-decreasing order.
    Given a row-sorted binary matrix binaryMatrix, return leftmost column index(0-indexed) with at least a 1 in it. If such index doesn't exist, return -1.
    You can't access the Binary Matrix directly.
    
    You may only access the matrix using a BinaryMatrix interface:
    BinaryMatrix.get(row, col) returns the element of the matrix at index (row, col) (0-indexed).
    BinaryMatrix.dimensions() returns a list of 2 elements [rows, cols], which means the matrix is rows * cols.
    
    Submissions making more than 1000 calls to BinaryMatrix.get will be judged Wrong Answer.
    Also, any solutions that attempt to circumvent the judge will result in disqualification.
    For custom testing purposes you're given the binary matrix mat as input in the following four examples. You will not have access the binary matrix directly.
    
    Example 1:
    Input: mat = [[0,0],[1,1]]
    Output: 0
     * */
    
    class BinaryMatrix {
        /*
            [[0,0,0],
             [0,1,0],
             [0,1,1]]
        * */
        private val ROW = 3
        private val COL = 3
        private val matrix = Array(ROW) { IntArray(COL) }
    
        init {
            //For each individual row of the matrix, this row is sorted in non-decreasing order.
            matrix[0] = intArrayOf(0, 1, 1)
            matrix[1] = intArrayOf(0, 0, 0)
            matrix[2] = intArrayOf(0, 0, 1)
        }
    
        fun get(row: Int, col: Int): Int {
            return matrix[row][col]
        }
    
        fun dimensions(): List<Int> {
            val list = ArrayList<Int>(2)
            list.add(ROW)
            list.add(COL)
            return list
        }
    
    }
    
    class Solution {
        fun leftMostColumnWithOne(binaryMatrix: BinaryMatrix): Int {
            /*
            * method 1: binary search, Time complexity:O(row*log col)
            * */
            val list = binaryMatrix.dimensions()
            val rows = list.get(0)
            val cols = list.get(1)
            var minCol = Int.MAX_VALUE
            for (row in 0 until rows) {
                minCol = Math.min(minCol, findFirstOne(row, cols, binaryMatrix))
            }
            return if (minCol == Int.MAX_VALUE) -1 else minCol
        }
    
        private fun findFirstOne(row: Int, cols: Int, binaryMatrix: BinaryMatrix): Int {
            var left = 0
            var right = cols - 1
            while (left <= right) {
                val mid = (left + right) / 2
                if (binaryMatrix.get(row, mid) == 1) {
                    //if found out 1 in middle, we continue search left side, because we need find the left most one
                    right = mid - 1
                } else {
                    left = mid + 1
                }
            }
            return left
        }
    }
  • 相关阅读:
    Elasticsearch 技术分析(五):如何通过SQL查询Elasticsearch
    IntelliJ IDEA 2018.3 重大升级,哪些功能打动了你?
    终于有人把“TCC分布式事务”实现原理讲明白了!
    Elasticsearch 技术分析(一): 基础入门
    拜托!面试请不要再问我Spring Cloud底层原理
    一个正则表达式引发的血案
    程序员啊,他又加班了
    程序员你为什么这么累 | 编码规范
    全文搜索引擎 ElasticSearch 还是 Solr?
    TCP三次握手原理,你真的了解吗?
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12997224.html
Copyright © 2011-2022 走看看