βš”οΈ
DSA Important Questions
  • πŸ˜‡README
  • πŸ”«Question List
  • πŸ§ͺFormula List
  • πŸ—’οΈ01 Array
    • βœ…Rotate Matrix
    • βœ…Max Sum Contiguous Subarray
    • Find Duplicate in Array
    • βœ…Merge Intervals
    • βœ…Spiral Order Matrix I
    • Repeat and Missing Number Array
    • βœ…Merge Overlapping Intervals
    • βœ…Set Matrix Zeros
    • βœ…Spiral Order Matrix II
    • Largest Number
    • First Missing Integer
    • βœ…Pascal Triangle
    • ⭐Max Distance
    • βœ…Wavy Array
    • Next Permutation
    • βœ…Min Steps in Infinite Grid
    • Flip
    • ⭐Find Permutation
    • ⭐Maximum Absolute Difference
    • ⭐Maximum Unsorted Subarray
    • Reorder Data in Log File
    • βœ…Make equal elements Array
  • βž•02 Math
    • βœ…Excel Column Number
    • ⭐Excel Column Title
    • ⭐Grid Unique Paths
    • ⭐Power Of Two Integers
    • βœ…Next Similar Number
    • ⭐k-th Permutation
  • πŸ”03 Binary Search
    • ⭐Median of Array
    • ⭐Square Root of Integer
    • ⭐Rotated Sorted Array Search
    • ⭐Matrix Median
    • Capacity To Ship Packages Within B Days
  • 🧡04 String
    • Implement StrStr
    • ⭐Integer to Roman
    • ⭐Roman to Integer
    • Length of Last Word
    • ⭐atoi
    • ⭐Valid IP Addresses
    • ⭐Compare Version Numbers
    • ⭐Longest Palindromic Substring
    • Count And Say
    • Reverse the String
    • Power of 2
    • 🚧KMP: Minimum Characters Required to Make a String Palindromic
    • ⭐Convert to Palindrome
    • Bulls and Cows
  • 1️⃣05 Bit Manipulation
    • Reverse Bits
    • Single Number
    • ⭐Divide Integers
    • ⭐Single Number II
    • ⭐Count Total Set Bits
    • ⭐Palindromic Binary Representation
  • ✌️06 Two Pointers
    • Merge Two Sorted Lists II
    • ⭐3 Sum
    • Remove Duplicates from sorted Array
    • ⭐Container With Most Water
    • Remove Element from Array
    • ⭐Max Continuous Series of 1s
    • Pair With Given Difference
    • ⭐Maximum Ones After Modification
  • πŸ”—07 Linked List
    • Swap List Nodes in Pairs
    • Rotate List
    • ⭐Reorder List
    • Merge Two Sorted Lists
    • Remove Duplicates from Sorted List
    • Add Two Numbers as Lists
    • Remove Nth Node from List End
    • ⭐List Cycle
    • Intersection of Linked Lists
    • ⭐Reverse Linked List II
    • Palindrome List
    • ⭐K reverse linked list
    • ⭐Reverse Alternate K Nodes
    • ⭐Kth Node From Middle
    • ⭐Sort Binary Linked List
    • ⭐Even Reverse
  • πŸ“š08 Stacks and Queues
    • ⭐Rain Water Trapping
    • Generate all Parentheses
    • ⭐Largest Rectangle in Histogram
    • ⭐Min Stack
    • Redundant Braces
    • Nearest Smaller Element
    • ⭐First non-repeating character in a stream of characters
    • βœ…Balanced Parantheses!
  • πŸ”™09 Backtracking
    • ⭐Kth Permutation Sequence
    • ⭐Combination Sum
    • Combination Sum II
    • ⭐NQueens
    • Combinations
    • ⭐Subsets II
    • Subset
    • Palindrome Partitioning
  • πŸ’±10 Hashing
    • ⭐Longest Consecutive Sequence
    • ⭐4 Sum
    • ⭐Anagrams
    • ⭐Points on the Straight Line
    • 2 Sum
    • ⭐Valid Sudoku
    • Copy List
    • Longest Substring Without Repeat
  • πŸ—»11 Heaps & Maps
    • Merge K Sorted Lists
    • ⭐LRU Cache
    • ⭐Inversions
    • Distinct Numbers in Window
  • 🌳12 Tree Data Structure
    • βœ…Inorder Traversal
    • ⭐Recover Binary Search Tree
    • ⭐Inorder Traversal of Cartesian Tree
    • ⭐Least Common Ancestor
    • ⭐Construct Binary Tree From Inorder And Preorder
    • Flatten Binary Tree to Linked List
    • Valid Binary Search Tree
    • βœ…Preorder Traversal
    • ⭐Binary Tree From Inorder And Postorder
    • Balanced Binary Tree
    • βœ…Sorted Array To Balanced BST
    • Symmetric Binary Tree
    • βœ…Postorder Traversal
    • ⭐Populate Next Right Pointers Tree
    • Identical Binary Trees
    • BST Iterator
    • ZigZag Level Order Traversal BT
    • Path Sum
    • Next Pointer Binary Tree
    • Min Depth of Binary Tree
    • Root to Leaf Paths With Sum
    • ⭐Kth Smallest Element in Tree
    • ⭐2-Sum Binary Tree
    • Vertical Order traversal of Binary Tree
    • ⭐Diagonal Traversal
    • Cousins in Binary Tree
    • Path to Given Node
    • Remove Half Nodes
    • Merge two Binary Tree
    • ⭐Burn a Tree
    • Nodes at Distance K
    • Vertical Sum of a Binary Tree
    • Covered / Uncovered Nodes
  • 13 Dynamic Programming
    • Ugly Number II
    • Coin Change
    • 0 - 1 Knapsack Problem
    • Permutation Coefficient
  • 14 Greedy Algorithm
    • ⭐Gas Station
    • ⭐Majority Element
    • ⭐Distribute Candy
    • ⭐Highest Product
    • Assign Mice to Holes
    • ⭐Meeting rooms
  • πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦15 Graph
    • Clone Graph
    • Word Search Board
    • ⭐Stepping Numbers
    • Black Shapes
    • Knight On Chess Board
    • ❌Smallest Multiple With 0 and 1
    • ⭐Commutable Islands
    • Possibility of finishing all courses given pre-requisites
    • ❌Valid Path
    • Cycle in Directed Graph
    • ❌Cycle in Undirected Graph
