From 7cf5ded6b4abe689f1072df33d15c6038f68440a Mon Sep 17 00:00:00 2001 From: Sudhir Date: Tue, 18 Nov 2025 01:21:07 +0530 Subject: [PATCH] Solution #11 #1929 #485 - Sudhir - 18th Nov - commit #1 --- .../containerWithMostWater - Explanation.md | 126 +++++++++++++++++ .../containerWithMostWater - Solution.java | 15 ++ .../concatenationOfArray - Explanation.md | 84 ++++++++++++ .../concatenationOfArray- Solution.java | 12 ++ .../maxConsecutiveOnes - Explanation.md | 128 ++++++++++++++++++ .../maxConsecutiveOnes - Solution.java | 14 ++ 6 files changed, 379 insertions(+) create mode 100644 Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md create mode 100644 Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java create mode 100644 Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md create mode 100644 Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java create mode 100644 Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md create mode 100644 Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java diff --git a/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md new file mode 100644 index 0000000..62d67e8 --- /dev/null +++ b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Explanation.md @@ -0,0 +1,126 @@ +# Problem Title: Container With Most Water + +**Difficulty:** Medium +**Category:** Two Pointers, Greedy +**Leetcode Link:** https://leetcode.com/problems/container-with-most-water/ + +--- + +## πŸ“ Introduction + +You're given an array `height[]` where each element represents the height of a vertical line on the x-axis. Picking any two lines forms a container that can trap water. The objective is to determine the **maximum possible water** that can be trapped. + +--- + +## πŸ’‘ Approach & Key Insights + +- Water trapped between two lines depends on: + - Width = (right - left) + - Height = minimum of the two heights +- To maximize area, we need a combination of good width and good height. +- Moving the pointer with the **smaller height** gives a chance to find a taller boundary, which could increase area. + +--- + +## πŸ› οΈ Breakdown of Approaches + +### 1️⃣ Brute Force / Naive Approach + +- **Explanation:** + - Check all possible pairs of lines `(i, j)`. + - Compute the area for each. + - Keep track of the highest. +- **Time Complexity:** O(nΒ²) +- **Space Complexity:** O(1) +- **Example/Dry Run:** + +Example: `height = [1, 8, 6]` +``` +Pairs: +(0,1): width=1, minHeight=1 β†’ area=1 +(0,2): width=2, minHeight=1 β†’ area=2 +(1,2): width=1, minHeight=6 β†’ area=6 +Max = 6 +``` + +--- + +### 2️⃣ Optimized Approach + +- **Explanation:** + - Use two pointers starting at both ends. + - Compute area. + - Move the pointer with the lower height inward. + - Continue until left meets right. +- **Time Complexity:** O(n) +- **Space Complexity:** O(1) +- **Example/Dry Run:** + +Example: `height = [1,8,6,2,5,4,8,3,7]` +``` +l=0 (1), r=8 (7) β†’ area=8 β†’ move l +l=1 (8), r=8 (7) β†’ area=49 β†’ move r +l=1 (8), r=7 (3) β†’ area=18 β†’ move r +l=1 (8), r=6 (8) β†’ area=40 β†’ move r +... +Max area = 49 +``` + +--- + +### 3️⃣ Best / Final Optimized Approach + +- The two-pointer solution is already optimal. +- No better time complexity exists for this problem. + +--- + +## πŸ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | --------------- | ---------------- | +| Brute Force | O(nΒ²) | O(1) | +| Optimized | O(n) | O(1) | +| Best Approach | O(n) | O(1) | + +--- + +## πŸ“‰ Optimization Ideas + +- You can't beat O(n), but you can: + - Improve pointer movement logic for readability. + - Add early exits if certain patterns are detected. + +--- + +## πŸ“Œ Example Walkthroughs & Dry Runs + +```plaintext +Example: +Input: [4,3,2,1,4] +- (0,4): width=4, height=4 β†’ area=16 +- Next comparisons produce smaller areas +Output: 16 +``` + +```plaintext +Example: +Input: [1,2,1] +- (0,2): width=2, height=1 β†’ area=2 +- (1,2): width=1, height=1 β†’ area=1 +Output: 2 +``` + +--- + +## πŸ”— Additional Resources + +- Two Pointer Techniques +- Greedy Strategy Explanations +- LeetCode Official Discuss Section + +--- + +Author: +Date: 18/11/2025 + diff --git a/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java new file mode 100644 index 0000000..97b6214 --- /dev/null +++ b/Arrays & Strings/#11 - Container with most water - Medium/containerWithMostWater - Solution.java @@ -0,0 +1,15 @@ +class Solution { + public int searchInsert(int[] nums, int target) { + int left = 0, right = nums.length - 1; + while (left <= right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) + return mid; + else if (nums[mid] < target) + left = mid + 1; + else + right = mid - 1; + } + return left; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md new file mode 100644 index 0000000..9b160bd --- /dev/null +++ b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray - Explanation.md @@ -0,0 +1,84 @@ +# Problem Title: Concatenation of Array + +**Difficulty:** Easy +**Category:** Array, Simulation +**LeetCode Link:** https://leetcode.com/problems/concatenation-of-array/ + +--- + +## πŸ“ Introduction +You're given an integer array `nums`. Your task is to create a new array `ans` of size `2 * n` where: + +``` +ans[i] = nums[i] +ans[i + n] = nums[i] +``` + +In short: **Just stick the array to itself.** Yup, it’s literally copy–paste. + +--- + +## πŸ’‘ Approach & Key Insights +- This is a straightforward construction problem. +- No tricks, no traps, no two pointers pretending to be deep. +- Just build a new array where the second half repeats the first. + +--- + +## πŸ› οΈ Breakdown of Approaches + +### 1️⃣ Brute Force (Almost too simple to be called brute force) +- Create a new array of size `2n`. +- First loop: fill `ans[0..n-1]` with `nums`. +- Second loop: fill `ans[n..2n-1]` with `nums` again. +- **Time Complexity:** O(n) +- **Space Complexity:** O(n) + +### 2️⃣ Optimized / Best Approach +- Just do: `ans = nums + nums` (in languages that allow it). +- Or use array copy utilities. +- Still **O(n)** time and space β€” but cleaner code. + +There’s no further optimization possible here… unless you bully the interviewer. + +--- + +## πŸ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +|--------------|----------------|------------------| +| Standard | O(n) | O(n) | +| Best Approach| O(n) | O(n) | + +--- + +## πŸ“‰ Example / Dry Run + +### Example 1: +``` +Input: nums = [1,2,1] + +ans: +Index: 0 1 2 3 4 5 +Value: 1 2 1 1 2 1 + +Output: [1,2,1,1,2,1] +``` + +### Example 2: +``` +Input: nums = [1,3,2,1] +Output: [1,3,2,1,1,3,2,1] +``` + +--- + +## πŸ”— Additional Notes +- This problem is typically used to warm up or check basic array manipulation. +- If this feels too easy β€” good. It was supposed to. + +--- + +Author: +Date: 18/11/2025 + diff --git a/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java new file mode 100644 index 0000000..bfdf574 --- /dev/null +++ b/Arrays & Strings/#1929 - Concatenation of Array - Easy/concatenationOfArray- Solution.java @@ -0,0 +1,12 @@ +class Solution { + public int[] getConcatenation(int[] nums) { + int n = nums.length; + int[] ans = new int[n * 2]; + for(int i = 0; i < n; i++){ + ans[i] = nums[i]; + ans[i + n] = nums[i]; + } + + return ans; + } +} \ No newline at end of file diff --git a/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md new file mode 100644 index 0000000..1c954ce --- /dev/null +++ b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Explanation.md @@ -0,0 +1,128 @@ +# Problem Title: Max Consecutive Ones + +**Difficulty:** Easy +**Category:** Arrays +**Leetcode Link:** https://leetcode.com/problems/max-consecutive-ones/ + +--- + +## πŸ“ Introduction + +You're given a binary array `nums` (only 0s and 1s). Your task is to find the **maximum number of consecutive 1s** in the array. + +Deceptively simple on the surface, but a perfect warm-up problem to build sliding window intuition. + +--- + +## πŸ’‘ Approach & Key Insights + +- We want the **longest streak** of consecutive 1s. +- Every time we hit a `0`, the streak ends. +- The simplest way: use a running counter for current streak and a max counter to store the best streak seen. + +This is extremely efficient and requires just one pass. + +--- + +## πŸ› οΈ Breakdown of Approaches + +### 1️⃣ Brute Force / Naive Approach + +- **Explanation:** + - Check each starting index. + - Count how many 1s follow it. + - Track the maximum. + - Works, but extremely inefficient. +- **Time Complexity:** O(nΒ²) +- **Space Complexity:** O(1) +- **Example/Dry Run:** + +Example: `[1,1,0,1]` +``` +Start at index 0 β†’ streak = 2 +Start at index 1 β†’ streak = 1 +Start at index 2 β†’ streak = 0 +Start at index 3 β†’ streak = 1 +Max = 2 +``` + +--- + +### 2️⃣ Optimized Approach + +- **Explanation:** + - Traverse the array once. + - Keep: + - `currentCount` β†’ current streak of 1s + - `maxCount` β†’ best streak found + - Reset `currentCount` to 0 whenever you encounter a 0. +- **Time Complexity:** O(n) +- **Space Complexity:** O(1) +- **Example/Dry Run:** + +Example: `[1,1,0,1,1,1]` +``` +1 β†’ current=1, max=1 +1 β†’ current=2, max=2 +0 β†’ current=0 +1 β†’ current=1, max=2 +1 β†’ current=2, max=2 +1 β†’ current=3, max=3 +Final answer = 3 +``` + +--- + +### 3️⃣ Best / Final Optimized Approach + +- There is no faster solution than O(n) β€” the optimized pass is already the final method. + +--- + +## πŸ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | --------------- | ---------------- | +| Brute Force | O(nΒ²) | O(1) | +| Optimized | O(n) | O(1) | +| Best Approach | O(n) | O(1) | + +--- + +## πŸ“‰ Optimization Ideas + +- If the array is extremely large and memory-mapped, streaming evaluation still works due to O(1) space. +- Useful trick: whenever you see a problem involving **consecutive streaks**, think linear scan + counters first. + +--- + +## πŸ“Œ Example Walkthroughs & Dry Runs + +```plaintext +Example: +Input: [0,1,1,1,0,1,1] +Process: +- Streaks: 3 and 2 +Output: 3 +``` + +```plaintext +Example: +Input: [1,1,1,1] +Process: +- Entire array is a streak of 4 +Output: 4 +``` + +--- + +## πŸ”— Additional Resources + +- Sliding Window Basics +- Counting Techniques in Arrays +- LeetCode Discuss Thread + +--- + +Author: +Date: 18/11/2025 \ No newline at end of file diff --git a/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java new file mode 100644 index 0000000..f4e0ac4 --- /dev/null +++ b/Arrays & Strings/#485 - Max Consecutive Ones - Easy/maxConsecutiveOnes - Solution.java @@ -0,0 +1,14 @@ +class Solution { + public int findMaxConsecutiveOnes(int[] nums) { + int max = 0; + int tempMax = 0; + for(int i = 0 ; i< nums.length; i++){ + if(nums[i] == 1) max++; + if(nums[i] == 0){ + tempMax = Math.max(tempMax, max); + max = 0; + } + } + return Math.max(tempMax, max); + } +} \ No newline at end of file