Day 24/100 🚀 | #100DaysOfDSA Solved Array Partition (LC 561) This one looked extremely straightforward — sort the array and pair adjacent elements. But the real insight was understanding why sorting guarantees the maximum possible sum. Problems I Faced: • Initially overthought it — tried to reason about greedy pairing without sorting. • Took a moment to understand why pairing smallest with next smallest maximizes the overall minimum sum. • Resisted the urge to complicate it with extra logic. Final Approach: • Sorted the array. • Iterated in steps of 2. • Added every alternate element (minimum of each pair after sorting). • Kept the solution clean and simple. Key Insight: After sorting, pairing adjacent elements ensures the smaller value in each pair is as large as possible — maximizing the total sum. Time Complexity: O(n log n) Space Complexity: O(1) (ignoring sorting overhead) Not every problem needs complexity. Sometimes clarity is the real skill. Consistency continues. 💪 #100DaysOfCode #LeetCode #DSA #Java #Arrays #Greedy #ProblemSolving
Array Partition Problem Solved with Sorting and Greedy Approach
More Relevant Posts
-
🚀 Day 11 of #60DaysOfCode Solved a challenging tree problem today: 👉 Largest BST in a Binary Tree 🌳 Problem Statement: Given a binary tree, find the size of the largest subtree that satisfies Binary Search Tree (BST) properties. 💡 Approach: Used a postorder traversal (bottom-up approach) to efficiently determine whether each subtree is a BST. For every node, tracked: - Minimum value - Maximum value - Size of subtree - Whether it's a valid BST If a subtree satisfies BST conditions, we compute its size. Otherwise, we carry forward the maximum size found so far. ⚡ Key Insight: Instead of checking each subtree separately (which is inefficient), we combine results during traversal — achieving O(N) time complexity. ✅ Successfully passed all test cases! 🔍 This problem really strengthened my understanding of: - Tree Traversals - Recursion - Optimized problem-solving #DataStructures #Java #CodingJourney #BinaryTree #BST #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 Day 82 of my LeetCode Journey 🔥 📘 Problem: 110. Balanced Binary Tree 🎯 Difficulty: Easy 🔹 Problem Statement: Given a binary tree, determine if it is height-balanced. A binary tree is balanced if the height difference between the left and right subtree of every node is not more than 1. 🔹 Approach Used: Traverse the tree using Depth First Search (DFS) Calculate the height of left and right subtrees recursively If the height difference exceeds 1, return -1 to indicate the tree is not balanced Otherwise, return the height of the current node Finally, check if the returned value is -1 or not 🔹 Key Concepts: Binary Tree traversal Depth First Search (DFS) Recursion Height calculation of tree 🔹 Learning: Instead of calculating heights separately for every node (which would increase complexity), this approach combines height calculation and balance checking in a single traversal, making the solution more efficient. Time Complexity: O(n) Space Complexity: O(h) (recursion stack) #LeetCode #Day82 #Java #BinaryTree #DFS #Recursion #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 33 of #100DaysOfCode Solved 25. Reverse Nodes in K-Group on LeetCode 🔁 🧠 Key Idea: Reverse nodes of a linked list k at a time, while keeping the remaining nodes unchanged if they are fewer than k. ⚙️ Approach: 🔹Traverse the list to check if k nodes exist. 🔹If fewer than k nodes remain → return the list as it is. 🔹Reverse the current k-group using a helper reverse function. 🔹Recursively apply the same logic to the remaining list. 🔹Connect the reversed group with the result of the recursive call. This approach makes the solution clean and modular by separating the logic of: 🔹Finding k nodes 🔹Reversing a segment 🔹Connecting segments ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n/k) (due to recursion stack) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 37 — LeetCode Practice 🚀 Today’s focus was on strengthening linked list fundamentals and mastering pointer manipulation. ✅ Problem solved today: 🔹 Merge Two Sorted Lists 💡 Key learnings from today: • Strengthened understanding of the two-pointer technique in linked lists • Learned how using a dummy node simplifies head handling • Practiced merging nodes without creating a new list • Improved clarity on pointer movement and reference updates • Handled edge cases like one list being null Initially, I was carefully thinking about how to manage the head of the merged list. Then I realized that using a dummy node removes unnecessary conditional checks and makes the logic much cleaner. That small structural decision made the implementation simpler and more readable — achieving O(n + m) time complexity with O(1) extra space. This problem reminded me: Sometimes clean structure matters more than complex logic. Better pointer control. Cleaner implementation. Stronger linked list foundation. 💪 On to Day 38 🚀 #Day37 #DSA #LeetCode #ProblemSolving #Java #LinkedList #CodingJourney #Consistency #FutureEngineer
To view or add a comment, sign in
-
-
🚀 Day 20 of #100DaysOfCode Solved 54. Spiral Matrix on LeetCode 🌀 🧠 Key insight: Spiral traversal is all about maintaining boundaries. By shrinking the top, bottom, left, and right limits after each pass, we can cover every element exactly once. ⚙️ Approach: 🔹Maintain four pointers: top, down, left, right 🔹Traverse: 🔹Left → Right (top row) 🔹Top → Bottom (right column) 🔹Right → Left (bottom row) 🔹Bottom → Top (left column) 🔹Update boundaries after each traversal ⏱️ Time Complexity: O(m × n) 📦 Space Complexity: O(1) (excluding output list) #100DaysOfCode #LeetCode #DSA #Matrix #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 36 of #100DaysOfLeetCode ✅ Solved: Plus One (LeetCode 66) Difficulty: Easy Status: Accepted (114/114 Testcases Passed) Runtime: 0 ms 💯 Today’s problem looked simple but reinforced an important concept — handling carry in arrays. 🧠 Problem Summary: We are given a large integer represented as an array of digits. We need to increment the number by one and return the updated array. 🔎 Key Insight: Start from the last digit (right to left): If digit < 9 → increment and return. If digit == 9 → set it to 0 and carry over. If all digits are 9 → create a new array with an extra digit at the beginning. 💡 Example: Input: [9,9,9] Output: [1,0,0,0] 🎯 What I Practiced Today: Reverse traversal of arrays Carry-forward logic Edge case handling (all 9s case) Writing optimized O(n) solution Even easy problems strengthen fundamentals when you focus on edge cases. Consistency > Complexity. 🔥 #Day36 #100DaysOfCode #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 46/100 🚀 | #100DaysOfDSA Solved LeetCode 12 – Integer to Roman today. This problem is the reverse of Roman to Integer and focuses on handling both standard values and subtractive combinations like 4 (IV), 9 (IX), 40 (XL), etc. Approach: • Created two arrays: one for integer values and one for their Roman representations. • Included subtractive values (900, 400, 90, 40, 9, 4) directly in the arrays. • Iterated from largest to smallest value. • Repeatedly subtracted the value while appending the corresponding Roman numeral. Time Complexity: O(1) Space Complexity: O(1) Key takeaway: Handling special cases upfront makes the conversion logic clean and avoids complex conditional checks. Another day, another concept reinforced. Consistency over intensity. 💪 #LeetCode #DSA #Java #Strings #ProblemSolving #Consistency #100DaysOfDSA
To view or add a comment, sign in
-
-
🚀 Day 73/100 of my DSA Journey ✅ Problem solved today: LeetCode 1461 – Check If a String Contains All Binary Codes of Size K 🧠 What I focused on: Using a HashSet to store all substrings of length k Sliding window technique to generate substrings efficiently Understanding why we only need to check if the number of unique substrings equals 2^k Handling edge cases when string length is smaller than k This problem nicely combined strings + sliding window + hashing logic. 📌 Key takeaway: Sometimes the solution is just counting unique patterns efficiently. 📈 Day 73 done. Consistency continues. #LeetCode #DSA #SlidingWindow #Strings #Hashing #Java #ProblemSolving #Consistency #LearningInPublic #100DaysOfCode
To view or add a comment, sign in
-
Day 117 🚀 | LeetCode Progress Solved 3 problems today and strengthened array manipulation skills 💪 📌 Problem 1: Sort Array By Parity (#905) – 🟢 Easy 👉 Two-pointer / index swapping technique 👉 Place even numbers at the front while iterating 👉 In-place solution with O(1) extra space 👉 Time Complexity: O(n) Efficient way to partition arrays without extra memory ⚡ 📌 Problem 2: Find All Numbers Disappeared in an Array (#448) – 🟢 Easy 👉 Index marking technique using negative values 👉 Mark visited indices by flipping the sign 👉 Positive indices at the end represent missing numbers 👉 Time: O(n) | Space: O(1) (excluding result list) A clever trick to use the array itself as a hash map 🧠 📌 Problem 3: Minimum Changes to Make an Alternating Binary String (#1758) – 🟢 Easy 👉 Compare with two possible patterns: "010101..." and "101010..." 👉 Count mismatches for both patterns 👉 Return the minimum operations needed 👉 Time Complexity: O(n) Simple logic but great practice for pattern-based string problems 🔍 Small improvements every day lead to big progress over time 📈 #Day117 #LeetCode #DSA #Java #ProblemSolving #CodingJourney #GeekStreak #100DaysOfCod
To view or add a comment, sign in
-
🚀 Day 15/100 – Intersection of Two Arrays. Today I solved “Intersection of Two Arrays” on LeetCode. 🔹 Problem Statement: Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique, and you can return the result in any order. 🔹 Approach (Using HashSet): Store all elements of nums1 in a HashSet (to remove duplicates). Traverse nums2 and check if the element exists in the first set. If yes → add it to another set (to keep the result unique). Convert the result set into an array and return it. 🔹 Why This Works: HashSet gives O(1) average lookup time. Automatically removes duplicates. Efficient and clean solution. 🔹 Time Complexity: O(n + m) (Where n = length of nums1, m = length of nums2) 🔹 Space Complexity: O(n) (for storing elements in sets). 💡 What I Learned Today: How to use HashSet for fast searching. How to remove duplicates efficiently. How to convert a Set into an array. Day 15 done — consistency maintained! 🚀 #Day15 #100DaysOfCode #LeetCode #Java #DSA #Consistency
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