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)
        }
    }
  • 相关阅读:
    IO模型
    Java NIO概述
    消息系统避免分布式事务
    JVM调优总结
    设计模式的六大原则
    Java 内存区域与内存溢出
    windows go安装
    ZooKeeper原理及使用
    再谈HashMap
    Html5 播放实时音频流
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15307986.html
Copyright © 2011-2022 走看看