Powered by GitBook
On this page
  • Brute Force: Not Accepted
  • Using two poiners
  1. 06 Two Pointers

Container With Most Water

PreviousRemove Duplicates from sorted ArrayNextRemove Element from Array

Last updated 3 years ago

Thoughts: 1st Attempt

15th June 2022

  • 0:00 I've never seen this question before, and the question has a tag β€œtrick”. It doesn't sound like one of those question I should spend more than 10 minutes on.

  • 11:00 I am able to do it on paper. But I'm not able to code it. This is not right. I'll give it a few more minutes.

  • 24:00 OK, I was able to code what I did on paper.

  • 25:00 I'll see the solution now.

  • 33:00 Watching this YouTube Video.

Brute Force: Not Accepted

int Solution::maxArea(vector<int> &A) {
    int res = INT_MIN;
    if(A.size() <= 1)
        return 0;
    for(int size = 1; size <= A.size(); size++) 
        for(int start = 0; start < A.size() - size; start++) {
            int height = INT_MAX;
            for(int i = start; i <= start + size; i++)
                height = min(height, A[i]);
            res = max(res, height * size);
        }
    return res;
}

Using two poiners

int Solution::maxArea(vector<int> &A) {
    if(A.size() <= 1)
        return 0;
    long long res = 0;
    long long p1 = 0, p2 = A.size() - 1;
    while(p1 < p2) {
        res = max(res, (p2 - p1) * min((long long)A[p1], (long long)A[p2]));
        if(A[p1] < A[p2])
            p1++;
        else
            p2--;
    }
    
    return res;
}

Time Complexity: O(n)O(n)O(n)​

Space Complexity: O(1)O(1)O(1)​

✌️
⭐
Container With Most Water - InterviewBitInterviewBit
Logo