Statistics from a Large Sample LeetCode solution in Java
1class Solution {2public double[] sampleStats(int[] count) {3double[] ans = new double[5];4ans[0] = -1;5ans[1] = -1;6int place = 0;7while (ans[0] == -1) {8if (count[place] > 0) ans[0] = place;9place++;10}11place = count.length - 1;12while (ans[1] == -1) {13if (count[place] > 0) ans[1] = place;14place--;15}16int countEl = count[0];17int max = count[0];18for (int i = 1; i < count.length; i++) {19countEl += count[i];20if (count[i] > max) {21max = count[i];22ans[4] = i;23}24}25for (int i = 0; i < count.length; i++) {26if (count[i] > 0) {27double tmp = count[i];28tmp /= countEl;29ans[2] += tmp * i;30}31}32place = 0;33int whereToStop = 0;34while (whereToStop < countEl / 2) {35whereToStop += count[place];36place++;37}38place--;39if (countEl % 2 == 1) {40if (whereToStop == countEl / 2) {41place++;42while (count[place] == 0) place++;43}4445ans[3] = place;46} else {47double tmp = place;48if (whereToStop == countEl / 2) {49place++;50while (count[place] == 0) place++;51}52tmp += place;53ans[3] = tmp / 2;54}55return ans;56}57}