🚀 Day 514 of #750DaysOfCode 🚀 Today’s problem: 1022. Sum of Root To Leaf Binary Numbers 🌳 This was a really nice combination of: Binary Trees DFS Traversal Bit Manipulation 🧠 Problem Insight Each root-to-leaf path in the binary tree represents a binary number. Example path: 1 → 0 → 1 Represents binary 101 = 5 Instead of storing binary as a string and converting later ❌ We can build the number directly while traversing: current = current * 2 + node.val This works because: Multiplying by 2 shifts bits left Adding node value appends the current bit Clean. Efficient. Interview-friendly. ✅ ⏱ Complexity Time: O(N) Space: O(H) (recursion stack) 💡 Key Takeaway When dealing with binary paths: 👉 Think in terms of bit shifting, not string building. Small optimization. Big impact in interviews. Consistency > Motivation. 514 days done. Still building. 💪 #LeetCode #Java #BinaryTree #DFS #BitManipulation #DataStructures #CodingJourney #Consistency
Binary Tree DFS Traversal and Bit Manipulation
More Relevant Posts
-
🚀 Day 523 of #750DaysOfCode 🚀 📌 Problem Solved: Check if Binary String Has at Most One Segment of Ones (LeetCode 1784) 🔎 Problem Summary: Given a binary string s (without leading zeros), we need to check whether the string contains at most one contiguous segment of 1s. If the 1s appear in more than one separate segment, we return false. (LeetCode) 💡 Key Insight: Since the string starts with 1, the only valid structure is: 111...000... If we ever encounter the pattern "01", it means a 1 appeared again after a 0, which creates multiple segments of ones. (AlgoMonster) ⚙️ Approach: Traverse the string or simply check if "01" exists. If "01" is found → more than one segment → return false. ⏱ Complexity: Time: O(n) Space: O(1) 📚 Takeaway: Sometimes string problems become much easier when we identify patterns instead of counting segments. Consistency > Motivation. On to Day 524 tomorrow. 💪 #LeetCode #Java #DataStructures #Algorithms #CodingChallenge #Consistency #ProblemSolving #100DaysOfCode #750DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 44 of #100DaysOfCode Solved 1011. Capacity To Ship Packages Within D Days on LeetCode 📦 🧠 Key Insight: We need to find the minimum ship capacity such that all packages can be shipped within D days. This is a classic case of Binary Search on Answer. ⚙️ Approach: 1️⃣ The minimum capacity = max(weight) (since one package must fit) 2️⃣ The maximum capacity = sum of all weights 3️⃣ Apply Binary Search between these bounds 4️⃣ For each capacity mid, simulate shipping: 🔹Keep adding weights until capacity exceeds 🔹Move to the next day when exceeded 5️⃣ If we can ship within D days → try smaller capacity 6️⃣ Else → increase capacity This helps us find the minimum valid capacity efficiently. ⏱️ Time Complexity: O(n log(sum)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
#Day67 of my second #100DaysOfCode Today’s problem pushed me to think carefully about comparisons and sorting logic. DSA • Solved Reverse Pairs (LeetCode 493) — count pairs (i, j) such that i < j and nums[i] > 2 * nums[j] • Brute force: check every pair using nested loops → O(n²) time • Optimal approach: Modified Merge Sort to count valid pairs during merge → O(2n log n) time, O(n) space • Key insight: count pairs across the left and right halves before merging. Another reminder of how divide-and-conquer + merge sort patterns appear in many hard problems. #DSA #Algorithms #LeetCode #MergeSort #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 12 of My LeetCode Journey Today I solved Rotate Image (LeetCode 48). Problem: Given an n × n matrix representing an image, rotate the matrix by 90 degrees clockwise. The challenge is to perform the rotation in-place, without using another matrix. Approach: I used a simple two-step trick: 1️⃣ Transpose the matrix (swap rows and columns) 2️⃣ Reverse each row This combination effectively rotates the matrix by 90° clockwise. Time Complexity: O(n²) Space Complexity: O(1) Problems like this improve understanding of matrix manipulation and in-place transformations. #leetcode #datastructures #algorithms #java #codinginterview #softwareengineering
To view or add a comment, sign in
-
-
🚀 Day 1/30: Kicking off my DSA Journey! Today’s focus: LeetCode 283 – Move Zeroes. 📝 The Challenge Given an integer array nums, the goal is to shift all the 0s to the end while keeping the non-zero elements strictly in their original order. ⚠️ The Catch: This must be done in-place (no extra memory allowed). 📌 Example Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, 0] Explanation: The zeros are pushed to the back, while 1, 3, 12 stay in their exact original sequence. 💡 First Thoughts The brute-force way out? Just create a secondary array, dump the non-zero numbers in first, and pad the rest with zeros. But that directly violates the $O(1)$ space constraint. The real puzzle was figuring out how to achieve this by only modifying the original array. ⚙️ The Solution: Two-Pointer Technique To optimize the solution, I utilized the Two-Pointer method: Pointer i acts as the explorer, traversing the array. Pointer j acts as an anchor, tracking where the next non-zero element belongs. The Steps: Start j at index 0. Loop through the array with i. Whenever i finds a non-zero number, swap it with nums[j] and bump j up by 1. 🧠 Why It Works Because j only steps forward when a non-zero element is securely locked into place, the original relative order is perfectly preserved. As the non-zeroes move to the front, the zeros naturally get pushed to the back. Zero extra data structures, just a single pass! 📊 Complexity Time Complexity: $O(n)$ — We only traverse the array once. Space Complexity: $O(1)$ — Everything is modified completely in-place. 🔑 Takeaways The Two-Pointer strategy is an absolute lifesaver for tricky array manipulations. "In-place modification" and "preserving relative order" are incredibly common technical interview constraints. Small problems are the best way to test and solidify your core fundamentals. Consistency beats motivation! 💪 Let's build these skills one problem at a time. ##30DaysOfCode #DSA #LeetCode #Java #ProblemSolving #InterviewPreparation #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode Solved 719. Find K-th Smallest Pair Distance on LeetCode 📏 🧠 Key Insight: We need to find the k-th smallest absolute difference among all possible pairs in the array. Brute force (generating all pairs) would be O(n²) → not efficient. Instead, we use Binary Search on Answer + Two Pointers. ⚙️ Approach: 1️⃣ Sort the array 2️⃣ Define search space: 🔹left = 0 (minimum distance) 🔹right = max(nums) - min(nums) 3️⃣ Apply Binary Search: 🔹For a given mid, count how many pairs have distance ≤ mid 4️⃣ Counting pairs efficiently: 🔹Use two pointers 🔹For each i, move j while nums[j] - nums[i] ≤ mid 🔹Add (j - i - 1) to count 5️⃣ If count ≥ k → try smaller distance 6️⃣ Else → increase distance 🎯 Final answer = smallest distance satisfying the condition ⏱️ Time Complexity: O(n log n + n log D) 🔹Sorting + Binary Search with linear check 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #TwoPointers #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟐𝟓 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on sorting an array containing only three distinct values using an efficient counting approach. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Sort Colors 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟏 – 𝐂𝐨𝐮𝐧𝐭𝐢𝐧𝐠 𝐀𝐫𝐫𝐚𝐲 • Created a count array of size 3 • Counted occurrences of 0, 1, and 2 • Overwrote the original array using the frequency values • Simple and easy to implement 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • When value range is small, counting is powerful • Rewriting the array can be simpler than swapping • Constraints often guide the optimal approach • Clear thinking avoids overcomplication 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) (since count array size is constant) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Not every sorting problem needs full sorting logic — sometimes counting is enough. 25 days consistent. On to Day 26 🚀 #DSA #Arrays #Sorting #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🌱 Day 11 of my #100DaysOfCode Journey Today I solved LeetCode Problem – Contains Duplicate. The problem asks us to determine if an integer array contains any duplicate values. If any value appears more than once, we return true; otherwise, false. The approach I used today was to sort the array first and then check adjacent elements for equality. This helps detect duplicates efficiently in a single pass. 🔹 What I practiced today: ✅ Array manipulation and sorting ✅ Comparing adjacent elements to find duplicates ✅ Thinking about time vs. space trade-offs 📊 Complexity Analysis: • Time Complexity: O(n log n) — due to sorting • Space Complexity: O(1) — in-place array check A simple yet practical problem that strengthens array handling and logical thinking in algorithm problems. #LeetCode #Java #DSA #100DaysOfCode #CodingJourney #ContainsDuplicate
To view or add a comment, sign in
-
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