Implement solutions for stock trading problems with varying constraints.
Part 1: Single Transaction
function maxProfit(prices: number[]): number
Example: prices = [7,1,5,3,6,4] → Output: 5 Buy on day 2 (price = 1) and sell on day 5 (price = 6)
Part 2: Two Transactions
function maxProfitTwo(prices: number[]): number
Example: prices = [3,3,5,0,0,3,1,4] → Output: 6 Buy at 0, sell at 3, then buy at 1, sell at 4
Part 3: K Transactions
function maxProfitK(k: number, prices: number[]): number
Example: k = 2, prices = [2,4,1] → Output: 2
Implement browser tab navigation with back and forward functionality.
Class Methods:
class BrowserTabs {
visit(url: string): void
back(steps: number): string
forward(steps: number): string
}
Example: visit("google.com") visit("facebook.com") visit("youtube.com") back(1) → "facebook.com" forward(1) → "youtube.com"
Implementation:
Design a system to manage food ratings and queries.
Requirements:
Example Usage:
rateFood("Pizza", 5)
rateFood("Burger", 3)
getHighestRated() → ["Pizza"]
getAverageRating() → 4.0
Implementation:
Given a range [1-n] and a string of numbers, find the missing number.
function findMissing(n: number, str: string): number
Examples:
Input: n = 5, str = "1234"
Output: 5
Input: n = 13, str = "1310923..."
Output: Missing number in range [1-13]
Find highest visible building looking right for each building.
function findVisibleBuildings(heights: number[]): number[]
Requirements:
- Taller buildings block view of shorter ones
- Return -1 if no taller building visible
- Consider line of sight mechanics
Find valid dictionary words from phone number combinations.
function letterCombinations(digits: string, dict: Set<string>): string[]
Similar to LeetCode 17 but:
- Only return combinations in dictionary
- Handle invalid combinations
- Optimize dictionary lookups
Find length of longest increasing path in matrix.
function longestIncreasingPath(matrix: number[][]): number
Constraints:
- 1 <= m,n <= 200
- 0 <= matrix[i][j] <= 2^31 - 1
- Only move up/down/left/right
- No diagonal movement
- No boundary wrapping
Example:
Input: [
[9,9,4],
[6,6,8],
[2,1,1]
]
Output: 4 (path: 1→2→6→9)
Select k engineers to maximize team performance.
function maxPerformance(
n: number,
speed: number[],
efficiency: number[],
k: number
): number
Performance = sum(speeds) * min(efficiencies)
Example:
Input:
n = 6
speed = [2,10,3,1,5,8]
efficiency = [5,4,3,9,7,2]
k = 2
Output: 60 (engineers 2 and 5)
Constraints:
- 1 <= k <= n <= 10^5
- 1 <= speed[i] <= 10^5
- 1 <= efficiency[i] <= 10^8
Find mine with largest explosion radius impact.
interface Mine {
x: number;
y: number;
radius: number;
}
function findMostDestructive(mines: Mine[]): Mine
Example:
Input: [
{x: -3, y: 3, radius: 3},
{x: -1, y: 2, radius: 3},
...
]
Output: {x: -2, y: -2, radius: 3.5}
Count connected land cells in binary grid.
function numIslands(grid: string[][]): number
Example:
Input: [
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]
Output: 3
Constraints:
- 1 <= m,n <= 300
- grid[i][j] is '0' or '1'
Find max unique candy flavors after sharing k consecutive candies.
function maxUniqueFlavors(
candies: number[],
k: number
): number
Example:
Input: candies = [1,2,2,3,4,3], k = 3
Output: 3
Constraints:
- 1 <= candies.length <= 10^5
- 1 <= candies[i] <= 10^5
- 0 <= k <= candies.length
Calculate how many times a sentence fits on screen.
function wordsTyping(
sentence: string[],
rows: number,
cols: number
): number
Requirements:
- Words can't be split across lines
- Skip to next line if word doesn't fit
- Optimize for large screens
Generate n x n matrix with numbers 1 to n^2 in spiral order.
function generateMatrix(n: number): number[][]
Example:
Input: n = 3
Output: [
[1,2,3],
[8,9,4],
[7,6,5]
]
Constraints:
- 1 <= n <= 20
Find maximum loot without robbing adjacent houses.
function rob(nums: number[]): number
Example:
Input: [5,5,10,100,10,5]
Output: 110
Explanation: Rob 5 + 100 + 5 = 110
Constraints:
- Can't rob adjacent houses
- Maximize total amount
- Handle various array sizes
Validate partially filled 9x9 Sudoku board.
function isValidSudoku(board: string[][]): boolean
Rules:
1. Each row: digits 1-9 without repetition
2. Each column: digits 1-9 without repetition
3. Each 3x3 sub-box: digits 1-9 without repetition
Note: Only validate filled cells
Find if word exists in grid, allowing cell revisits.
function exist(
board: string[][],
word: string
): boolean
Requirements:
- Adjacent cells only (horizontal/vertical)
- Can reuse same cell
- Return true if word exists
- Handle all edge cases