From 80c004eafd2a4c937f59f7a909edb70efc672ea7 Mon Sep 17 00:00:00 2001 From: Gourav Rusiya Date: Sat, 22 Apr 2023 17:49:04 +0530 Subject: [PATCH 1/4] Update README.md (#366) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1dcb41a5..b2cac739 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

Join Us on Telegram & Facebook

- + From 214fdad3962a4d895deb93a06c1bd425c22d682b Mon Sep 17 00:00:00 2001 From: Amrit <0902cs221009@rjit.ac.in> Date: Tue, 18 Mar 2025 18:49:42 +0530 Subject: [PATCH 2/4] added problem number 011 (container-with-most-water)(https://leetcode.com/problems/container-with-most-water/) (#501) * added problem container-with-most-water and updated readme --- C++/container-with-most-water.cpp | 32 +++++++++++++++++++++++++++++++ README.md | 5 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) 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..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 | | | @@ -297,6 +297,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 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 | | +
⬆️ Back to Top @@ -515,6 +516,8 @@ 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 e2deab360473da33d5ec892647ca2872f96e9e2a Mon Sep 17 00:00:00 2001 From: RAJPUT MIHIKA AJITSINGH <163915078+mihika632@users.noreply.github.com> Date: Sun, 5 Oct 2025 01:00:34 +0530 Subject: [PATCH 3/4] Add swapPairs function for linked list node swapping Implement a solution to swap adjacent nodes in a linked list using a dummy node for edge cases. --- Python/24_swapPairs.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Python/24_swapPairs.py diff --git a/Python/24_swapPairs.py b/Python/24_swapPairs.py new file mode 100644 index 00000000..58ad4724 --- /dev/null +++ b/Python/24_swapPairs.py @@ -0,0 +1,41 @@ +# Definition for singly-linked list. +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + +class Solution: + def swapPairs(self, head: ListNode) -> ListNode: + """ + Approach: + - Use a dummy node to handle edge cases (like swapping at head). + - Initialize 'prev' pointer at dummy. + - Traverse the list in pairs (first, second). + - Swap nodes by reassigning next pointers: + prev -> second -> first -> next_pair + - Move 'prev' two nodes ahead and continue. + + Intuition: + - We are not modifying node values, only connections. + - Swapping nodes in a linked list requires careful pointer manipulation. + + Time Complexity: O(n), n = number of nodes, as we traverse each node once. + Space Complexity: O(1), we only use a few pointers. + """ + dummy = ListNode(0) + dummy.next = head + prev = dummy + + while prev.next and prev.next.next: + first = prev.next + second = prev.next.next + + # Swapping + first.next = second.next + second.next = first + prev.next = second + + # Move prev pointer two nodes ahead + prev = first + + return dummy.next From 3d8dd884aeb384dd11cdda081229e30765ba938b Mon Sep 17 00:00:00 2001 From: RAJPUT MIHIKA AJITSINGH <163915078+mihika632@users.noreply.github.com> Date: Sun, 5 Oct 2025 01:02:04 +0530 Subject: [PATCH 4/4] Add divide function for two integers Implement division of two integers using bit manipulation and handle edge cases. --- Python/29_divideTwoIntegers.py | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Python/29_divideTwoIntegers.py diff --git a/Python/29_divideTwoIntegers.py b/Python/29_divideTwoIntegers.py new file mode 100644 index 00000000..d89e9b6f --- /dev/null +++ b/Python/29_divideTwoIntegers.py @@ -0,0 +1,47 @@ +class Solution: + def divide(self, dividend: int, divisor: int) -> int: + """ + Approach: + - Handle edge case: overflow when dividend = -2^31 and divisor = -1 + - Work with absolute values to simplify calculation. + - Use bit manipulation (left shifts) to speed up repeated subtraction: + - Keep doubling the divisor until it exceeds dividend. + - Subtract the largest multiple of divisor from dividend and accumulate quotient. + - Apply sign at the end based on the input signs. + + Intuition: + - Division is repeated subtraction. + - Using bit shifts allows us to subtract large chunks at once, reducing complexity from O(n) to O(log n)^2. + + Time Complexity: O(log(dividend)^2), because we double the divisor each time and subtract in a loop. + Space Complexity: O(1), only constant extra variables are used. + """ + # 32-bit integer limits + INT_MAX = 2**31 - 1 + INT_MIN = -2**31 + + # Edge case for overflow + if dividend == INT_MIN and divisor == -1: + return INT_MAX + + # Determine the sign of the result + negative = (dividend < 0) != (divisor < 0) + + # Work with absolute values + dividend, divisor = abs(dividend), abs(divisor) + quotient = 0 + + # Repeated subtraction using bit shifts + while dividend >= divisor: + temp_divisor, multiple = divisor, 1 + while dividend >= (temp_divisor << 1): + temp_divisor <<= 1 + multiple <<= 1 + dividend -= temp_divisor + quotient += multiple + + # Apply sign + if negative: + quotient = -quotient + + return quotient