Kraft Inequality Check
Kraft Check
Given a list of codeword lengths, determine whether a prefix-free code can exist.
Function
func KraftSatisfied(lengths []int) bool
Behavior
lengths[i] == 0means the symbol is unused and should be ignored.- Return
trueiffsum 2^{-lengths[i]} <= 1. - If
lengthsis empty, returntrue.
Constraints
- Each length is
0 <= l <= 30(fits in 64-bit integer arithmetic).
Notes
- Avoid floating-point error by using integer arithmetic.
Run tests to see results
No issues detected