今天再来一个中等题,这题没有特殊的算法解法,直接根据题意解题就行啦
中等题:视频推荐算法
问题描述
西瓜视频正在开发一个新功能,旨在将访问量达到80百分位数以上的视频展示在首页的推荐列表中。实现一个程序,计算给定数据中的80百分位数。
例如:假设有一个包含从1到100的整数数组,80百分位数的值为80,因为按升序排列后,第80%位置的数字就是80。
99 百分位数:假如有 N 个数据,将数据从小到大排列,99 百分位数是第 N99%位置处的数据(遇到小数时四舍五入获取整数)。一般计算逻辑是先排序,定位到 N99%的位置。返回该位置处的数据。同理,80 百分位数就是第 N*80%位置处的数据。
测试样例
样例1:
输入:
data = "10,1,9,2,8,3,7,4,6,5"
输出:8
样例2:
输入:
data = "1,0,8,7,3,9,12,6,4,15,17,2,14,5,10,11,19,13,16,18"
输出:15
样例3:
输入:
data = "5,3,9,1,7"
输出:7
思路:
这个问题相对简单,只需要直接进行排序,将输入字符串按逗号分割并转换为整数数组就可以了
解题:
import java.util.Arrays;
public class Main {
public static int solution(String data) {
String[] dataArray = data.split(",");
int[] numbers = new int[dataArray.length];
for (int i = 0; i < dataArray.length; i++) {
numbers[i] = Integer.parseInt(dataArray[i]);
}
Arrays.sort(numbers);
int index = (int) Math.round(numbers.length * 0.8) - 1; // Adjust for 0-based index
index = Math.min(Math.max(index, 0), numbers.length - 1); // Ensure index is in bounds
return numbers[index];
}
public static void main(String[] args) {
System.out.println(solution("10,1,9,2,8,3,7,4,6,5") == 8);
System.out.println(solution("1,0,8,7,3,9,12,6,4,15,17,2,14,5,10,11,19,13,16,18") == 15);
System.out.println(solution("76,100,5,99,16,45,18,3,81,65,102,98,36,4,2,7,22,66,112,97,68,82,37,90,61,73,107,104,79,14,52,83,27,35,93,21,118,120,33,6,19,85,49,44,69,53,67,110,47,91,17,55,80,78,119,15,11,70,103,32,9,40,114,26,25,87,74,1,30,54,38,50,8,34,28,20,24,105,106,31,92,59,116,42,111,57,95,115,96,108,10,89,23,62,29,109,56,58,63,41,77,84,64,75,72,117,101,60,48,94,46,39,43,88,12,113,13,51,86,71") == 96);
}
}
具体步骤
-
解析输入数据:
- 使用
split方法将输入字符串按逗号分割成字符串数组。 - 将字符串数组转换为整数数组。
- 使用
-
排序数组:
- 使用
Arrays.sort方法对整数数组进行排序。
- 使用
-
计算80百分位数的位置:
- 计算
N * 0.8并四舍五入得到整数位置。 - 由于数组是0-based索引,所以需要将计算出的位置减1。
- 计算
-
获取80百分位数:
- 根据计算出的位置,直接访问数组中的元素。