Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions C++/container-with-most-water.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& 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;
}
};

41 changes: 41 additions & 0 deletions Python/24_swapPairs.py
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions Python/29_divideTwoIntegers.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<h2>Join Us on Telegram & Facebook</h2>
<a href="https://t.me/joinchat/K5RKXyxYoW5iYzU1">
<a href="https://t.me/codecks">
<img align="left" height=100 src="https://media.giphy.com/media/wlR4kWTnwEyY8RwHKM/giphy.gif">
</a>
<a href="https://www.facebook.com/groups/codedecks">
Expand Down Expand Up @@ -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) <br> [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 | | |
Expand Down Expand Up @@ -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 | |


<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down Expand Up @@ -515,6 +516,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
| [Amrit Kumar](https://github.com/amrit-GH23) <br> <img src="https://avatars.githubusercontent.com/u/146119347?s=400&u=599ba9f51c94e08861dc01580db03fadde609ad8&v=4" width="100" height="100"> | India | C++ | [CodeChef](https://www.codechef.com/users/amrit_kumar08)<br/>[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/)

<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down