We sampled integers between 0 and 255, and stored the results in an array count:  count[k] is the number of integers we sampled equal to k.
Return the minimum, maximum, mean, median, and mode of the sample respectively, as an array of floating point numbers.  The mode is guaranteed to be unique.
(Recall that the median of a sample is:

The middle element, if the elements of the sample were sorted and the number of elements is odd;
The average of the middle two elements, if the elements of the sample were sorted and the number of elements is even.)

 
Example 1:
Input: count = [0,1,3,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,3.00000,2.37500,2.50000,3.00000]
Example 2:
Input: count = [0,4,3,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
Output: [1.00000,4.00000,2.18182,2.00000,1.00000]

 
Constraints:

count.length == 256
1 <= sum(count) <= 10^9
The mode of the sample that count represents is unique.
Answers within 10^-5 of the true value will be accepted as correct.

Source: leetcode | Difficulty: MEDIUM
Tags: Data structures

Mathematics

Probability

Starter code: class Solution:
    def sampleStats(self, count: List[int]) -> List[float]:
        

Solutions:
class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		mn = -1
		mx = 0
		sm = 0
		cnt = 0
		mode_i = 0
		mode_cnt = 0
		indexes = collections.deque()
		median_count = 0
		for (i, c) in enumerate(count):
			sm += i * c
			cnt += c
			mode_i = i if mode_cnt < c else mode_i
			mode_cnt = c if mode_cnt < c else mode_cnt
			if c:
				mx = i
				if mn == -1:
					mn = i
				while indexes and median_count + count[indexes[0]] < cnt / 2:
					median_count += count[indexes[0]]
					indexes.popleft()
				indexes.append(i)
		median = 0
		if cnt % 2:
			median = indexes[0]
		elif count[indexes[0]] + median_count > cnt // 2:
			median = indexes[0]
		else:
			median = (indexes[0] + indexes[1]) / 2
		return (mn, mx, sm / cnt, median, mode_i)


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		N = sum(count)
		total = 0
		c = 0
		median = pre = post = -1
		mm = 256
		MM = -1
		mode = (-1, -1)
		for i in range(len(count)):
			total += count[i] * i
			c += count[i]
			if count[i]:
				mm = min(mm, i)
				MM = max(MM, i)
				mode = max(mode, (count[i], i))
			if median < 0:
				if c >= N / 2:
					if N % 2:
						pre = post = i
					elif c == N / 2:
						pre = i
					elif pre > 0:
						post = i
					else:
						pre = post = i
				if pre > 0 and post > 0:
					median = (pre + post) / 2
		mean = total / N
		return [mm, MM, mean, median, mode[1]]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		sosohang = 0
		sobenhat = 0
		solonnhat = 0
		tong = 0
		for i in range(0, len(count)):
			if count[i] != 0:
				sobenhat = i
				break
		for i in range(len(count) - 1, -1, -1):
			if count[i] != 0:
				solonnhat = i
				break
		mode = 0
		for i in range(0, len(count)):
			sosohang += count[i]
			if count[i] > count[mode]:
				mode = i
			tong += count[i] * i
		dem = 0
		print(sosohang)
		print(tong)
		mean = tong / sosohang
		if sosohang % 2 == 0:
			vitri1 = sosohang // 2
			vitri1_bool = False
			vitri2 = sosohang // 2 + 1
			vitri2_bool = False
			median = 0
			for i in range(0, len(count)):
				dem += count[i]
				if vitri2_bool == True and vitri1_bool == True:
					break
				if dem >= vitri1 and vitri1_bool == False:
					median += i
					vitri1_bool = True
				if dem >= vitri2 and vitri2_bool == False:
					median += i
					vitri2_bool = True
			return [float(sobenhat), float(solonnhat), float(mean), float(median / 2), float(mode)]
		else:
			median = 0
			for i in range(0, len(count)):
				dem += count[i]
				if dem >= sosohang // 2 + 1:
					median += i
					break
			return [float(sobenhat), float(solonnhat), float(mean), float(median), float(mode)]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		ans = []
		for i in range(0, len(count)):
			if count[i] != 0:
				ans.append(i)
				break
		for i in range(len(count) - 1, 0, -1):
			if count[i] != 0:
				ans.append(i)
				break
		s = 0
		for i in range(0, len(count)):
			s += i * count[i]
		total = sum(count)
		ans.append(s / total)
		total = sum(count)
		if total % 2 == 0:
			total = total // 2
			for i in range(0, len(count)):
				total = total - count[i]
				if total == 0:
					med = (i + i + 1) / 2
					ans.append(med)
					break
				if total < 0:
					med = i
					ans.append(med)
					break
		else:
			total = total // 2
			for i in range(0, len(count)):
				total = total - count[i]
				if total <= 0:
					med = i
					ans.append(med)
					break
		ans.append(max(range(len(count)), key=count.__getitem__))
		return ans


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		d = {}
		mini = None
		sample_size = total = 0
		mode = (-1, -1)
		for (n, freq) in enumerate(count):
			if freq:
				maxi = n
				if mini is None:
					mini = n
				sample_size += freq
				total += n * freq
				d[sample_size] = n
			if freq > mode[1]:
				mode = (n, freq)
		d = {k: v for (k, v) in d.items() if k >= sample_size / 2}
		median = d[min(d)]
		if sample_size % 2 == 0 and sample_size / 2 in d:
			del d[min(d)]
			median += d[min(d)]
			median /= 2
		return [mini, maxi, total / sample_size, median, mode[0]]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		minimum = len(count)
		maximum = 0
		sumValues = 0
		for (i, value) in enumerate(count):
			if value != 0:
				minimum = min(i, minimum)
				maximum = max(i, maximum)
				sumValues += i * value
			if value == max(count):
				mode = i
		mean = sumValues / sum(count)
		if len(count) % 2 == 0:
			lowMid = sum(count) // 2
			upMid = lowMid + 1
			currentCount = 0
			median = 0
			for (i, e) in enumerate(count):
				currentCount += e
				if currentCount >= lowMid and median == 0:
					median += i / 2
				if currentCount >= upMid:
					median += i / 2
					break
		else:
			mid = sum(count) // 2 + 1
			currentCount = 0
			median = 0
			for (i, e) in enumerate(count):
				currentCount += e
				if currentCount >= mid:
					median += i
					break
		return [minimum, maximum, mean, median, mode]


from bisect import bisect

class Solution:

	def sampleStats(self, counts: List[int]) -> List[float]:
		n = sum(counts)
		minimum = next((i for i in range(len(counts)) if counts[i]))
		maximum = next((i for i in range(len(counts) - 1, -1, -1) if counts[i]))
		mean = sum((i * counts[i] for i in range(1, 256))) / n
		mode = float(counts.index(max(counts)))
		for i in range(1, 256):
			counts[i] += counts[i - 1]
		median = 0.5 * (bisect(counts, n / 2) + bisect(counts, (n - 1) / 2))
		return [minimum, maximum, mean, median, mode]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		ans = [-1, -1, -1, -1, -1]
		for i in range(256):
			if count[i] > 0:
				ans[0] = i
				break
		for i in range(255, -1, -1):
			if count[i] > 0:
				ans[1] = i
				break
		(s, n) = (0, 0)
		for i in range(256):
			s += i * count[i]
			n += count[i]
		ans[2] = s / n
		if n % 2 == 0:
			acc = 0
			(m1, m2) = (-1, -1)
			for i in range(256):
				acc += count[i]
				if acc >= n // 2 and m1 == -1:
					m1 = i
				if acc >= n // 2 + 1 and m2 == -1:
					m2 = i
					break
			ans[3] = (m1 + m2) / 2
		else:
			acc = 0
			for i in range(256):
				acc += count[i]
				if acc >= n // 2 + 1:
					ans[3] = i
					break
		M = 0
		for i in range(256):
			if count[i] > M:
				M = count[i]
				ans[4] = i
		return ans


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		mini = -1
		maxi = 0.0
		mode = 0.0
		biggest = 0
		total = sum(count)
		mid = total // 2 if total != 1 else 1
		median = 0.0
		accumulator = 0
		tally = 0
		nums = 0
		for (i, num) in enumerate(count):
			if not num:
				continue
			else:
				maxi = float(i)
				tally += num * i
			if mini == -1 and num:
				mini = float(i)
			if num > biggest:
				biggest = num
				mode = float(i)
			if total % 2:
				if accumulator < mid and accumulator + num >= mid:
					median = float(i)
			elif accumulator < mid - 1 and accumulator + num >= mid - 1:
				median = float(i)
			elif num and accumulator == mid:
				median = (median + i) / 2
			accumulator += num
		return [mini, maxi, tally / accumulator, median, mode]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		Min = len(count)
		Max = 0
		Sum = 0
		Num = 0
		curr_count = count[0]
		for i in range(len(count)):
			if count[i] != 0:
				Min = min(i, Min)
				Max = max(i, Max)
				Sum += i * count[i]
				Num += count[i]
				Mean = Sum / Num
				if count[i] > curr_count:
					curr_count = count[i]
					Mod = i * 1.0
		total = Num // 2
		if Num % 2 == 0:
			for i in range(Min, Max + 1):
				total = total - count[i]
				if total == 0:
					Med = (2 * i + 1) / 2
					break
				if total < 0:
					Med = i
					break
		else:
			for i in range(Min, Max + 1):
				total = total - count[i]
				if total <= 0:
					Med = i
					break
		return [float(Min), float(Max), Mean, Med, Mod]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		tmp_ = [sum(count[:i + 1]) for i in range(256)]
		return [next((i for i in range(256) if count[i])) * 1.0, next((i for i in range(255, -1, -1) if count[i])) * 1.0, sum((i * v for (i, v) in enumerate(count))) / sum(count) * 1.0, (bisect.bisect(tmp_, (sum(count) - 1) / 2) + bisect.bisect(tmp_, sum(count) / 2)) / 2.0, float(count.index(max(count)))]


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		output = []
		output.append([i for (i, x) in enumerate(count) if x][0])
		output.append([i for (i, x) in enumerate(count) if x][-1])
		nums = sum(count)
		total = 0
		for (i, x) in enumerate(count):
			if x:
				total += i * x
		output.append(total / nums)
		if nums % 2 == 0:
			curr = 0
			first = (nums - 2) // 2
			second = first + 1
			print(first, second)
			(firstNum, secondNum) = (None, None)
			for (i, x) in enumerate(count):
				if x:
					curr += x
				if not firstNum and curr > first:
					firstNum = i
				if curr > second:
					secondNum = i
				if secondNum:
					output.append((firstNum + secondNum) / 2)
					break
		else:
			curr = 0
			for (i, x) in enumerate(count):
				if x:
					curr += x
				if curr >= (nums - 1) // 2:
					output.append(i)
					break
		output.append(count.index(max(count)))
		return output


class Solution:

	def sampleStats(self, count: List[int]) -> List[float]:
		n = sum(count)
		sm = 0
		curr = 0
		mx = 0
		mode = -1
		minim = -1
		maxim = -1
		(mid1, mid2) = (-1, -1)
		for i in range(256):
			curr += count[i]
			sm += i * count[i]
			if mid1 == -1 and curr >= n // 2:
				mid1 = i
			if mid2 == -1 and curr >= n // 2 + 1:
				mid2 = i
			if count[i] > mx:
				mode = i
				mx = count[i]
			if minim == -1 and count[i] != 0:
				minim = i
			if count[i] != 0:
				maxim = i
		mean = sm / n
		if n % 2 == 0:
			median = (mid1 + mid2) / 2
		else:
			median = mid1
		ans = [round(minim, 5), round(maxim, 5), round(mean, 5), round(median, 5), round(mode, 5)]
		return ans