Some problems look difficult at first, but once you know the right data structure, they become much easier. 🚀 Day 99/365 — DSA Challenge Solved: Sliding Window Maximum Problem idea: We are given an array and a window of size k that moves from left to right. For each window position, we need to find the maximum element inside that window. Efficient approach: Use a Deque (Double Ended Queue) to keep track of useful elements for the current window. Steps: 1. Store indices in the deque 2. Remove indices that are outside the current window 3. Remove elements smaller than the current element from the back 4. The front of the deque always stores the index of the maximum element 5. Record the maximum once the first window is formed This ensures we always know the maximum of the current window efficiently. ⏱ Time: O(n) 📦 Space: O(k) Day 99/365 complete. 💻 266 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #Deque #LeetCode #LearningInPublic
Solving Sliding Window Maximum with Deque in Java
More Relevant Posts
-
When a problem asks for maintaining a condition on both minimum and maximum, monotonic data structures become very powerful. 🚀 Day 110/365 — DSA Challenge Solved: Longest Continuous Subarray With Absolute Diff ≤ Limit Problem idea: We need to find the longest subarray such that the difference between max and min elements is ≤ limit. Efficient approach: Use Sliding Window + Two Monotonic Deques. Steps: 1. Use one deque to maintain maximum elements (decreasing order) 2. Use another deque to maintain minimum elements (increasing order) 3. Expand window by moving the right pointer 4. If (max − min) exceeds limit, shrink window from the left 5. Track the maximum valid window size This ensures we can get max and min in O(1) time. ⏱ Time: O(n) 📦 Space: O(n) Day 110/365 complete. 💻 255 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #Deque #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Some problems become much easier when you reframe the question. 🚀 Day 107/365 — DSA Challenge Solved: Minimum Operations to Reduce X to Zero Problem idea: We need to remove elements from the left or right so that their sum equals x, with minimum operations. Efficient approach: Instead of removing elements, think in reverse: 👉 Find the longest subarray with sum = totalSum − x Steps: 1. Calculate total sum of the array 2. Set target = totalSum − x 3. Find the longest subarray with sum = target using sliding window 4. Result = total length − longest subarray length This converts the problem into a familiar sliding window problem. ⏱ Time: O(n) 📦 Space: O(1) Day 107/365 complete. 💻 258 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Many subarray problems follow the same hidden pattern — once you recognize it, they become much easier. 🚀 Day 102/365 — DSA Challenge Solved: Count Number of Nice Subarrays Problem idea: We need to count the number of subarrays that contain exactly k odd numbers. Efficient approach: Use the same trick as previous problem: subarrays with exactly k odds = subarrays with ≤ k odds − subarrays with ≤ (k − 1) odds Steps: 1. Use a sliding window to count subarrays with at most k odd numbers 2. Expand window by moving right pointer 3. If odd count exceeds k, shrink window from left 4. Add valid subarrays ending at each index 5. Subtract results to get exact count This pattern works beautifully for binary-like conditions. ⏱ Time: O(n) 📦 Space: O(1) Day 102/365 complete. 💻 263 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Yesterday's matrix problem was solved using traversal. Today's matrix problem was solved using Binary Search. Same topic. Different thinking. 🚀 Day 95/365 — DSA Challenge Solved: Search a 2D Matrix Key observation: The matrix is not just row-wise sorted. The first element of each row is greater than the last element of previous row. This means the matrix behaves like a sorted 1D array. So we can apply Binary Search. We convert index → row, col: row = mid / cols col = mid % cols And perform normal binary search. ⏱ Time: O(log(m * n)) 📦 Space: O(1) Day 95/365 complete. 💻 270 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Matrix #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 69 of #100DaysOfCode Solved 98. Validate Binary Search Tree on LeetCode 🔗 🧠 Key Insight: A valid BST must satisfy: 👉 Left subtree → all values < root 👉 Right subtree → all values > root 👉 This must hold for every node (not just immediate children) ⚙️ Approach (Range Validation - DFS): 1️⃣ Start with full range: 🔹 (-∞, +∞) 2️⃣ For each node: 🔹 Check if min < node.val < max 🔹 If not → ❌ invalid BST 3️⃣ Recurse left: 🔹 Range becomes (min, node.val) 4️⃣ Recurse right: 🔹 Range becomes (node.val, max) 5️⃣ Return true only if both subtrees are valid ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(h) #100DaysOfCode #LeetCode #DSA #BinaryTree #BST #Recursion #DFS #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Some subarray counting problems become much easier when we transform them into “at most” problems. 🚀 Day 101/365 — DSA Challenge Solved: Binary Subarrays With Sum Problem idea: We need to count the number of subarrays whose sum equals a given goal in a binary array. Efficient approach: Instead of directly counting subarrays with exact sum, we use a trick: subarrays with sum = goal = subarrays with sum ≤ goal − subarrays with sum ≤ (goal − 1) Steps: 1. Create a helper function to count subarrays with sum at most k 2. Use a sliding window to maintain a valid window where sum ≤ k 3. Add the number of valid subarrays ending at each index 4. Subtract results to get the exact count for the goal This converts the problem into an efficient sliding window solution. ⏱ Time: O(n) 📦 Space: O(1) Day 101/365 complete. 💻 264 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 70/100 – DSA Challenge Consistency is turning into confidence 💯 Today’s problem focused on string comparison and optimization. 🔍 Problem: Given two arrays of strings — queries and dictionary — identify all query words that differ from any dictionary word by at most 2 characters. 🧠 Approach: Compare each query with every dictionary word Count character mismatches If mismatches ≤ 2 → include the query in the result Use early termination to optimize comparisons 💡 Key Learning: This problem reinforced the importance of efficient brute force, where small optimizations like early breaks can significantly improve performance. ⏱️ Time Complexity: O(Q × D × L) 📦 Space Complexity: O(1) (excluding output) Another milestone unlocked 🔓 — Day 70 done! On to Day 71 🚀 #100DaysOfCode #DSA #Java #ProblemSolving #CodingJourney #Consistency #KeepLearning
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟖𝟏 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding two unique numbers where all others appear twice. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Single Number III 🔗 https://lnkd.in/dEQaaF3F 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐁𝐢𝐭 𝐌𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧 (𝐎𝐩𝐭𝐢𝐦𝐚𝐥) Steps: • XOR all elements → gives a ⊕ b (two unique numbers) • Find rightmost set bit → differentiates a & b • Divide numbers into 2 groups based on that bit • XOR each group → get the two unique numbers 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • XOR cancels duplicate elements • Bit tricks help split data into meaningful groups • Problems can often be optimized from O(n²) → O(n) • Understanding bit-level operations is very powerful 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When duplicates cancel out, think in terms of XOR and bit patterns. 81 days consistent 🚀 On to Day 82. #DSA #Arrays #BitManipulation #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Some linked list problems are all about reversing connections in a controlled range. 🚀 Day 115/365 — DSA Challenge Solved: Reverse Linked List II Problem idea: We need to reverse a portion of a linked list from position left to right, while keeping the rest unchanged. Efficient approach: Use in-place reversal with pointer manipulation. Steps: 1. Use a dummy node to simplify edge cases 2. Move a pointer to the node just before position left 3. Start reversing nodes one by one within the range 4. Adjust links so that reversed nodes are inserted at the correct position 5. Connect the reversed portion back to the list This avoids creating extra space and keeps the solution efficient. ⏱ Time: O(n) 📦 Space: O(1) Day 115/365 complete. 💻 250 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LinkedList #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Some problems break the standard sliding window pattern — especially when negative numbers are involved. 🚀 Day 104/365 — DSA Challenge Solved: Shortest Subarray with Sum at Least K Problem idea: We need to find the length of the shortest subarray whose sum is at least k. The challenge is that the array can contain negative numbers, so normal sliding window won't work. Efficient approach: Use Prefix Sum + Monotonic Deque. Steps: 1. Build a prefix sum array 2. Use a deque to store indices of useful prefix sums 3. While current sum − smallest prefix ≥ k → update answer 4. Maintain increasing order in deque by removing larger prefix sums from the back 5. This ensures we always get the shortest valid subarray This approach efficiently handles negative numbers while keeping optimal time complexity. ⏱ Time: O(n) 📦 Space: O(n) Day 104/365 complete. 💻 261 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #Deque #PrefixSum #LeetCode #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