Skip to content

Commit b09d359

Browse files
committedApr 3, 2025
Add solution #1093
1 parent b564da5 commit b09d359

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,131 LeetCode solutions in JavaScript
1+
# 1,132 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -873,6 +873,7 @@
873873
1090|[Largest Values From Labels](./solutions/1090-largest-values-from-labels.js)|Medium|
874874
1091|[Shortest Path in Binary Matrix](./solutions/1091-shortest-path-in-binary-matrix.js)|Medium|
875875
1092|[Shortest Common Supersequence](./solutions/1092-shortest-common-supersequence.js)|Hard|
876+
1093|[Statistics from a Large Sample](./solutions/1093-statistics-from-a-large-sample.js)|Medium|
876877
1103|[Distribute Candies to People](./solutions/1103-distribute-candies-to-people.js)|Easy|
877878
1108|[Defanging an IP Address](./solutions/1108-defanging-an-ip-address.js)|Easy|
878879
1122|[Relative Sort Array](./solutions/1122-relative-sort-array.js)|Easy|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* 1093. Statistics from a Large Sample
3+
* https://leetcode.com/problems/statistics-from-a-large-sample/
4+
* Difficulty: Medium
5+
*
6+
* You are given a large sample of integers in the range [0, 255]. Since the sample is so large,
7+
* it is represented by an array count where count[k] is the number of times that k appears in
8+
* the sample.
9+
*
10+
* Calculate the following statistics:
11+
* - minimum: The minimum element in the sample.
12+
* - maximum: The maximum element in the sample.
13+
* - mean: The average of the sample, calculated as the total sum of all elements divided by the
14+
* total number of elements.
15+
* - median:
16+
* - If the sample has an odd number of elements, then the median is the middle element once
17+
* the sample is sorted.
18+
* - If the sample has an even number of elements, then the median is the average of the two
19+
* middle elements once the sample is sorted.
20+
* - mode: The number that appears the most in the sample. It is guaranteed to be unique.
21+
*
22+
* Return the statistics of the sample as an array of floating-point numbers [minimum, maximum,
23+
* mean, median, mode]. Answers within 10-5 of the actual answer will be accepted.
24+
*/
25+
26+
/**
27+
* @param {number[]} count
28+
* @return {number[]}
29+
*/
30+
var sampleStats = function(count) {
31+
let minimum = 256;
32+
let maximum = -1;
33+
let sum = 0;
34+
let totalCount = 0;
35+
let mode = 0;
36+
let maxFrequency = 0;
37+
38+
for (let value = 0; value < 256; value++) {
39+
const frequency = count[value];
40+
if (frequency > 0) {
41+
minimum = Math.min(minimum, value);
42+
maximum = Math.max(maximum, value);
43+
sum += value * frequency;
44+
totalCount += frequency;
45+
if (frequency > maxFrequency) {
46+
maxFrequency = frequency;
47+
mode = value;
48+
}
49+
}
50+
}
51+
52+
const mean = sum / totalCount;
53+
const median = findMedian(count, totalCount);
54+
55+
return [minimum, maximum, mean, median, mode];
56+
};
57+
58+
function findMedian(count, totalCount) {
59+
const isOdd = totalCount % 2 === 1;
60+
const target = Math.floor(totalCount / 2);
61+
let currentCount = 0;
62+
let firstMedian = -1;
63+
64+
for (let value = 0; value < 256; value++) {
65+
currentCount += count[value];
66+
if (isOdd) {
67+
if (currentCount > target) return value;
68+
} else {
69+
if (firstMedian === -1 && currentCount >= target) {
70+
firstMedian = value;
71+
}
72+
if (currentCount > target) {
73+
return (firstMedian + value) / 2;
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)
Please sign in to comment.