Problems


December 27, 2022

Next palindrome using same digits

We will find the next permutation that is greater than the first half of the palindrome, say greaterHalf. Then add it to the mid item if there is one and to reversed of greaterHalf. Time...

Read

December 27, 2022

First day where you have been in all the rooms

The idea is that we need to go back a lot of times. Define by dp[i] is number of days to reach cell i. We can only reach it from the cell i-1, so we need at least dp[i-1] steps. Then it will lead us...

Read

December 27, 2022

Perfect number

We will find all the divisors of the number and then sum them up. If the sum is equal to the number, then it is a perfect number. Time complexity: O(sqrt(n)) Space complexity:...

Read

December 27, 2022

Prefix and suffix search

We will generate all possible prefixes and suffixes for each word and store them in a hashmap. Then, we will iterate over the queries and return the maximum weight. Time complexity:...

Read

December 27, 2022

Synonymous sentences

We will create a graph where the nodes are the words and the edges are the synonyms. Then we will do a DFS to find all the possible sentences and add that to a hashset to remove duplicates. Finally,...

Read

December 26, 2022

Bitwise or of all subsequence sums

We will iterate over the array and update the prefix sum and bitwise or of all subsequence sums. Time complexity: O(n) Space complexity: O(1)

Read

December 26, 2022

Out of boundary paths

We will solve the problem recursively. We will use a memo to store the number of paths from (i, j) to (m, n). Time complexity: O(mn) Space complexity:...

Read

December 26, 2022

Shortest unsorted continuous subarray

We will sort the array and find the first and last index where the elements are different. Time complexity: O(nlog(n)) Space complexity: O(n)

Read

December 25, 2022

Longest subsequence with limited sum

We will sort the numbers and then create a prefix sum array. Then we will iterate over the numbers and find the index of the largest number that is less than or equal to limit - nums[i]....

Read

December 25, 2022

Minimum number of moves to make palindrome

We will iterate over the string and count the number of moves for each character. We will update the result. Time complexity: O(n^2) Space complexity: O(1)

Read
... 12 13 14 15 16 ...