..

Leetcode Daily 1

Hello World.

Hi. This is my very first post to somewhat self-hosted blog about programming and stuff. I don’t have time and knowledge to write about something really useful, but I thought that it would be nice to save my thoughts about something here.

I guess I’ll show how I solved Leetcode Daily today

link to Daily: click

func count_b(i int) int {
    bits := 0
    for i > 0 {
        bits += i & 1
        i = i >> 1
    }
    return bits
}

func countBits(n int) []int {
   ret := make([]int, n+1)
   for i := 0; i < n + 1; i++ {
       ret[i] = count_b(i)
   } 
   return ret 
}

Pretty easy stuff. You go with every number I step by step, moving its binary representation to the right. If last bit equals 1 == we increment bits.

But… I don’t know. This is O(n log n) solution. I’ve seen in Editorial some other DP-like ideas about how to solve it (with memoization), but I can’t understand them yet.

Maybe later.