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/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 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 diff --git a/README.md b/README.md index 1dcb41a5..85accc7c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

Join Us on Telegram & Facebook

- + @@ -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