🔥 𝗗𝗮𝘆 𝟴𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟱𝟭. 𝗖𝗼𝘂𝗻𝘁 𝗡𝗲𝗴𝗮𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿𝘀 𝗶𝗻 𝗮 𝗦𝗼𝗿𝘁𝗲𝗱 𝗠𝗮𝘁𝗿𝗶𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks simple — but the optimal solution is a beautiful staircase traversal that most people miss. 𝙏𝙝𝙚 𝙜𝙧𝙞𝙙 𝙥𝙧𝙤𝙥𝙚𝙧𝙩𝙮: Rows and columns are both sorted in non-increasing order. That means once you hit a negative, everything below it in the same column is also negative. 𝙎𝙩𝙖𝙞𝙧𝙘𝙖𝙨𝙚 𝙖𝙥𝙥𝙧𝙤𝙖𝙘𝙝 (𝙩𝙤𝙥-𝙧𝙞𝙜𝙝𝙩 𝙘𝙤𝙧𝙣𝙚𝙧): ✅ Start at row 0, last column ✅ If grid[row][col] < 0 → all elements below are negative too → add (rows - row) to count, move left ✅ If grid[row][col] >= 0 → move down 𝘾𝙤𝙢𝙥𝙡𝙚𝙭𝙞𝙩𝙮: ⏱ Time: O(m + n) — at most m+n steps 📦 Space: O(1) The naive brute force is O(m×n). This approach uses the sorted structure to skip entire column segments in one shot. That's the difference between knowing a pattern and just looping. This staircase trick works on any sorted matrix — keep it in your toolkit! 🧠 📂 𝑭𝒖𝒍𝒍 𝒔𝒐𝒍𝒖𝒕𝒊𝒐𝒏 𝒐𝒏 𝑮𝒊𝒕𝑯𝒖𝒃: https://lnkd.in/g_Edk5d3 17 more days. Staying consistent! 💪 #LeetCode #Day83of100 #100DaysOfCode #Java #DSA #Matrix #TwoPointers #CodingChallenge #Programming
More Relevant Posts
-
🔥 𝗗𝗮𝘆 𝟴𝟲/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟮𝟰. 𝗙𝗶𝗻𝗱 𝗣𝗶𝘃𝗼𝘁 𝗜𝗻𝗱𝗲𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Deceptively simple — and a perfect introduction to the prefix sum pattern. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Find the index where the sum of all elements to the left equals the sum of all elements to the right. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗣𝗿𝗲𝗳𝗶𝘅 𝗦𝘂𝗺: ✅ Compute the total sum in one pass ✅ Track leftTotal as we iterate ✅ rightTotal = total - leftTotal - nums[i] ✅ If leftTotal == rightTotal → pivot found! No extra arrays. No nested loops. Just one pre-computation and one clean scan. 𝗧𝗵𝗲 𝗸𝗲𝘆 𝗶𝗻𝘀𝗶𝗴𝗵𝘁: Instead of recalculating both sides at every index, derive the right sum from what you already know — total and left. That drops it from O(n²) to O(n) instantly. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — two passes 📦 Space: O(1) This pattern — precompute total, derive the other side on the fly — shows up everywhere: product arrays, equilibrium points, range queries. A must-know! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/g_E5Ahfe 14 more days. The finish line is in sight! 💪 #LeetCode #Day86of100 #100DaysOfCode #Java #DSA #PrefixSum #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
📘 DSA Journey — Day 19 Today’s focus: Sliding Window with uniqueness and distance constraints. Problems solved: • Minimum Consecutive Cards to Pick Up (LeetCode 2260) • Substrings of Size Three with Distinct Characters (LeetCode 1876) Concepts used: • Sliding Window / Two-pointer technique • HashMap / Set for tracking duplicates • Window size and distance calculation Key takeaway: In Minimum Consecutive Cards to Pick Up, the goal is to find the smallest subarray containing duplicate elements. A HashMap is used to store the last index of each card. When a duplicate is found, we calculate the distance between indices and update the minimum length. In Substrings of Size Three with Distinct Characters, a fixed-size sliding window (size = 3) is used. We check whether all characters in the window are distinct using a set or frequency array, and count such valid substrings. Both problems highlight how tracking element positions or uniqueness helps efficiently solve problems involving constraints on subarrays or substrings. Continuing to strengthen sliding window patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟕𝟔/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐃𝐞𝐜𝐨𝐝𝐞 𝐭𝐡𝐞 𝐒𝐥𝐚𝐧𝐭𝐞𝐝 𝐂𝐢𝐩𝐡𝐞𝐫𝐭𝐞𝐱𝐭 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Treat the encoded string as a matrix with given rows. Traverse diagonally starting from each column of the first row. Append characters while moving diagonally (down-right). Trim trailing spaces from the result. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Matrix simulation with diagonal traversal. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Many string problems can be visualized as matrix traversals — changing perspective simplifies the solution. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Strings #Matrix #Simulation #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
📌 LeetCode Daily Challenge — Day 26 Problem: 3548. Equal Sum Grid Partition II Topic: Array, Matrix, Prefix Sum, HashSet, Greedy 📌 Quick Problem Sense: You're given an m × n integer grid. Make one straight cut, horizontal or vertical to split it into two non-empty parts with equal sums. The twist? You're allowed to remove at most one border element (right at the cut edge) from either side to balance the sums. Return true if such a partition is possible! 🧠 Approach (Simple Thinking): 🔹 Compute the total sum of the grid upfront 🔹 Scan row by row, maintain a running top sum, derive bottom = total - top 🔹 The imbalance is diff = top - bottom 🔹 A cut is valid if: diff == 0 → already perfectly balanced ✅ diff equals a corner cell of the top section ✅ diff equals the left-border cell of the current bottom row ✅ diff exists in a HashSet of all values seen so far ✅ 🔹 Use a HashSet to track all border values already scanned, O(1) lookup to check if removing one element fixes the imbalance 🔹 For vertical cuts → simply transpose the matrix and reuse the same horizontal cut logic! 🔹 Also try reversed row order to cover cuts scanned from the opposite direction, 4 passes total, all reusing the same function 🔄 ⏱️ Time Complexity: 4 passes through the grid (original, reversed, transposed, transposed+reversed) → O(m × n) Single scan per pass, HashSet lookups in O(1) — clean and efficient! 📦 Space Complexity: HashSet of seen values → O(m × n) worst case Transpose grid → O(m × n) Overall → O(m × n) I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/g2FHaT4S If you solved it by explicitly enumerating only the border cells, or used a different balance-check trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #Programming
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟬/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟰𝟲. 𝗖𝗵𝗲𝗰𝗸 𝗜𝗳 𝗡 𝗮𝗻𝗱 𝗜𝘁𝘀 𝗗𝗼𝘂𝗯𝗹𝗲 𝗘𝘅𝗶𝘀𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 90 days in — still finding elegant solutions. 🎯 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Check if there exist two indices i ≠ j such that arr[i] == 2 * arr[j]. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗛𝗮𝘀𝗵𝗦𝗲𝘁 𝗼𝗻𝗲-𝗽𝗮𝘀𝘀: ✅ For each element, check if its double already exists in the set ✅ Also check if half of it exists — but only if it's even (to avoid false integer division) ✅ Add the element to the set and move on 𝗧𝗵𝗲 𝘀𝘂𝗯𝘁𝗹𝗲 𝗲𝗱𝗴𝗲 𝗰𝗮𝘀𝗲: Why guard i%2 == 0 before checking i/2? If i = 3, then i/2 = 1 in integer division — but 3 is not double of 1. The even check ensures we only look for a true half, not a truncated one. 💡 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — single pass 📦 Space: O(n) — HashSet No sorting. No nested loops. Just one scan with O(1) lookups. The even-check guard is the kind of small detail that separates a correct solution from a buggy one in an interview. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gYqNTTEZ 10 more days. Double digits left — let's finish this! 🚀 #LeetCode #Day90of100 #100DaysOfCode #Java #DSA #HashSet #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
🚀Day 39 #LeetCode 199 – Binary Tree Right Side View Ever wondered what a binary tree looks like from the right side? 👀 Let’s break it down! 🧠 Problem Insight When you observe a binary tree from the right, at every level you only see one node — the rightmost node. 👉 So the goal is simple: Capture the last node at each level ⚡ Approach (Level Order Traversal - BFS) ✔ Traverse the tree level by level ✔ At each level, pick the last node ✔ Add it to the result 📊 Complexity ⏱ Time: O(n) 📦 Space: O(n) 🔥 Key Takeaway 👉 Rightmost node at each level = Visible node 💡 Example Input: [1,2,3,null,5,null,4] Output: [1,3,4] 💬 Have you tried solving this using DFS (Right-first traversal)? Drop your approach below! #LeetCode #DataStructures #BinaryTree #CodingInterview #Java #Programming
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟮𝟳𝟲𝟮. 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗼𝘂𝘀 𝗦𝘂𝗯𝗮𝗿𝗿𝗮𝘆𝘀 | 🟡 𝗠𝗲𝗱𝗶𝘂𝗺 | 𝗝𝗮𝘃𝗮 This one is a proper sliding window + monotonic deque problem — one of the most powerful combos in DSA. 𝗧𝗵𝗲 𝗰𝗼𝗻𝗱𝗶𝘁𝗶𝗼𝗻: A subarray is valid if max - min ≤ 2 for all pairs. Checking this naively for every subarray is O(n²). We need smarter. 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝘄𝗶𝗻𝗱𝗼𝘄 + 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀: ✅ maxDeque → monotonic decreasing (tracks window max) ✅ minDeque → monotonic increasing (tracks window min) ✅ Expand right every iteration ✅ Shrink left when max - min > 2 ✅ Every valid window ending at right contributes (right - left + 1) subarrays 𝗪𝗵𝘆 𝘁𝘄𝗼 𝗱𝗲𝗾𝘂𝗲𝘀? They give O(1) access to the window's current max and min at all times — no rescanning needed when the window shrinks. 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(n) — each element enters and exits each deque at most once 📦 Space: O(n) deque space The moment this pattern clicked for me — sliding window controlling the range, deques tracking the extremes — a whole class of hard problems became approachable. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/e_Mu8-Eh 16 more days. Grinding through! 💪 #LeetCode #Day84of100 #100DaysOfCode #Java #DSA #SlidingWindow #MonotonicDeque #CodingChallenge #Programming
To view or add a comment, sign in
-
✨ Day 38 of 90 – Pattern Mastery Journey 🧠 Pattern:Binary Triangle Pattern 💡 Approach: ✔ Used nested loops to control rows and columns ✔ Applied a simple condition `(i + j) % 2` to alternate values ✔ Printed ‘1’ when the sum is even, otherwise ‘0’ ✔ No extra variables needed — clean and efficient logic 🚀 This problem helped me understand how **mathematical conditions can simplify pattern logic**, making the code more optimized and readable. #PatternMasteryJourney #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
✅ Solved LeetCode 217 — Contains Duplicate! Given an integer array nums, return true if any value appears at least twice. 🧠 My Approach: Sort + Linear Scan → Sort the array → adjacent duplicates are guaranteed to be neighbors → One pass to check if nums[i] == nums[i-1] → Time: O(n log n) | Space: O(1) 💡 Key Insight: Sorting brings duplicates side by side — no need for extra space like a HashSet. Trade time for space! ```java Arrays.sort(nums); for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) return true; } return false; ``` 🔄 Alternative approaches: • HashSet → O(n) time, O(n) space • Brute force → O(n²) time (avoid!) Every problem teaches you a new trade-off. Keep grinding! 💪 #LeetCode #DSA #CodingInterview #Java #ProblemSolving #SoftwareEngineering #100DaysOfCode #Programming
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