1 242
code with mosh join this channel if u want to get free mosh courses
01 فبراير 2025, 02:47
01 فبراير 2025, ~03:45
Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts
إظهار المزيد"Can You Solve the 100 Locker Riddle?" Your rich, eccentric uncle has left behind a mysterious will. You and 99 relatives are called to his mansion for the reading. Here’s the twist: There are 100 lockers, each hiding a secret word. Each of you is assigned a number from 1 to 100. The rules are: Heir 1 opens every locker. Heir 2 closes every 2nd locker. Heir 3 changes the state of every 3rd locker (opens if closed, closes if open). This continues until Heir 100 changes the state of the 100th locker. At the end, only the lockers that remain open will reveal the code to the safe. Here’s the challenge: Without going through all 100 steps, can you figure out which lockers will stay open?
def maxSubArray(nums):
max_current = max_global = nums[0]
for i in range(1, len(nums)):
max_current = max(nums[i], max_current + nums[i])
max_global = max(max_global, max_current)
return max_globaldef maxArea(height):
left, right = 0, len(height) - 1
max_area = 0
while left < right:
width = right - left
current_height = min(height[left], height[right])
current_area = width * current_height
max_area = max(max_area, current_area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
# Example usage:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
print(maxArea(height)) # Output: 49
Explanation
We use two pointers, left and right, to traverse the array from both ends.
The area between the two lines is calculated as width * height, where width = right - left and height = min(height[left], height[right]).
To maximize the area, we move the pointer pointing to the shorter line inward.
This approach ensures we only traverse the array once, making it efficient with a time complexity of O(n).
Additional Resource
LeetCode Container With Most Water Discussiondef maxArea(height):
left, right = 0, len(height) - 1
max_area = 0
while left < right:
width = right - left
current_height = min(height[left], height[right])
current_area = width * current_height
max_area = max(max_area, current_area)
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
# Example usage:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
print(maxArea(height)) # Output: 49
Explanation
We use two pointers, left and right, to traverse the array from both ends.
The area between the two lines is calculated as width * height, where width = right - left and height = min(height[left], height[right]).
To maximize the area, we move the pointer pointing to the shorter line inward.
This approach ensures we only traverse the array once, making it efficient with a time complexity of O(n).
Additional Resource
LeetCode Container With Most Water Discussion