Forwarded from Maki Asrat
The is the interview question I just had:
Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.
Example 1:
Input: nums = [1,0,1,1,0]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.
The max number of consecutive ones is 4.
max_cont= 4
max_count= 3
max_count =4
[1,0,1,1,0]
Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.
The max number of consecutive ones is 4.
Constraints:
1 <= nums.length <= 105
nums[i] is either 0 or 1.
Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.
Example 1:
Input: nums = [1,0,1,1,0]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones.
The max number of consecutive ones is 4.
max_cont= 4
max_count= 3
max_count =4
[1,0,1,1,0]
Example 2:
Input: nums = [1,0,1,1,0,1]
Output: 4
Explanation:
- If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones.
- If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones.
The max number of consecutive ones is 4.
Constraints:
1 <= nums.length <= 105
nums[i] is either 0 or 1.
Mistakes are proof that you’re trying. Embrace every error as a lesson and keep moving forward
👍5
If you were interviewed, how was your technical interview?
Anonymous Poll
45%
I solved the question with little or no help
20%
I solved the quesiton with help
14%
I didnt solve
20%
just to see the result
👍3🤔1
🎄✨ Merry Christmas! 🎄✨
Wishing you all joy, love, and peace this holiday season.
As for the New Year… umm, is it appropriate to say "Happy New Year" now? 🤔
I mean, we’re rocking the Ethiopian calendar here! 🗓😎
Wishing you all joy, love, and peace this holiday season.
As for the New Year… umm, is it appropriate to say "Happy New Year" now? 🤔
I mean, we’re rocking the Ethiopian calendar here! 🗓😎
🎄10👎1
409. Longest Palindrome
Given a string s which consists of lowercase or uppercase letters, return the length of the longest
palindrome
that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
Submit
Given a string s which consists of lowercase or uppercase letters, return the length of the longest
palindrome
that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome.
Example 1:
Input: s = "abccccdd"
Output: 7
Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
Input: s = "a"
Output: 1
Explanation: The longest palindrome that can be built is "a", whose length is 1.
Constraints:
1 <= s.length <= 2000
s consists of lowercase and/or uppercase English letters only.
Submit
👍5
Leetcode with dani
409. Longest Palindrome Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome. …
can u solve it with out hash?and with only one loop and constant O(1) space?
check this ai it makes study easy https://app.youlearn.ai/learn/content/dc7773a6d5b3
YouLearn AI
Bootstrap
Bootstrap is a framework designed for creating responsive, mobile-first websites, utilizing a grid system and media queries to adapt styles for various devices. It offers ready-to-use components like navbars, tables, and alerts, enhancing development speed…
👍3🔥1
🚀 Daily LeetCode Challenge: Container With Most Water 🚀
Post Content
Hey everyone! 👋
Here’s today’s LeetCode problem to sharpen your problem-solving skills. Let’s dive in! 💻
Problem: Container With Most Water
Difficulty: Medium
Link: Container With Most Water - LeetCode
Problem Statement
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]). Find two lines that, together with the x-axis, form a container that holds the most water. Return the maximum amount of water the container can store.
Example
Input:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output:
49
Explanation:
The container is formed by the lines at indices 1 (height = 8) and 8 (height = 7).
The width of the container is 8 - 1 = 7.
The height of the container is min(8, 7) = 7.
The total area (water held) is width * height = 7 * 7 = 49.
Post Content
Hey everyone! 👋
Here’s today’s LeetCode problem to sharpen your problem-solving skills. Let’s dive in! 💻
Problem: Container With Most Water
Difficulty: Medium
Link: Container With Most Water - LeetCode
Problem Statement
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]). Find two lines that, together with the x-axis, form a container that holds the most water. Return the maximum amount of water the container can store.
Example
Input:
height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output:
49
Explanation:
The container is formed by the lines at indices 1 (height = 8) and 8 (height = 7).
The width of the container is 8 - 1 = 7.
The height of the container is min(8, 7) = 7.
The total area (water held) is width * height = 7 * 7 = 49.
👍6
🚀 Say Goodbye to Limited AI—Meet DeepSeek-V3! 🌍
Tired of ChatGPT’s limited conversations and dumb replies from other AI? I was too—until I found DeepSeek-V3!
try it
Tired of ChatGPT’s limited conversations and dumb replies from other AI? I was too—until I found DeepSeek-V3!
try it
💯7
Leetcode with dani
🚀 Daily LeetCode Challenge: Container With Most Water 🚀 Post Content Hey everyone! 👋 Here’s today’s LeetCode problem to sharpen your problem-solving skills. Let’s dive in! 💻 Problem: Container With Most Water Difficulty: Medium Link: Container With Most…
Approach
Brute Force: Use two nested loops to check every possible pair of lines.
Time Complexity: O(n²)
Space Complexity: O(1)
Optimized Approach: Use two pointers to traverse the array from both ends.
Time Complexity: O(n)
Space Complexity: O(1)
Solution Code
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
Brute Force: Use two nested loops to check every possible pair of lines.
Time Complexity: O(n²)
Space Complexity: O(1)
Optimized Approach: Use two pointers to traverse the array from both ends.
Time Complexity: O(n)
Space Complexity: O(1)
Solution Code
def 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
👍6👌2
DeepSeek R1, an open-source AI model has overtaken ChatGPT to become the #1 app on the US App Store.
🔥 Problem of the Day: "Maximum Subarray" (Medium)
LeetCode #53 | Topic: Dynamic Programming / Greedy
📝 Problem Statement
Given an integer array nums, find the contiguous subarray with the largest sum. Return the sum.
Example:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6 (Because [4,-1,2,1] sums to 6)
💡 Hints to Get Started
Should you track the current subarray or just its sum?
Pro Tip:Kadane’s Algorithm can solve this in O(n) time!
⏳ Time to Solve: 40 minutes!
Drop your solution in the comments 💬. I’ll post the optimized answer tomorrow
LeetCode #53 | Topic: Dynamic Programming / Greedy
📝 Problem Statement
Given an integer array nums, find the contiguous subarray with the largest sum. Return the sum.
Example:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6 (Because [4,-1,2,1] sums to 6)
💡 Hints to Get Started
Should you track the current subarray or just its sum?
Pro Tip:
⏳ Time to Solve: 40 minutes!
Drop your solution in the comments 💬. I’ll post the optimized answer tomorrow
👍6❤3
can u Write a Python function to check if a word is a palindrome?… but you can’t use loops or reversed().
Leetcode with dani
Problem of the Day: "Maximum Subarray" (Medium)
LeetCode #53 | Topic: Dynamic Programming / Greedy
📝 Problem Statement
Given an integer array nums, find the contiguous subarray with the largest sum. Return the sum.
Example:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6 (Because [4,-1,2,1] sums to 6)
💡 Hints to Get Started
Should you track the current subarray or just its sum?
Pro Tip:Kadane’s Algorithm can solve this in O(n) time!
⏳ Time to Solve: 40 minutes!
Drop your solution in the comments 💬. I’ll post the optimized answer tomorrow
LeetCode #53 | Topic: Dynamic Programming / Greedy
📝 Problem Statement
Given an integer array nums, find the contiguous subarray with the largest sum. Return the sum.
Example:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6 (Because [4,-1,2,1] sums to 6)
💡 Hints to Get Started
Should you track the current subarray or just its sum?
Pro Tip:
⏳ Time to Solve: 40 minutes!
Drop your solution in the comments 💬. I’ll post the optimized answer tomorrow
Solution: Kadane’s Algorithm
The most efficient way to solve this problem is using Kadane’s Algorithm, which runs in O(n) time and uses O(1) space. Here’s how it works:
Initialize two variables:
max_current: Tracks the maximum sum of the subarray ending at the current position.
max_global: Tracks the overall maximum sum found so far.
Iterate through the array:
For each element, update max_current to be the maximum of the current element itself or the sum of max_current and the current element.
Update max_global to be the maximum of max_global and max_current.
Return max_global as the result.
The most efficient way to solve this problem is using Kadane’s Algorithm, which runs in O(n) time and uses O(1) space. Here’s how it works:
Initialize two variables:
max_current: Tracks the maximum sum of the subarray ending at the current position.
max_global: Tracks the overall maximum sum found so far.
Iterate through the array:
For each element, update max_current to be the maximum of the current element itself or the sum of max_current and the current element.
Update max_global to be the maximum of max_global and max_current.
Return max_global as the result.
👍5⚡1
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_global
👍6
Leetcode with dani
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_global
If you solved it differently or have any questions, drop your approach or thoughts in the comments! Let’s learn and grow together. 💡
"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?
⚡5
Leetcode with dani
"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…
you can see the video below
YouTube
Can you solve the locker riddle? - Lisa Winer
View full lesson: http://ed.ted.com/lessons/can-you-solve-the-locker-riddle-lisa-winer
Your rich, eccentric uncle just passed away, and you and your 99 nasty relatives have been invited to the reading of his will. He wanted to leave all of his money to you…
Your rich, eccentric uncle just passed away, and you and your 99 nasty relatives have been invited to the reading of his will. He wanted to leave all of his money to you…