📅 Day 88 of #100DaysOfCode Problem: Binary Tree Maximum Path Sum (LeetCode 124) Approach (DFS + Greedy): 1️⃣ Used recursion to compute the maximum gain from left and right subtrees. 2️⃣ Ignored negative contributions using max(0, subtreeSum) — since negative paths reduce the total. 3️⃣ At each node, calculated the current path sum as: left + right + root->val 4️⃣ Updated a global maxSum to track the best path seen so far. 5️⃣ Returned only one side (max(left, right) + root->val) to maintain a valid upward path. #100DaysOfCode #LeetCode #DSA #BinaryTree #DFS #Recursion #DynamicProgramming #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
Binary Tree Maximum Path Sum LeetCode Solution
More Relevant Posts
-
📅 Day 95 of #100DaysOfCode Problem: Path Sum (LeetCode 112) Approach (Recursion / DFS): 1️⃣ Traversed the binary tree using Depth First Search (DFS). 2️⃣ Subtracted the current node value from the target sum. 3️⃣ When reaching a leaf node, checked if remaining sum equals node value. 4️⃣ Recursively checked both left and right subtrees. 5️⃣ Returned true if any root-to-leaf path matched the target sum. #100DaysOfCode #LeetCode #DSA #BinaryTree #DFS #Recursion #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
LeetCode Daily – #1784 Check if Binary String Has at Most One Segment of Ones [Easy] The Insight: Since the string always starts with '1' (no leading zeros), the only way a second segment of ones can appear is if a '0' comes BEFORE another '1' — i.e., the substring "01" exists in the string. So the entire problem reduces to one check: → Does "01" appear in the string? If yes → false. If no → true. Time: O(N) | Space: O(1) This is one of those problems where stripping away the noise leads to an almost trivially elegant solution. The key was recognizing the constraint — no leading zeros — which guarantees the string always opens with a '1', making "01" the only possible signal of a broken segment. Sometimes the best code is the code you don't write. 🚀 #LeetCode #CodingChallenge #CPlusPlus #ProblemSolving #Programming #SoftwareEngineering #POTD #DSA
To view or add a comment, sign in
-
-
📅 Day 93 of #100DaysOfCode Problem: Palindrome Partitioning (LeetCode 131) Approach (Backtracking + Recursion): 1️⃣ Used backtracking to explore all possible substring partitions. 2️⃣ At each step, checked if the current substring is a palindrome. 3️⃣ If it is a palindrome → added it to the current path. 4️⃣ Recursively continued for the remaining substring. 5️⃣ Once we reached the end of the string → stored the valid partition. 6️⃣ Backtracked to explore other possible partitions. #100DaysOfCode #LeetCode #DSA #Backtracking #Recursion #Strings #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
📅 Day 97 of #100DaysOfCode Problem: Is Graph Bipartite? (LeetCode 785) Approach (BFS + Graph Coloring): 1️⃣ Assigned colors to nodes while traversing the graph using BFS. 2️⃣ Started coloring an unvisited node with color 0. 3️⃣ Colored all adjacent nodes with the opposite color. 4️⃣ If two connected nodes had the same color → graph is not bipartite. 5️⃣ Repeated for all disconnected components. #100DaysOfCode #LeetCode #DSA #Graphs #BFS #GraphTheory #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
📅 Day 98 of #100DaysOfCode Problem: Delete and Earn (LeetCode 740) Approach (Dynamic Programming — House Robber Pattern): 1️⃣ Converted the array into a points array where each index stores total value earned from that number. 2️⃣ Choosing a number deletes its adjacent values (num - 1 and num + 1). 3️⃣ Recognized the pattern similar to the House Robber problem. 4️⃣ At each step, decided whether to take or skip the current value. 5️⃣ Used dynamic programming to maximize total earned points. Many DP problems become easier once you recognize familiar patterns 🧠 #100DaysOfCode #LeetCode #DSA #DynamicProgramming #DP #Arrays #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
🚀 Day 182 of #200DaysOfCoding 📌 Problem: Check if Binary String Has at Most One Segment of Ones (LeetCode 1784) Today’s challenge was about checking whether a binary string contains at most one continuous segment of '1's. 🧠 Problem Understanding We are given a binary string s that starts with 1 and contains only 0 and 1. We need to verify that all 1s appear in one contiguous block. ✔ Valid Examples 111000 110 1000 ❌ Invalid Example 1001 → Here, the segment of 1s breaks and appears again later. 💡 Key Idea If we ever encounter 0 followed by 1, it means a new segment of 1s has started, which violates the condition. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) Consistent practice like this is helping me strengthen my problem-solving and data structure skills every day. #Day182 #200DaysOfCoding #LeetCode #DSA #Programming #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
📅 Day 90 of #100DaysOfCode Problem: Find Minimum in Rotated Sorted Array (LeetCode 153) Approach (Binary Search): 1️⃣ Used two pointers: left and right. 2️⃣ Found mid in each iteration. 3️⃣ If nums[mid] > nums[right], it means the minimum lies in the right half → move left = mid + 1. 4️⃣ Otherwise, the minimum lies in the left half (including mid) → move right = mid. 5️⃣ Continued until left == right, which gives the minimum element. #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #Cplusplus #CodingChallenge #Programming #DeveloperLife #DailyDSA #KeepLearning #TechCommunity #GrowthMindset #Motivation
To view or add a comment, sign in
-
-
Day 29/30 — LeetCode Challenge 🚀 Problem: Find Numbers with Even Number of Digits (LC 1295) Today’s problem focused on simple number manipulation and digit counting. The idea is to iterate through the array, count how many digits each number has, and check whether the count is even. 🔹 Approach: Traverse each number in the vector. Count digits using a while loop (divide by 10). If digit count is even → increment result counter. 💻 C++ Solution: class Solution { public: int findNumbers(vector& nums) { int rem=0,cn=0,cnt=0; for(int i: nums) { int a=i; int cn=0; while(a) { if(a)cn++; rem=a%10; a=a/10; } if(cn%2==0)cnt++; } return cnt; } }; ✔️ Time Complexity: O(n * d) ✔️ Practiced loops, digit extraction, and clean iteration. Almost at the finish line — Day 30 next! 💪 #leetcode #dsa #cpp #codingchallenge #30daychallenge #programming
To view or add a comment, sign in
-
-
They said C++ was powerful. They forgot to mention the emotional roller coaster. 🎢 Here is proven representation of the C++ learning curve. I call it the "Honeymoon to Humility" pipeline. Current Status: Firmly planted in the "Wait for the move semantics and standard library updates" phase of the Slope of Enlightenment. 😅 To all my fellow developers grappling with pointers, template metaprogramming, and the ever-shifting standard: We're in this together. #cpp #programming #developerlife
To view or add a comment, sign in
-
-
While implementing STL containers in C++, I noticed something interesting 🤯 Both map and multimap are internally implemented using balanced trees (like Red-Black Trees). Both provide find() to search for a key. So I asked myself: 👉 If find() already tells us whether a key exists, why does count() exist? Here’s the catch 👇 In map, keys are unique count(key) → returns 0 or 1 In multimap, keys can be duplicate count(key) → returns number of occurrences So technically: find() → gives iterator (access to element) count() → gives frequency (especially useful in multimap) Even though map will only return 0 or 1, STL keeps interface consistency across associative containers. Small observation. But this is the kind of detail that sharpens STL understanding. ⚡ Curious — do you use find() or count() for existence checks in map? And do you think STL keeps count() just for interface consistency? #CPP #STL #CppDevelopers #DataStructures #Programming #SoftwareEngineering #DSA #CompetitiveProgramming #CodingJourney #Map #Multimap #LearningInPublic
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