Skip to main content

Posts

Changes remaining at Lemonade shop

  Problem Summary You run a lemonade shop where each lemonade costs 5. Customers arrive sequentially and each pays with a bill of 5, 10, or 20. If they pay with 5 → no change needed. If they pay with 10 → you must give back 5. If they pay with 20 → you must give back 15 using your current 5 and/or 10 bills. Initially, you have  no cash . You must decide if you can serve  all  customers in order while always giving correct change. Input: T test cases. For each test case: Integer n — number of customers. Array customerBills of size n with values in {5, 10, 20}. Output: For each test case print: true if you can give correct change to every customer. false otherwise. Constraints: 1≤T≤ 10 5 1≤n≤ 10 5 customerBills[i] ∈ {5, 10, 20} Sum of all n over test cases ≤  10 5 . Examples Explanation Sample 1: Input: 3 6 5 5 10 5 20 20...

Leetcode 90: Subsets II

  1. Problem Statement (Simple Explanation) You’re given an integer array nums that  may contain duplicates . You must return  all possible subsets  (the  power set ) of nums, with the constraint: The output  must not contain duplicate subsets . Subsets can be in any order. 1 <= nums.length <= 10, so maximum 2 10 = 1024 subsets. 2. Examples Example 1: Input: nums = [1,2,2] All subsets (unique): [] [1] [2] [1,2] [2,2] [1,2,2] Output: [[],[1],[1,2],[1,2,2],[2],[2,2]] Example 2: Input: nums = [0] Subsets: [] [0] Output: [[],[0]] 3. Approach – Backtracking with Duplicate Skipping This is similar to  Problem 78 (Subsets) , but nums can have duplicates, so we must  avoid generating duplicate subsets . Key Idea: Sort  the array nums. This groups duplicates together and makes it easier to skip them. Use  backtracking  to build subsets: Maintain: current subset (list). start index ...