zoukankan      html  css  js  c++  java
  • 315. Count of Smaller Numbers After Self

    题目:

    /**
     * 315. Count of Smaller Numbers After Self
     * https://leetcode.com/problems/count-of-smaller-numbers-after-self/description/
     * https://www.cnblogs.com/grandyang/p/5078490.html
     *
    You are given an integer array nums and you have to return a new counts array.
    The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
    
    Example:
    
    Input: [5,2,6,1]
    Output: [2,1,1,0]
    Explanation:
    To the right of 5 there are 2 smaller elements (2 and 1).
    To the right of 2 there is only 1 smaller element (1).
    To the right of 6 there is 1 smaller element (1).
    To the right of 1 there is 0 smaller element.
     * */

    首先使用brute force方法,结果accept了:

    class Solution {
        fun countSmaller(nums: IntArray): List<Int> {
            val result = ArrayList<Int>()
            val size = nums.size
            
         
        val list = ArrayList<Int>()
        for (i in size - 1 downTo 0) {
       if (list.isEmpty()) {
       list.add(nums[i])
       result.add(0)
       } else if (nums[i] > list.get(list.size - 1)) {
       //insert into last
       list.add(list.size, nums[i])
       result.add(list.size - 1)
      } else {
       val index = binarySearch(list, nums[i])
       list.add(index, nums[i])
       result.add(index)
       }
        }
        result.reverse()
        return result
        }
    
        private fun help(size: Int, currentIndex: Int, nums: IntArray, result: ArrayList<Int>) {
            var count = 0
            val currentNum = nums[currentIndex]
            var newIndex = currentIndex + 1
            if (newIndex == size) {
                newIndex = size - 1
            }
            for (i in newIndex until size) {
                if (nums[i] < currentNum) {
                    count++
                }
            }
            result.add(count)
        }
    }

    以下用binary search去优化:

     /*
       * solution 2: binary search, Time complexity: O(nlogn), Space complexity:O(n)
       * [left,right), 左闭右开,mean not including right
       * for example: for loop,
       * for(i in 0 until 10)=>[0,10)=>0 count to 9
       * for(i in 0 ... 10)=>[0,10]=>0 count to 10
       * */
        private fun binarySearch(list: ArrayList<Int>, target: Int): Int {
            var left = 0
            var right = list.size - 1
            while (left < right) {
                val mid = (left + right) / 2
                if (target > list[mid]) {
                    left = mid + 1
                } else {
                    right = mid
                }
            }
            //return the position that can be insert target into list,
            //the element in the left of position which just less than target
            return left
        }
  • 相关阅读:
    python + selenium 环境搭建及问题
    SQL数据库各种查询建表插入集合-待续持续更新
    使chrome支持跨域访问
    PayPal 开发详解(七):运行REST API SAMPLE
    PayPal 开发详解(六):下载paypal立即付款SDK 并编译打包
    PayPal 开发详解(五):创建第一个应用,获取clientId和clientSecret
    PayPal 开发详解(四):买家付款
    PayPal 开发详解(三):在网站上创建【立即付款】按钮
    PayPal 开发详解(二):开启【自动返回】和【数据传输】
    PayPal 开发详解(一):注册PayPal帐号
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/11638015.html
Copyright © 2011-2022 走看看