Skip to main content

Posts

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:...

Leetcode 86: Partition List

  1. Problem Statement (Simple Explanation) You’re given: The head of a singly linked list. An integer x. You must  rearrange the list  so that: All nodes with value < x come  before  nodes with value >= x. Within each group (< x and >= x),  relative order  of nodes must be preserved (stable partition). Return the head of the new list. You must do this by re-linking nodes (no need to allocate new nodes, just moving pointers). 2. Examples Example 1: Input: head = [1,4,3,2,5,2], x = 3 Nodes < 3: 1,2,2 Nodes >= 3: 4,3,5 Preserving original order within each group: < 3 group in original order: 1,2,2 >= 3 group in original order: 4,3,5 Concatenate: 1 → 2 → 2 → 4 → 3 → 5 Output: [1,2,2,4,3,5] Example 2: Input: head = [2,1], x = 2 < 2 group: 1 >= 2 group: 2 Result: [1,2]. Output: [1,2] 3. Approach – Two Lists (Less ...