码迷,mamicode.com
首页 > 其他好文 > 详细

Compute Quantile

时间:2015-10-22 06:47:11      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.Arrays;
import java.util.Comparator;

public class Quantile {

  

Scanner sc = new Scanner(System.in);
String s = null;
while (sc.hasNext()) {
s = sc.nextLine();
System.out.println(s);
}



public static void main(String[] args) { // TODO Auto-generated method stub int[][] a = {{5, 2}, {7, 2}, {6, 2}}; int[][] b = {{2, 1000}, {4, 100}, {3, 1000}}; int[][] c = {{2, 1}, {3, 2}, {5, 3}, {6, 1}, {8, 1}, {11, 1}, {12, 1}, {15, 2}, {18, 2}, {19, 1}}; int[][] d = {{1, 1,}, {2, 3}, {3, 1}};
Quantile e
= new Quantile(); int[] ret = e.computeQuantiles(a, 3, 3); System.out.println(Arrays.toString(ret)); ret = e.computeQuantiles(d, 2, 3); System.out.println(Arrays.toString(ret)); ret = e.computeQuantiles(b, 3, 3); System.out.println(Arrays.toString(ret)); ret = e.computeQuantiles(c, 5, 10); System.out.println(Arrays.toString(ret)); } private int[] computeQuantiles(int[][] nums, int Q, int M) { Pair[] pairs = new Pair[M]; int[] quant = new int[Q - 1]; int N = 0; for (int i = 0; i < M; i++) { pairs[i] = new Pair(nums[i][0], nums[i][1]); N += nums[i][1]; } Arrays.sort(pairs, new Comparator<Pair>(){ @Override public int compare(Pair a, Pair b) { return a.val - b.val; } }); int quantIndex = 0; int numIndex = 0; for (int i = 0; i < Q - 1; i++) { quant[i] = (i + 1) * N / Q; } for (Pair pair : pairs) { numIndex += pair.count; while (quantIndex < Q - 1 && quant[quantIndex] <= numIndex) { quant[quantIndex] = pair.val; quantIndex++; } } return quant; } } class Pair { int val; int count; public Pair(int val, int count) { this.val = val; this.count = count; } }

 

Compute Quantile

标签:

原文地址:http://www.cnblogs.com/airwindow/p/4899730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!