Day 6/75 – Data Structures & Algorithms Practice Solved the “Best Time to Buy and Sell Stock” problem on LeetCode using Java. • 212 / 212 test cases passed • Runtime: 1 ms (Beats 99.92%) • Time Complexity: O(n) • Space Complexity: O(1) Approach: Used a single-pass greedy strategy by tracking the minimum price so far and continuously updating the maximum profit. This avoids checking all pairs and keeps the solution efficient. Key Takeaways: Importance of optimizing brute-force logic Writing clean and efficient code Strengthening understanding of greedy techniques Consistent daily problem-solving is helping me improve both logic building and performance optimization. #75DaysOfDSA #DSA #Java #LeetCode #ProblemSolving #SoftwareEngineering
Solved LeetCode Best Time to Buy and Sell Stock in Java
More Relevant Posts
-
🚀 Day 70 of #100DaysOfCode Solved 107. Binary Tree Level Order Traversal II on LeetCode 🔗 🧠 Key Insight: This is just level order traversal (BFS) but: 👉 Return levels from bottom to top instead of top to bottom ⚙️ Approach (DFS with Level Tracking): 1️⃣ Start traversal with level = 0 2️⃣ For each node: 🔹 If level not present → insert new list at front 🔹 Add value at correct position: 👉 res.get(res.size() - 1 - level) 3️⃣ Recurse: 🔹 Left → level + 1 🔹 Right → level + 1 ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) #100DaysOfCode #LeetCode #DSA #BinaryTree #BFS #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day: 97/365 📌 LeetCode POTD: Minimum Distance to the Target Element Easy Key takeaways/Learnings from this problem: 1. This one shows that sometimes the simplest approach—just scanning the array—is more than enough. 2. Key learning: track the minimum absolute distance on the go instead of storing unnecessary data. 3. Good reminder that not every problem needs fancy algorithms, clarity beats complexity. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
🚀Day 40 LeetCode 1161 – Maximum Level Sum of a Binary Tree Ever wondered how to find the level of a binary tree with the maximum sum of node values? 🤔 Here’s the idea: We traverse the tree level by level (BFS) and compute the sum at each level. The trick is to keep track of the maximum sum and return the smallest level where this occurs. 💡 Key Insight: Level-order traversal (using a queue) naturally processes nodes one level at a time, making it perfect for this problem. 🔧 Approach: ✔ Use a queue for BFS ✔ For each level: • Calculate sum of nodes ✔ Track maximum sum and corresponding level 🔥 Takeaway: Whenever a problem involves levels in a tree, think BFS first — it often leads to the cleanest solution. #LeetCode #DataStructures #Java #CodingInterview #BinaryTree #BFS #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Journey — LeetCode Practice 📌 Problem: Binary Tree Level Order Traversal (LeetCode 102) 💻 Language: Java 🔹 Approach: To solve this problem, I used Breadth-First Search (BFS) with a Queue to traverse the binary tree level by level. • Start by adding the root node into a queue • Use queue.size() to track the number of nodes at the current level • Process all nodes of that level using a loop • Store each level’s values in a separate list • Add left and right child nodes into the queue for the next level • Add each level list into the final result This approach efficiently groups nodes by levels and naturally fits the problem requirement. ⏱ Time Complexity: O(n) 🧩 Space Complexity: O(n) 📖 Key Learning: This problem strengthened my understanding of BFS, Queue operations, and level-wise tree traversal. It also reinforced how queue.size() helps process one complete level at a time — a very useful pattern for many tree problems 💡 #DSA #Java #LeetCode #ProblemSolving #CodingJourney #LearningInPublic #BinaryTree #BFS #Queue
To view or add a comment, sign in
-
-
🚀 Solved Binary Tree Level Order Traversal on LeetCode Today I practiced another important Binary Tree problem using Queue + BFS Traversal in Java. 🌳 🔹 Level Order Traversal Order: Node by Node, Level by Level 🔹 Approach Used: • Used Queue data structure • Inserted root node first • Processed one level at a time • Stored each level in separate list • Added left and right child nodes step by step 📊 Result: ✔️ All test cases passed (35/35) ✔️ Runtime: 1 ms ✔️ Beats 95.37% submissions 💡 Key Learning: DFS helps in depth traversal, but Queue + BFS is perfect when we need level wise output. Understanding when to use Stack, Recursion, or Queue is what makes problem solving stronger. 🔥 Currently improving Trees, Graphs, Binary Search, HashMap patterns, and DP step by step. #DSA #Java #LeetCode #BinaryTree #BFS #Queue #ProblemSolving #CodingJourney #Placements
To view or add a comment, sign in
-
-
Day 88 of #100DaysOfLeetCode 💻✅ Solved #56. Merge Intervals problem in Java. Approach: • Sorted intervals based on start time • Compared current interval with last merged interval • Merged overlapping intervals • Added non-overlapping intervals directly Performance: ✓ Runtime: 8 ms (Beats 91.28% submissions) 🚀 ✓ Memory: 49.16 MB (Beats 43.60% submissions) Key Learning: ✓ Learned interval merging technique ✓ Strengthened sorting + traversal logic ✓ Improved handling of overlapping ranges Learning one problem every single day 🚀 #Java #LeetCode #DSA #Arrays #Sorting #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 113 - LeetCode Journey Solved LeetCode 222 – Count Complete Tree Nodes ✅ This problem involves counting the total number of nodes in a complete binary tree, where all levels are fully filled except possibly the last, and nodes are as far left as possible. Approach: I implemented a recursive solution to count nodes. For each node, I recursively calculated the number of nodes in the left and right subtrees and added 1 for the current node. Although this solution works correctly, it follows a straightforward traversal approach and does not fully utilize the properties of a complete binary tree. A more optimized approach can reduce time complexity by comparing left and right subtree heights to detect perfect subtrees and compute their node count directly. Complexity Analysis: • Time Complexity: O(n) • Space Complexity: O(h), where h is the height of the tree (recursion stack) Key Takeaways: • Basic tree traversal (DFS) can solve counting problems effectively • Understanding tree properties can help in optimizing solutions further • Complete binary trees allow more efficient approaches than general trees All test cases passed successfully 🎯 #LeetCode #DSA #BinaryTree #Recursion #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 41 of Daily DSA 🚀 Solved LeetCode 20: Valid Parentheses ✅ Problem: Given a string containing only (), {}, [], determine if the input string is valid. Rules: Open brackets must be closed by the same type Open brackets must be closed in the correct order Every closing bracket must have a matching opening bracket Approach: Used a Stack to track opening brackets and validate matching pairs. Steps: Traverse the string Push opening brackets onto the stack For closing brackets → check top of stack If it matches → pop Else → return false At the end, stack should be empty ⏱ Complexity: • Time: O(n) • Space: O(n) 📊 LeetCode Stats: • Runtime: 3 ms (Beats 87.41%) ⚡ • Memory: 43.37 MB A classic stack problem that builds strong fundamentals for expression parsing & validation. #DSA #LeetCode #Java #Stack #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 74 of #100DaysOfLeetCode 💻✅ Solved #162. Find Peak Element problem in Java. Approach: • Used Binary Search technique to efficiently find the peak element • Set two pointers, left at start and right at end of the array • Calculated mid index using safe mid formula • Compared nums[mid] with nums[mid + 1] to determine direction • If mid element is smaller, moved search space to right half • Otherwise, moved search space to left half including mid • Continued until left and right pointers converged • Final position (left == right) represents the peak index Performance: ✓ Runtime: 0 ms (Beats 100.00% submissions) 🚀 ✓ Memory: 44.32 MB (Beats 25.49% submissions) Key Learning: ✓ Strengthened understanding of Binary Search on unsorted arrays ✓ Learned how to apply divide-and-conquer beyond traditional searching ✓ Improved intuition for peak finding using neighbor comparison ✓ Practiced optimizing search space instead of linear scanning Learning one problem every single day 🚀 #Java #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 10/30 — DSA Challenge 🚀 Problem: Construct Binary Tree from Inorder and Postorder Traversal Topic: Tree + Recursion + Hashing Difficulty: Medium Approach: Used postorder to identify root node (last element) Used inorder to split tree into left and right subtrees Stored inorder indices in a HashMap for fast lookup Built tree recursively (right subtree first, then left) Mistake / Challenge: Initially confused about traversal order Made mistake by building left subtree first instead of right Fix: Understood that postorder processes (Left → Right → Root) So while constructing → build Right first, then Left Key Learning: Preorder → Root first Postorder → Root last Always align recursion order with traversal type Time Taken: 50 minutes Consistency check ✅ See you on Day 11. GitHub Repo: https://lnkd.in/gHW9vKUf #DSA #LeetCode #Java #Trees #Recursion #LearningInPublic
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