Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-324

Huffman Maximum Codeword Length

Problem

Build a Huffman code from a list of symbol frequencies using a min-heap keyed by frequency and then insertion order, and return the longest codeword length. Return 0 when fewer than two symbols are present.

Hint

Repeatedly merge the two lightest subtrees and track how deep each original symbol ends up.

Test cases

case 1
in: [[5,2,1]]
expected: 2
case 2
in: [[1,1,1,1]]
expected: 2
case 3
in: [[8]]
expected: 0
case 4
in: [[]]
expected: 0
case 5
in: [[3,3,3,1,1]]
expected: 3
Starter codePython
import heapq
def huffman_max_length(freqs):
    # Your code here
    pass

5 test cases · run your code against them in the editor