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 }
标签:
leetcode
, binary search
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)