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
        }
    }
  • 相关阅读:
    在Java中,final修饰的类有什么特点
    基于 枚举值 输出 枚举描述的 jackson 自定义注解方法
    基于 r2dbc jpa java reactor流式编程的查询合并
    分组后 排除存在某种情况的 的查询
    基于Mysql 根据输入值 为基础的 环形排序
    时间范围内的按时间统计的每日数据填充
    记一次vue发版,在nginx下不乱码,在IIS下乱码的奇葩经历
    处理webflux 项目 增加 content-path
    java stream 不执行转换 不执行 管道中的操作
    增加一个spring mvc 的枚举转换器
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12997224.html
Copyright © 2011-2022 走看看