Problems


January 9, 2023

Maximal score after applying k operations

We will use a heap to keep track of the maximum score. We will pop the maximum score from the heap and apply the operations to the popped score. We will push the new scores to the heap. We will...

Read

January 8, 2023

Minimum health to beat game

Without considering the armor, we need 1 + total_damage amount of health to pass. Considering the armor, we just use it to shield the most severe damage, call it max_damage, and it should save us...

Read

January 8, 2023

Max points on a line

We will calculate the slope of each pair of points. If the slope is the same, we will increase the count. If the slope is different, we will update the maximum count. Time complexity:...

Read

January 8, 2023

Find xor beauty of array

First, note that by symmetry of i and j, we know that the value of ((nums[i] | nums[j]) & nums[k]) and ((nums[j] | nums[i]) & nums[k]) are equal. Which then implies that for a pair of (i, j)...

Read

January 7, 2023

Assign cookies

We will sort both child and cookies. Then we will try to satisfy the child with the smallest greed factor. If the cookie is not enough, we will move to the next cookie. If the cookie is enough, we...

Read

January 6, 2023

Maximum ice cream bars

We will sort the input costs array, then iterate the array, if the current cost is less than coins, we will update coins to be coins-costs[i], and...

Read

January 6, 2023

Minimum operations to make the array alternating

We count all the elements of odd and even positions separately and take most common 2 numbers from each group. If there is no common number, we will take the most common number from each group. Then...

Read

January 6, 2023

Most profit assigning work

We will zip together difficulty and profit as jobs and sort them by difficulty. Then we will sort workers. For each worker, we will find the maximum profit he can make under his ability. The maximum...

Read

January 6, 2023

Max increase to keep city skyline

For grid[i][j], it can't be higher than the maximun of its row nor the maximum of its column. So the maximum increasing height for a building at (i, j) is min(row[i], col[j]) -...

Read

January 5, 2023

Number of segments in a string

We will split the string by space, then count the number of non-empty string. Time complexity: O(n) Space complexity: O(n)

Read