🔥 𝗗𝗮𝘆 𝟴𝟯/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟯𝟱𝟭. 𝗖𝗼𝘂𝗻𝘁 𝗡𝗲𝗴𝗮𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿𝘀 𝗶𝗻 𝗮 𝗦𝗼𝗿𝘁𝗲𝗱 𝗠𝗮𝘁𝗿𝗶𝘅 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 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

To view or add a comment, sign in

Explore content categories