315:计算右侧小于当前元素的个数

This commit is contained in:
huangge1199@hotmail.com 2021-07-18 13:27:49 +08:00
parent 8c6aeafc53
commit 5e99be931f
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,105 @@
//给定一个整数数组 nums按要求返回一个新数组 counts数组 counts 有该性质 counts[i] 的值是 nums[i] 右侧小于 num
//s[i] 的元素的数量
//
//
//
// 示例
//
// 输入nums = [5,2,6,1]
//输出[2,1,1,0]
//解释
//5 的右侧有 2 个更小的元素 (2 1)
//2 的右侧仅有 1 个更小的元素 (1)
//6 的右侧有 1 个更小的元素 (1)
//1 的右侧有 0 个更小的元素
//
//
//
//
// 提示
//
//
// 0 <= nums.length <= 10^5
// -10^4 <= nums[i] <= 10^4
//
// Related Topics 树状数组 线段树 数组 二分查找 分治 有序集合 归并排序
// 👍 615 👎 0
package leetcode.editor.cn;
import java.util.*;
//315:计算右侧小于当前元素的个数
class CountOfSmallerNumbersAfterSelf {
public static void main(String[] args) {
//测试代码
Solution solution = new CountOfSmallerNumbersAfterSelf().new Solution();
System.out.println(solution.countSmaller(new int[]{1,-3,-2}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
private int[] c;
private int[] a;
public List<Integer> countSmaller(int[] nums) {
List<Integer> resultList = new ArrayList<Integer>();
discretization(nums);
init(nums.length + 5);
for (int i = nums.length - 1; i >= 0; --i) {
int id = getId(nums[i]);
resultList.add(query(id - 1));
update(id);
}
Collections.reverse(resultList);
return resultList;
}
private void init(int length) {
c = new int[length];
Arrays.fill(c, 0);
}
private int lowBit(int x) {
return x & (-x);
}
private void update(int pos) {
while (pos < c.length) {
c[pos] += 1;
pos += lowBit(pos);
}
}
private int query(int pos) {
int ret = 0;
while (pos > 0) {
ret += c[pos];
pos -= lowBit(pos);
}
return ret;
}
private void discretization(int[] nums) {
Set<Integer> set = new HashSet<Integer>();
for (int num : nums) {
set.add(num);
}
int size = set.size();
a = new int[size];
int index = 0;
for (int num : set) {
a[index++] = num;
}
Arrays.sort(a);
}
private int getId(int x) {
return Arrays.binarySearch(a, x) + 1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,24 @@
<p>给定一个整数数组 <em>nums</em>,按要求返回一个新数组&nbsp;<em>counts</em>。数组 <em>counts</em> 有该性质: <code>counts[i]</code> 的值是&nbsp; <code>nums[i]</code> 右侧小于&nbsp;<code>nums[i]</code> 的元素的数量。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>nums = [5,2,6,1]
<strong>输出:</strong><code>[2,1,1,0]
<strong>解释:</strong></code>
5 的右侧有 <strong>2 </strong>个更小的元素 (2 和 1)
2 的右侧仅有 <strong>1 </strong>个更小的元素 (1)
6 的右侧有 <strong>1 </strong>个更小的元素 (1)
1 的右侧有 <strong>0 </strong>个更小的元素
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>0 &lt;= nums.length &lt;= 10^5</code></li>
<li><code>-10^4&nbsp;&lt;= nums[i] &lt;= 10^4</code></li>
</ul>
<div><div>Related Topics</div><div><li>树状数组</li><li>线段树</li><li>数组</li><li>二分查找</li><li>分治</li><li>有序集合</li><li>归并排序</li></div></div>\n<div><li>👍 615</li><li>👎 0</li></div>