Problems


August 11, 2022

Partition equal subset sum

This is basically a 0-1 knapsack problem, for each element, either we can take the element or leave it. First, we will check, if the sum of the numbers is not even, we immediately return false. Then...

Read

August 10, 2022

Word break

We can solve the problem using BFS. We can model the problem as graph, every index can be considered as vertex and every edge is a completed word. Then the problem just boiled down if the path...

Read

August 10, 2022

Convert sorted array to binary search tree

If there is no element in the sorted list, then we return null. This logic we will use as our base case. Then we will select the middle of the sorted list as our root node. Then from there, all the...

Read

August 10, 2022

Maximum product subarray

We will keep track of minimum and maximum products of each item and keep the current maximum is our result. When the whole traversal is done, we will return the maximum result. Time Complexity:...

Read

August 10, 2022

Partition labels

We will first count the last position of all chacracter in a hashmap. Then we will iterate through the string, check if the last index of the chacracter is equal to the current index, if yes, then we...

Read

August 10, 2022

Decode ways

We could decode a string with 1 character with just 1 way, but if we have a 2 digit string, then we can decode it 2 ways. For example, 12 can be decoded 1->A, b->2 or...

Read

August 9, 2022

Binary trees with factors

We will convert the array to array set, so that we can do the lookup in constant time. Then we will check, if a candidate can divide the root without any remainder and root//candidate is available in...

Read

August 9, 2022

Valid parenthesis string

We will keep track of number of left parenthesis and number of right parenthesis in the string. For *, we could count it as left parenthesis or right parenthesis or we could ignore it....

Read

August 9, 2022

Hand of straights

We will first check if the number of card is divisible by the group size, if not, we immediately return false. Then we count the number of cards, and create a min heap with all the distinct cards....

Read

August 9, 2022

Longest palindromic substring

We will create a helper function to count the number of palindrome in a string starting from a left and right pointers. For odd number, both will be same index, for even left and right pointers will...

Read
... 76 77 78 79 80 ...