From 8c63db1e3cbb9a8b17e41236b6dc0f359015c2bb Mon Sep 17 00:00:00 2001 From: amrit-GH23 Date: Sun, 16 Mar 2025 12:02:14 +0530 Subject: [PATCH 1/4] added problem container-with-most-water and updated readme --- C++/container-with-most-water.cpp | 32 +++++++++++++++++++++++++++++++ README.md | 5 +++++ 2 files changed, 37 insertions(+) create mode 100644 C++/container-with-most-water.cpp diff --git a/C++/container-with-most-water.cpp b/C++/container-with-most-water.cpp new file mode 100644 index 00000000..3db55daa --- /dev/null +++ b/C++/container-with-most-water.cpp @@ -0,0 +1,32 @@ +// https://leetcode.com/problems/container-with-most-water +// code for the question : "container-with-most-water" +// language : cpp + + +class Solution { + public: + int maxArea(vector& height) { + int n = height.size(); + int max_area = 0; // Variable to store the maximum water that can be contained + int i = 0; // Left pointer + int j = n - 1; // Right pointer + + // Use two-pointer approach to find the maximum area + while (i < j) { + // Calculate the area between the two current pointers + int current_area = (j - i) * min(height[i], height[j]); + + // Update the maximum area found so far + max_area = max(max_area, current_area); + + // Move the pointer with the smaller height + if (height[i] < height[j]) { + i++; // Move left pointer to the right + } else { + j--; // Move right pointer to the left + } + } + return max_area; + } + }; + \ No newline at end of file diff --git a/README.md b/README.md index b2cac739..d7a97a92 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | +| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | +_O(n) | Medium | Two Pointer, Greedy| |
@@ -515,6 +517,9 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Amrit Kumar](https://github.com/amrit-GH23)
| India | C++ | [CodeChef] (https://www.codechef.com/users/amrit_kumar08)
[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/) + +
⬆️ Back to Top
From 5a20fd44936011fb42ca5c2fba1944a9b3e405ee Mon Sep 17 00:00:00 2001 From: amrit-GH23 Date: Sun, 16 Mar 2025 12:06:51 +0530 Subject: [PATCH 2/4] updated readme.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d7a97a92..742e3f0a 100644 --- a/README.md +++ b/README.md @@ -296,8 +296,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | -| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | -_O(n) | Medium | Two Pointer, Greedy| | +| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | _O(n) | Medium | Two Pointer, Greedy| |
@@ -517,8 +516,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) -| [Amrit Kumar](https://github.com/amrit-GH23)
| India | C++ | [CodeChef] (https://www.codechef.com/users/amrit_kumar08)
[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/) - +| [Amrit Kumar](https://github.com/amrit-GH23)
| India | C++ | [CodeChef](https://www.codechef.com/users/amrit_kumar08)
[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/)
⬆️ Back to Top From 4d6b8e48e11422affb72033472525511ad5d608f Mon Sep 17 00:00:00 2001 From: amrit-GH23 Date: Sun, 16 Mar 2025 12:09:31 +0530 Subject: [PATCH 3/4] corrected readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 742e3f0a..df9c1ae7 100644 --- a/README.md +++ b/README.md @@ -296,7 +296,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | -| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | _O(n) | Medium | Two Pointer, Greedy| | +| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer, Greedy| |
From e8f3953f2982c52135b41257bb0276c0e702108d Mon Sep 17 00:00:00 2001 From: amrit-GH23 Date: Tue, 18 Mar 2025 18:33:35 +0530 Subject: [PATCH 4/4] updated the readme.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index df9c1ae7..85accc7c 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [Python](./Python/pascals-triangle-ii.py) | _O(N^2)_ | _O(K)_ | Easy | | | | 1480 | [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Java](./Java/running-sum-of-1d-array.java) | _O(N)_ | _O(N)_ | Easy | Simple sum | | | 42 | [Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) | [Python](./Python/trapping_rain.py) | _O(N^2)_ | _O(N)_ | Hard | Array | | -| 11 | [Container with Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](./Python/container_with_most_water.py) | _O(N)_ | _O(N)_ | Medium | Array Two Pointers | | +| 11 | [Container with Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](./Python/container_with_most_water.py)
[C++](./C++/container-with-most-water.cpp) | _O(N)_ | _O(N)_ | Medium | Array Two Pointers | | | 1134 🔒 | [Armstrong Number](https://leetcode.com/problems/armstrong-number/) | [Java](./Java/Armstrong-Number.java) | _O(N)_ | _O(1)_ | Easy | | | | 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets/) | [Python](./Python/count-good-triplets.py) | _O(N^3)_ | _O(1)_ | Easy | | | | 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [Java](./Java/matrix-diagonal-sum.java) | _O(N)_ | _O(1)_ | Easy | | | @@ -296,7 +296,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | -| 011 | [Container With Most water](https://leetcode.com/problems/container-with-most-water/) | [C++](./C++/container-with-most-water.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer, Greedy| | +