Skip to main content

Posts

Leetcode 87: Scramble String

1. Problem Statement (Simple Explanation) We define a  scramble  operation on a string s: If |s| == 1: stop. If |s| > 1: Split s at some index i into two non-empty substrings: x = s[0..i-1], y = s[i..end]. Randomly choose to keep them in order (x + y) or swap them (y + x). Recursively apply the same process to x and y. Given two strings s1 and s2  of equal length , return true if s2 is a  scrambled string  of s1 under this definition, otherwise false. Constraints: 1 <= length(s1) <= 30 s1.length == s2.length Both consist of lowercase letters. 2. Examples Example 1: Input: s1 = "great", s2 = "rgeat" One valid scrambling sequence: "great" → split "gr" + "eat" No swap: "gr" + "eat" Recurse: "gr" → "g" + "r" → swap → "rg" Recurse: "eat" → "e" + "at" → "e" + "a" + "t" (no swaps) F...

EVacuate to Moon

  Problem Summary You have N cars and M power outlets. Car i has  capacity  A[i] Watt-hours (max it can store). Outlet j has  power  B[j] Watts. There are H hours available. Rules: Each outlet can be assigned to  at most one  car. Each car can be charged by  at most one  outlet. A used outlet charges its car for H hours. Energy given by outlet j: B[j] * H Watt-hours. Energy actually stored in car i if using outlet j: min(A[i], B[j] * H) (cannot exceed car’s capacity). Goal (per test case): Assign outlets to cars to  maximize the sum  of stored energy over all cars. Examples Explanation Sample: Input: 3 1 2 2 100 20 40 2 1 2 10 20 11 3 2 1 30 30 30 40 20 Output: 80 20 50 Test case 1: 1 car with capacity 100. 2 outlets: 20 W and 40 W. H = 2 hours. Possible energies: Using 20 W: min(100, 20*2) = min(100, 40) = 40 Using 40 W:...