Flash memory write erasures

Write codeMedium20 min

Your task

A NOR flash page holds one word, and programming that word can only clear bits: a bit that already reads 0 can never be driven back to 1 without erasing the page, and an erase drives every bit of the word back to 1. The page starts erased. writes lists the values the firmware wants stored, in order, and word_bits gives the width of the word. Before each write the controller erases the page if and only if the wanted value cannot be reached from the stored value by clearing bits alone. Rewriting the value already stored costs nothing. Return how many erases the whole sequence costs. Every value is non-negative and fits inside word_bits bits.

Examples

flash_erase_cycles(writes: list[int], word_bits: int) -> int
Example 1
Inputflash_erase_cycles([12, 8, 15], 4)
Output1
Example 2
Inputflash_erase_cycles([7, 7, 7], 4)
Output0
Example 3
Inputflash_erase_cycles([0, 1], 4)
Output1