zoukankan      html  css  js  c++  java
  • Daily Coding Problem: Problem #1000

    /**
     *This problem was asked by Uber.
    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
    Find the minimum element in O(log N) time.
    You may assume the array does not contain duplicates.
    For example, given [5, 7, 10, 3, 4], return 3.
     * */
    class Problem_1000 {
        /*
        * Solution: Divide and Conquer, check left side and right side
        * Time:O(logn), Space:O(logn)
        * */
        fun findMin(array: IntArray): Int {
            return help(array, 0, array.lastIndex)
        }
    
        private fun help(array: IntArray, i: Int, j: Int): Int {
            //because array is sorted in ascending
            if (array[i] <= array[j]) {
                //so this one is the minimum of two side
                return array[i]
            }
            //compare left side and right side
            val mid = i + (j - i) / 2
            val leftSideMinimum = help(array, i, mid)
            val rightSideMinimum = help(array, mid + 1, j)
            return Math.min(leftSideMinimum, rightSideMinimum)
        }
    }
  • 相关阅读:
    nginx反向代理
    遇到的好玩的mvc路由
    有意思的OWIN,附脱离iis的webapi
    nginx转发配置
    SQL 2016安装中遇到的问题
    3级级联 国家--城市
    box.css
    common.css
    节假日设置
    Order_Leave.aspx
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15307986.html
Copyright © 2011-2022 走看看