Problems


July 13, 2022

Binary tree level order traversal

This is a classing BFS problem. We should traverse the whole tree with BFS, and store the values level by level to a list. Then combine each level to a list and return. This is very efficient....

Read

July 13, 2022

Number of 1 bits

We can logical and any number with 1 to check whether the last bit of that number is 1 or 0. We can also check this by checking the number is odd or even. Odd numbers always has last bit as 1. We...

Read

July 13, 2022

Product of array except self

We can go thorugh the whole list once and caculate and store the multiplicatio of prefix indeies in the result array directly. That way we don't use any extra memory. Then we do the same thing but in...

Read

July 13, 2022

Single number

We can iterate through the entire array and XOR everything. For starter we start our result to 0, as it has no effect on our XOR operation. If we XOR the same numbers, it turns into 0. So, every pair...

Read

July 13, 2022

Valid palindrome

We will take 2 pointers, at the beginning of the string and end of the string, then we compare both. If the character is not an aplhabet or number we move the pointer value by one. We also convert...

Read

July 13, 2022

Valid sudoku

We can use 3 hashmap to store the values of rows, colums and square. For rows and columns hashmap we will use the row and column number as key. For square hashmap, we can use the integer division of...

Read

July 12, 2022

Group anagrams

We are given a list of strings. We can iterate through each string, and sort it charactes, then use this as key in a hashmap, and the value of the hashmap will be a list. We will append the original...

Read

July 12, 2022

Rotting oranges

This is a BFS problem. We have to count the number of steps as minutes to return. If we can't traverse the whole grid, then we will return -1. Only tricky part is we can have multiple rotten oranges...

Read

July 12, 2022

Top k frequent element

We will count the frequency of each element and store it in a hashmap, where the number itself will be the key and frequency will be the value. Then we sort the elements of the hashmap and get the...

Read

July 12, 2022

Valid anagram

We can split both string to characters, sort and then compare each characters at every position. If we don't find any match, we return False. After comparing every character, we will return True....

Read
... 91 92 93 94 95