Encode and Decode Strings
Prefix each string with its length and a delimiter (e.g., #).
During decoding, read the length, then take that many characters as the string after #.
Works for any characters — even spaces, punctuation, or # inside the text.
Top K Frequent Elements
Count frequency of each number
Group numbers by frequency
Extract the top k frequent ones from the highest frequency bucket
Longest common prefix
Assume the first string is the prefix.
Compare it with each next string using.
Keep shortening the prefix as needed.
Return the final prefix.
Minimum number of jumps to reach end
This is a Greedy problem.
You don’t need to explore all possible jumps.
The idea:
– Keep track of how far you can reach in the current jump (currentEnd)
– Keep track of the farthest you can reach overall (farthest)
– When you reach currentEnd, it means you must make another jump.
Products of Array Except Self
Loop through array and prepare right prefix product
Loop through array and prepare left prefix product
Product = Left Elements Product * Right Element Products
H-Index Problem
Sort the citations array in descending order (highest → lowest).
Initialize a variable h = 0.
For each iindex element, check if the current paper’s citations ≥ i + 1 (i+1 due to 0 index )
If yes → update h = i + 1
If not → stop the loop (because further papers will have fewer citations)
Return h as the H-index.
Gas Station
We use the Greedy Approach:
Keep track of:totalTank — total gas available minus total cost (to check if a solution exists)currentTank — current gas balance while traversingstart — potential starting index
If at any point currentTank < 0, it means we can’t reach the next station from the current start.
– So, we reset start = i + 1
– Reset currentTank = 0
If totalGas < 0 → return -1
Else → return start.
Candy
Approach — Two Pass Greedy
The trick:
A child’s candy count depends only on neighbors.
You can’t fix both sides in one pass — you must consider left neighbor and right neighbor separately.
So:
– Pass 1 (Left → Right):
If ratings[i] > ratings[i-1], give candies[i] = candies[i-1] + 1.
– Pass 2 (Right → Left):
If ratings[i] > ratings[i+1], give candies[i] = max(candies[i], candies[i+1] + 1)
Finally, sum all candies.
Trapping Rain Water
– Find left max array
– Find right max array
– Loop through height and sum
– sum = Min(leftMax[i], rightMax[i]) -height[i] if less than zero consider 0 contribution
Roman to Integer
We’ll map each Roman symbol to its integer value.
Then we iterate through the string:
– If the current symbol’s value is less than the next, subtract it.
– Otherwise, add it.
Length of Last Word
Start from the end of the string (since we only care about the last word).
Skip trailing spaces until you find the start of the last word.
Count characters until you hit another space or the beginning of the string.
Return the count.
Integer to Roman
Define a mapping between Roman symbols and their integer values — already done in your romanNum array.
[{key: M, val: 1000}]
Start from the largest value (1000) and move downward.
For each symbol:
– Check how many times it fits in the remaining number (Math.floor(remaining / curr)).
– Append that symbol that many times to the result string.
– Reduce remaining by using modulus (remaining % curr).
– Continue until remaining becomes 0.
Return the final Roman numeral string.
Text Justification
Initialize:out = [], line = [], lineLen = 0.
Iterate each word:
If lineLen + word.length + line.length > maxWidth → justify current line.
Else → push word into line, update lineLen += word.length.
Justify current line:totalSpaces = maxWidth - lineLengaps = line.length - 1
If one word → append all spaces after it.
Else → distribute spaces evenly, extra spaces go to left gaps.
Reset for next line:line = [], lineLen = 0, start with new word.
After loop:
Left-align last line → join with single space, pad right.
Return out.


