🚀 Day 82 of #100DaysOfCode Today, I solved LeetCode 41 – First Missing Positive, a classic hard problem that tests in-place array manipulation and optimization. 💡 Problem Overview: Given an unsorted array, the goal is to find the smallest missing positive integer in O(n) time and O(1) space. 🧠 Approach: ✔️ Used index-based placement (cyclic sort idea) ✔️ Placed each number x at index x - 1 whenever possible ✔️ Ignored negative numbers and values out of range ✔️ Finally, scanned the array to find the first index where the value is incorrect This ensures optimal time and space complexity without using extra data structures. ⚡ Key Takeaways: In-place manipulation can eliminate the need for extra space Index mapping is a powerful trick for array problems Hard problems often rely on simple but clever observations 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) Solving challenging problems and strengthening core fundamentals 🚀 #LeetCode #100DaysOfCode #DSA #Arrays #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep #HardProblems
Solving LeetCode 41 – First Missing Positive in O(n) time and O(1) space
More Relevant Posts
-
•Solving Leetcode Problem | Day 9 • Problem: Peak Index in a Mountain Array • Approach: Used Binary Search to efficiently find the peak element in O(log n) time. Instead of checking both sides linearly, I compared arr[mid] with arr[mid + 1] to determine the direction: If arr[mid] > arr[mid+1] → we are in the decreasing part → move left (end = mid) Else → we are in the increasing part → move right (start = mid + 1) This way, the search space keeps shrinking until start == end, which directly gives the peak index. • Key Learning: Binary Search is not just for sorted arrays — it’s about identifying patterns and reducing search space smartly. • Time Complexity: O(log n) • Space Complexity: O(1) Consistency > Motivation. Showing up daily. #leetcode #dsa #binarysearch #javaprogramming #codingjourney #problemSolving #100daysofcode
To view or add a comment, sign in
-
-
🚀 Day 70 of #100DaysOfCode 📌 LeetCode Q3: 3Sum 💡 Problem: Find all unique triplets in the array which gives the sum of 0. 🧠 Approach I Used: - First, sorted the array - Fixed one element - Applied two-pointer technique for the remaining part - Skipped duplicates to avoid repeated triplets ⚡ Key Insight: Instead of brute force O(n³), using sorting + two pointers reduces it to O(n²) ❗ Edge Cases Handled: - Duplicate values - No valid triplets - Negative + positive mix ⏱ Complexity: - Time: O(n²) - Space: O(1) (excluding result) 🔥 Takeaway: Two-pointer + sorting = deadly combo for array problems 💯 💬 Open to feedback & better approaches! #Day70 #100DaysOfCode #LeetCode #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 92/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 347 – Top K Frequent Elements(Medium) 🧠 Approach: Count the frequency of each element using a hashmap, then sort the elements based on frequency in descending order and pick the top k. 💻 Solution: class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = {} for num in nums: freq[num] = freq.get(num, 0) + 1 sorted_items = sorted(freq.items(), key=lambda x: x[1], reverse=True) return [item[0] for item in sorted_items[:k]] ⏱ Time | Space: O(n log n) | O(n) 📌 Key Takeaway: Hashmaps combined with sorting provide a simple way to solve frequency-based problems, though heaps or bucket sort can optimize performance further. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 78 of #100DaysOfCode Today, I solved LeetCode 154 – Find Minimum in Rotated Sorted Array II, a problem that focuses on binary search and handling edge cases with duplicates. 💡 Problem Overview: Given a rotated sorted array that may contain duplicates, the goal is to find the minimum element efficiently. 🧠 Approach: ✔️ Applied modified binary search ✔️ Compared mid element with the right boundary ✔️ Carefully handled duplicate values to avoid incorrect elimination of search space This approach ensures correctness even when duplicates are present. ⚡ Key Takeaways: Binary search can be adapted for complex scenarios Duplicates introduce edge cases that must be handled carefully Choosing the correct condition is key to narrowing the search space 📊 Complexity Analysis: Time Complexity: O(log n) (average), O(n) (worst case due to duplicates) Space Complexity: O(1) Strengthening problem-solving with optimized approaches 🚀 #LeetCode #100DaysOfCode #DSA #BinarySearch #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
Day 97/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 456 – 132 Pattern(Medium) 🧠 Approach: Traverse the array from right to left while maintaining a stack. Use a variable to track the “middle” element (the ‘2’ in 132 pattern) and check if a valid pattern exists. 💻 Solution: class Solution: def find132pattern(self, nums: List[int]) -> bool: stack = [] third = float('-inf') for i in range(len(nums) - 1, -1, -1): if nums[i] < third: return True while stack and nums[i] > stack[-1]: third = stack.pop() stack.append(nums[i]) return False ⏱ Time | Space: O(n) | O(n) 📌 Key Takeaway: Using a monotonic stack while iterating backwards helps efficiently detect complex patterns in linear time. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 23/128 of my LeetCode journey 🚀 Solved Contains Duplicate II — a great problem that helped reinforce the sliding window technique and efficient use of data structures. 💡 Key takeaway: Instead of checking all pairs, I used a sliding window with a Set to track elements within a range of size k. This avoids unnecessary comparisons and keeps the solution efficient. 🔍 What I practiced: Sliding window technique Efficient use of Set Optimizing time complexity Every problem might not be complex, but each one strengthens the fundamentals — and that’s what builds strong problem-solving skills over time. 💪 #LeetCode #DSA #CodingJourney #128DaysOfCode #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 171 of My LeetCode Journey Problem 112: Path Sum 💡 Problem Insight: Today’s problem was about checking whether a binary tree has a root-to-leaf path whose sum equals a target value. The key detail here is root-to-leaf — not any path. Missing that leads to wrong answers. 🧠 Concept Highlight: The solution is a clean use of DFS (recursion): Subtract the current node’s value from the target Move to left and right subtrees When you reach a leaf, check if the remaining sum is zero This ensures you explore all valid paths without unnecessary work. 💪 Key Takeaway: Tree problems often reduce to accumulating state along a path. Instead of storing paths, update the condition as you traverse. ✨ Daily Reflection: This problem reinforced how recursion naturally fits tree traversal. Once you think in terms of paths and state, the solution becomes straightforward. #Day171 #LeetCode #BinaryTree #DFS #PathSum #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 12 of #LeetCode Top Interview 150 Just solved LeetCode 380 – Insert Delete GetRandom O(1) 💻 This problem really tested my understanding of data structures + optimization. The challenge was to design a system that supports insert, delete, and random access — all in constant time O(1). 🔑 Key Learnings: Combining HashMap + Dynamic Array (vector) for efficiency Smart use of index mapping to handle deletions in O(1) Understanding how to balance time vs space complexity Writing clean and optimized logic under constraints ⚡ Result: ✅ All test cases passed ⏱ Runtime: 32 ms (Beats 91.12%) 💾 Memory optimized Every day I’m getting better at breaking down problems and thinking in terms of optimal solutions, not just working ones. Consistency is slowly turning into confidence 💪 #Day12 #LeetCode #DSA #CodingJourney #100DaysOfCode #ProblemSolving #Cpp #TechGrowth #WomenInTech
To view or add a comment, sign in
-
-
🚀 Day 10/100 — LeetCode Challenge Solved 3Sum The brute force approach is simple: Try all triplets → O(n³) But that’s not scalable. 💡 Optimized Approach: 1) First, sort the array 2) Fix one element 3) Use two pointers to find the remaining pair 👉 This reduces the complexity significantly. Also handled duplicates carefully to avoid repeated triplets. 🧠 Time Complexity: O(n²) 💾 Space Complexity: O(1) (ignoring output) 💡 What I learned: Sometimes optimization is not about complex logic, but about: 1) Sorting 2) Using patterns like two pointers 3) Eliminating unnecessary work This problem felt like a step up from previous ones. Staying consistent. #LeetCode #DSA #100DaysOfCode #Cpp #TwoPointers #CodingJourney
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development