LeetCode solutions

1183. Statistics from a Large Sample

My accepted Python solution to LeetCode problem 1183, Statistics from a Large Sample, running in 0ms.

  • Difficulty: Medium
  • Python
  • Runtime 0ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2026-01-02.

python
class Solution:
    def sampleStats(self, count: List[int]) -> List[float]:
        # Find minimum
        minimum = -1
        for i in range(256):
            if count[i] > 0:
                minimum = i
                break
        
        # Find maximum
        maximum = -1
        for i in range(255, -1, -1):
            if count[i] > 0:
                maximum = i
                break
        
        # Calculate mean and total count
        total_sum = 0
        total_count = 0
        for i in range(256):
            total_sum += i * count[i]
            total_count += count[i]
        mean = total_sum / total_count
        
        # Find mode (element with highest count)
        mode = 0
        max_count = 0
        for i in range(256):
            if count[i] > max_count:
                max_count = count[i]
                mode = i
        
        # Find median
        # Need to find the middle element(s)
        if total_count % 2 == 1:
            # Odd count - find middle element
            target = total_count // 2 + 1
            cumulative = 0
            for i in range(256):
                cumulative += count[i]
                if cumulative >= target:
                    median = i
                    break
        else:
            # Even count - find two middle elements
            target1 = total_count // 2
            target2 = total_count // 2 + 1
            median1 = median2 = -1
            cumulative = 0
            for i in range(256):
                cumulative += count[i]
                if median1 == -1 and cumulative >= target1:
                    median1 = i
                if median2 == -1 and cumulative >= target2:
                    median2 = i
                    break
            median = (median1 + median2) / 2
        
        return [float(minimum), float(maximum), mean, median, float(mode)]

Source