Day 26 of Daily DSA 🚀 Solved LeetCode 225: Implement Stack using Queues ✅ Problem: Implement a LIFO stack using only queue operations. Approach: Used two queues: main queue → stores elements in stack order helper queue → assists during push operation Key idea: Move all elements from main to helper Insert the new element into main Move all elements back from helper to main This ensures the latest pushed element stays at the front, allowing pop() and top() in constant time. ⏱ Complexity: • push() → O(n) • pop() → O(1) • top() → O(1) • empty() → O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 42.54 MB (Beats 89.59%) A great problem to understand how one data structure can simulate another. #DSA #LeetCode #Java #DataStructures #CodingJourney #ProblemSolving #Consistency
Implement Stack using Queues in Java
More Relevant Posts
-
🚀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
-
-
Today I solved LeetCode 107 – Binary Tree Level Order Traversal II. 🧩 Problem Summary: Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. Instead of returning the traversal from top to bottom, the result should be returned from the lowest level up to the root. 💡 Key Concepts Used: Binary Trees Breadth First Search (BFS) Queue data structure Level Order Traversal Bottom-up traversal logic 🧠 Approach: Use a queue to perform level order traversal (BFS). Process nodes level by level. For each level, collect the node values into a list. Instead of adding the level at the end, insert it at the beginning of the result list. This automatically builds the traversal from bottom to top. 📚 What I Learned: How BFS works in tree-based problems. Processing nodes level by level using a queue. Building bottom-up results efficiently. Strengthening understanding of tree traversal variations. Consistent practice with tree problems helps build stronger algorithmic thinking 🌳 Learning step by step every day 🚀 #LeetCode #DSA #BinaryTree #LevelOrderTraversal #BFS #Queue #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Today I solved 206. Reverse Linked List, one of the most fundamental problems in Data Structures. 📌 Problem Statement Given the head of a singly linked list, reverse the list and return the reversed list. 🧠 Approach (Iterative) To reverse the linked list, we keep track of three pointers: prev → stores the previous node curr → current node we are processing next → temporarily stores the next node At each step, we reverse the link by pointing the current node to the previous node, then move all pointers one step forward. ⚡ Time Complexity: O(n) 📦 Space Complexity: O(1) 📊 Example Input: [1,2,3,4,5] Output: [5,4,3,2,1] ✨ This problem is important because Linked List reversal is a building block for many advanced problems like reversing in groups, detecting cycles, and more. #LeetCode #DataStructures #LinkedList #Java #DSA #CodingPractice #100DaysOfCode
To view or add a comment, sign in
-
-
Day 5/100 – Data Structures & Algorithms Problems Solved: • Design Linked List • Rotate List • Intersection of Two Linked Lists • Minimum Index Sum of Two Lists • Remove Duplicates from Sorted List • Remove Duplicates from Sorted List II Concepts Practiced: • Linked List manipulation • Pointer traversal • Hashing for lookup optimization • Handling duplicates in sorted structures Key Learning: Working with Linked Lists improves understanding of pointer movement, node manipulation, and efficient traversal techniques. Staying consistent with daily problem solving while preparing for backend development roles. #100DaysOfCode #DSA #LeetCode #Java #BackendDevelopment
To view or add a comment, sign in
-
-
🚀 Day 70 Today’s problem: Type of Triangle 🔺 ✨ Even simpler approach (no extra space): Just sort + direct comparison — no HashSet needed! 💡 Steps: Sort the array Check triangle condition → a + b > c Compare values directly: All equal → Equilateral Any two equal → Isosceles All different → Scalene 🧠 Minimal Code Idea: No extra data structures Fewer lines More readable 🔥 Why better? Less memory + faster checks = clean & optimal thinking 💬 Takeaway: Good code isn’t just correct — it’s simple and efficient. #Day70 #DSA #Java #CleanCode #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 27 of #75DaysofLeetCode Solved LeetCode 933 — Number of Recent Calls Today I worked on a simple yet powerful problem that teaches an important concept used in real-world systems: sliding window over streaming data. 💡 Problem Insight: We need to count how many requests happened in the last 3000 milliseconds for every new request. 📌 Instead of checking all past requests every time (which is inefficient), we use a Queue to maintain only relevant data. ⚡ Approach: Store incoming timestamps in a queue Remove all outdated timestamps (< t - 3000) The remaining size of the queue gives the answer 🧠 This is a perfect example of: Sliding Window Technique Real-time Data Processing Efficient Queue Usage 💻 Java Code: class RecentCounter { Queue<Integer> queue = new LinkedList<>(); public int ping(int t) { queue.offer(t); while (queue.peek() < t - 3000) { queue.poll(); } return queue.size(); } } 📊 Complexity: Time: O(1) amortized Space: O(N) 🔥 Real-world connection: This concept is widely used in: API rate limiting Server request monitoring Streaming analytics ✅ Small problems like this build strong intuition for handling large-scale systems. #LeetCode #DataStructures #Java #Coding #100DaysOfCode #SoftwareEngineering #ProblemSolving
To view or add a comment, sign in
-
-
Day 68/100 🚀 | #100DaysOfDSA Solved LeetCode 374 – Guess Number Higher or Lower today. The problem was to find a hidden number between 1 and n using the provided guess() API, which tells whether the guessed number is too high, too low, or correct. Approach: Used Binary Search to efficiently find the target number. • Initialized left = 0 and right = n. • Repeatedly calculated mid = left + (right - left) / 2. • Called the guess(mid) API: If it returns 0, found the number → return mid. If it returns 1, the number is higher → move left = mid + 1. If it returns -1, the number is lower → move right = mid - 1. • Continued until the correct number is found. Time Complexity: O(log n) Space Complexity: O(1) Key takeaway: Whenever the search space is sorted or bounded with a condition, Binary Search is the most optimal approach to reduce time complexity drastically. #100DaysOfDSA #LeetCode #DSA #Java #BinarySearch #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
📌 Median of Two Sorted Arrays Platform: LeetCode #4 Difficulty: Hard ⚙️ Approach • Apply binary search on the smaller array for efficiency • Define search space from 0 to n1 • Partition both arrays such that: – Left half contains (n1 + n2 + 1) / 2 elements – Right half contains remaining elements • Identify boundary elements: – l1, l2 → left side elements – r1, r2 → right side elements • Check valid partition: – l1 <= r2 and l2 <= r1 • If valid: – For even length → median = average of max(left) and min(right) – For odd length → median = max(left) • If not valid: – If l1 > r2, move left – Else move right 🧠 Logic Used • Binary Search on partition index • Dividing arrays into balanced halves • Handling edge cases using boundary values • Achieving optimal O(log(min(n1, n2))) time complexity 🔗 GitHub: https://lnkd.in/g_3x55n8 ✅ Day 36 Completed – Revised advanced binary search and partition-based problem. #100DaysOfCode #Java #DSA #ProblemSolving #CodingJourney #Algorithms #DataStructures #LeetCode #CodingPractice #CodeEveryDay #BinarySearch #ArrayProblems
To view or add a comment, sign in
-
-
Day 95/100 – LeetCode Challenge ✅ Problem: #71 Simplify Path Difficulty: Medium Language: Java Approach: Stack-based Directory Parsing Time Complexity: O(n) Space Complexity: O(n) Key Insight: Split path by / and process each component: .. → pop from stack (go up one directory) . or empty → ignore (current directory) Valid directory name → push to stack Solution Brief: Used ArrayDeque as stack for directory components. Iterated through path components after splitting by /. Applied rules: .. → remove last directory if stack not empty . or empty → skip else → push to stack Built canonical path by joining stack components with /. Return "/" if empty, else constructed path. #LeetCode #Day95 #100DaysOfCode #Stack #String #Java #Algorithm #CodingChallenge #ProblemSolving #SimplifyPath #MediumProblem #Deque #PathParsing #DSA
To view or add a comment, sign in
-
-
⚡𝗗𝗮𝘆 𝟳𝟬 / 𝟭𝟮𝟬 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗖𝗼𝗱𝗲 Before today’s update, a quick note -> I wasn’t posting regularly for a few days because I was actively working on building a project. Now back to sharing the learning again. 🚀 Problem -> Topological Sorting (DFS based) =>Topological sorting is used in Directed Acyclic Graphs (DAGs) to order vertices such that for every directed edge u → v, node u appears before v in the ordering. In simple terms -> it helps determine dependency order (like course scheduling or task execution order). Approach -> → Use Depth First Search (DFS) → Maintain a visited array to track visited nodes → Use a stack to store the order of nodes → Traverse all vertices of the graph → For every unvisited node, perform DFS → After exploring all neighbors of a node, push it into the stack → Finally, pop elements from the stack to get the topological order Key Insight -> A node is added to the stack only after all its dependencies are visited, ensuring correct ordering. Time Complexity -> O(V + E) Space Complexity -> O(V) This problem helped me understand how DFS can be used to solve dependency ordering problems in graphs. #120DaysOfCode #Day70 #Graphs #TopologicalSort #DFS #Java #DSA
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