Problems
November 25, 2022
Find greatest common divisor of array
We can solve the problem by using Euclidean algorithm. We will find the greatest common divisor of the first two numbers in the array. Then we will find the greatest common divisor of the greatest...
ReadNovember 25, 2022
Sum of subarray minimums
We can solve it by using stack. We can iterate over the array, and for each element, we can check if the stack is empty or the top of the stack is greater than the current element. If the stack is...
ReadNovember 25, 2022
Swapping nodes in a linked list
We will iterate over the whole list to find the length of the list. Then we get the kth node as the first node and the (length-k+1)th node as the second node. We will swap the values of the two...
ReadNovember 25, 2022
Add two numbers II
We can solve the problem by using stack. We will push the values of the two linked lists into two stacks. Then we will pop the values from the two stacks and add them together. If the sum is greater...
ReadNovember 24, 2022
Complement of base 10 integer
What is the relationship between input and output? input + output = 111....11 in binary format. Is there any corner case? 0 is a corner case expecting 1, output > input. We can solve it by using bit...
ReadNovember 24, 2022
The employee that worked on the longest task
We will iterate over the logs, and for each log, we will update the start time and end time of the employee. We will also update the longest task time of the employee. After iterating over the logs,...
ReadNovember 24, 2022
Stone game VII
We can see dynamic programming structure in this problem: each time we take some stones from the left or from the left side, what is rest is always contigious array. So, let us denote by dp(i, j) the...
ReadNovember 24, 2022
Minimum number of arrows to burst balloons
We can solve it by using greedy algorithm. We can sort the balloons by the end time. We can iterate over the balloons, and for each balloon, we can check if the start time of the balloon is greater...
ReadNovember 23, 2022
Power of two
We can solve the problem iteratively. If the number is even, we can divide it by 2. If the number is odd, we can return false. We can use bit manipulation to check if the number is odd. Time...
ReadNovember 23, 2022
Final prices with a special discount in a shop
We will iterate over the prices array. For each price, we will iterate over the prices array again to find the first price that is less than or equal to the current price. If we find the price, we...
Read