e
Find Common Characters
Description:
Given a string array words
, return an array of all characters that show up in all strings within the words
(including duplicates). You may return the answer in any order.
Solution:
e
e
Maximize Sum Of Array After K Negations
Description: Given an integer array nums and an integer k, modify the array in the following way. Return
Solution:
e
e
e
e
e
Capacity To Ship Packages Within D Days ( not done)
Description
A conveyor belt has packages that must be shipped from one port to another within days
days.
The ith
package on the conveyor belt has a weight of weights[i]
. Each day, we load the ship with packages on the conveyor belt (in the order given by weights
). We may not load more weight than the maximum weight capacity of the ship.
Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days
days.
Solution
Binary Search + Greedy
low = max(weights) (smallest capacity to ship any package)
high = sum(weights) (ship everything in 1 day)
b. Greedy Feasibility Check For a given capacity mid (between low and high):
Try to ship packages using that capacity
Use a greedy approach:
Keep adding packages to the current day until capacity is exceeded
When it overflows, start a new day and continue
Count how many days you needed
If:
Days used ≤ days → it's a valid solution (try smaller capacity)
Days used > days → capacity too small (try larger)
c. Binary Search to Minimize Capacity Repeat the process to find the smallest capacity where it's still possible to ship within days.
Partition Array Into Three Parts With Equal Sum
Description:
Given an array of integers arr
, return true
if we can partition the array into three non-empty parts with equal sums.
Formally, we can partition the array if we can find indexes i + 1 < j
with (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])
Solution:
Using prefix sum
First we have to find if total sum divisible 3 (because we have to find 3 equal parts)
Second, we have to find first part which equal to target ( where target = total/3)
Then we keep find first 2 equal parts ( we do not need to check third one) while i+1 < j
If we found first 2 equal parts ⇒ return true
if not ⇒ return false;
Binary Prefix Divisible By 5
Description:
You are given a binary array nums
(0-indexed).
We define xi
as the number whose binary representation is the subarray nums[0..i]
(from most-significant-bit to least-significant-bit).
nums = [1,0,1]
, then x0 = 1
, x1 = 2
, and x2 = 5
.Return an array of booleans answer
where answer[i]
is true
if xi
is divisible by 5
.
Solution:
Using Brute-force
num = (num * 2 + nums[i]) % 5;
This means take the current number, shift it left by 1 bit, add the new incoming bit, and keep only its remainder modulo 5.
Because if we using sum ⇒ it might fail because the sum might be really big
Remove Outermost Parentheses
Description: A valid parentheses string is either empty "", "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.
Solution 1: use count variable to check:
empty
empty
empty
emptu