简体   繁体   English

在小于 O(n^2) 的范围内对给定的整数列表进行排名

[英]Ranking a given list of integers in less than O(n^2)

How to assign ranks to elements in a given list of integers?如何为给定整数列表中的元素分配等级? Is there an algorithm for that with time complexity less than O(n^2) ?是否有时间复杂度小于 O(n^2) 的算法?

Input:  2 6 7 3 9
Output: 1 3 4 2 5

You can easily do this in O(n * log n) by sorting:您可以通过排序在O(n * log n)轻松完成此操作:

int[] input = //...
int[] input = new int[]{2, 6, 7, 3, 9};
Integer[] indices = new Integer[input.length];
for (int i = 0; i < indices.length; i++) {
    indices[i] = i;
}

// find permutation of indices that would yield sorted input
Arrays.sort(indices, Comparator.comparingInt(i -> input[i]));

// invert permutation and add 1 to get rank 
int[] ranks = new int[indices.length];
for (int i = 0; i < indices.length; i++) {
    ranks[indices[i]] = i+1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从给定范围 Q 查询中创建数组 [1, 2, 3, 1],其中 N 个元素每个 [1, 3] [2, 3] [3, 4] 小于 O(QN)? - How to create array [1, 2, 3, 1] from given range Q queries with N elements each [1, 3] [2, 3] [3, 4] in less than O(QN)? 给定排序列表如何创建在 O(log(N)) 时间内不会落在某个范围内的整数列表? - Given sorted list how to create list of integers that wont fall in certain range in O(log(N)) time? 查找列表中小于给定值的最大整数和(代码跟踪) - Finding the largest sum of integers in a list less than a given value (code tracing) 从排序数组中查找小于O(n)的唯一数字 - Finding unique numbers from sorted array in less than O(n) 找到小于O(N)的序列的第n项 - Find nth term of a sequence in less than O(N) 是否可以使嵌套的for循环小于O(N ^ 2)? - Is it possible to make this nested for loop less than O(N^2)? 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2) 有没有办法在少于 O(n) 的时间内连接 Java 字符串? - Is there a way to concatenate Java strings in less than O(n) time? 如何计算小于n且总和大于n * 2的3个整数的组合数? - How to calculate the number of combinations of 3 integers less than n whose sum is greater than n * 2? 打印出素数小于给定数N - Print out prime Number less than a given number N
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM