Starting with #1/100DaysOfCode 💪 Sharing something from LeetCode today 🚀 Recently solved 1572. Matrix Diagonal Sum, and it had a small twist that can confuse many people. 💡 Approach : Traverse the matrix using loops Add elements where: i == j → Primary diagonal i + j == n - 1 → Secondary diagonal ⚠️ Common Confusion: Most people think that for this matrix: [[1,2,3], [4,5,6], [7,8,9]] the element 5 will be counted twice ❌ But it’s NOT counted twice ✅ Because we use: if(i == j) else if(i + j == n - 1) So once the first condition is true, the else if will not run. That’s why the middle element is added only one time. Grateful to learn and grow with guidance from: RAVI KUMAR Coding Blocks Sunstone #LeetCode #CodingJourney #Java #ProblemSolving #Consistency #CodingBlocks #SUNSTONE
LeetCode 1572 Matrix Diagonal Sum Solution
More Relevant Posts
-
🚀 Day 77 — LeetCode Practice 🚀 Today’s problem: Arranging Coins (LeetCode 441) 💡 What I learned today: • Practiced a greedy approach using simple iteration • Improved understanding of pattern-based problems • Learned how to efficiently simulate building a structure step by step • Strengthened logic for when to stop a loop based on conditions 🧠 Key Idea: Each row in the staircase needs an increasing number of coins (1, 2, 3, ...). Keep forming rows until the remaining coins are not enough to complete the next row. ⚡ Complexity: Time: O(n) Space: O(1) 📌 Takeaway: Even easy problems can teach strong fundamentals like iteration, condition handling, and greedy thinking. Consistency is key! #Day77 #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Sharing something from LeetCode today 🚀 If you enjoy puzzles where one small change affects everything — try this: LeetCode #73 — Set Matrix Zeroes A simple-looking matrix problem where one 0 can affect the entire row and column. 💡 My Approach: Traverse the matrix and mark the rows and columns that contain 0 Use extra arrays to keep track of affected rows & columns Update the matrix in a second pass visit : https://lnkd.in/gdVgt3V5 ⚠️ Common Mistake: Setting rows/columns to 0 immediately while traversing ❌ This causes newly updated 0s to spread further and gives wrong results 👉 Better way: First mark all affected rows & columns Then update the matrix in a separate pass ✅ Try solving it yourself — and if you do, share your approach/strategy! Grateful to learn and grow with guidance from: RAVI KUMAR Coding Blocks Sunstone #LeetCode #CodingJourney #DSA #Java #ProblemSolving #Consistency #CodingBlocks #SUNSTONE
To view or add a comment, sign in
-
-
🚀 Day 8 / 150 – LeetCode Challenge 📌 Problem: Plus One Today’s problem was simple but teaches an important concept — handling carry in arrays. 💡 Given a number represented as an array of digits, we need to add +1 and return the result. 🔑 Key Insight: - Start from the last digit - If digit < 9 → just add 1 and return - If digit == 9 → make it 0 and carry forward - If all digits are 9 → create a new array with leading ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(1) (except edge case) Consistency > Motivation. 8 days done, 142 to go 💪 #LeetCode #CodingChallenge #DataStructures #Java #Consistency #100DaysOfCode #CodingThinker
To view or add a comment, sign in
-
-
🚀 Day 73 — LeetCode Practice 🚀 Today’s problem: Find the Distance Value Between Two Arrays 💡 What I learned today: • Practiced nested loops and comparison logic • Strengthened understanding of absolute difference using Math.abs() • Learned how to use a boolean flag to optimize checking conditions • Improved handling of edge cases and conditions 🧠 Key idea: For each element in arr1, check all elements in arr2 → if no element satisfies the condition |arr1[i] - arr2[j]| ≤ d → then count it. ✨ Step by step, logic is becoming clearer and faster. #Day73 #LeetCode #Java #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 14 of #50DaysOfLeetCode Challenge Today’s coding challenge was Rotate List (LeetCode 61). The goal is to rotate a linked list to the right by k places. At first, it sounds like you need to move nodes one by one, but that’s inefficient. The key to a high-performance solution is to treat the list like a loop! The Efficiency Strategy: 1. Calculate Length: First, find the total length of the list (n). 2. Handle Large K: If k is larger than n, we only need to rotate k % n times. This avoids unnecessary cycles. 3. Make it a Circle: Connect the last node's "next" pointer to the original head to create a temporary circular list. 4. Find the New Break: Move (n - (k % n)) steps from the head to find the new tail of the rotated list. 5.Break the Loop: Save the node after the new tail as the new head, and then set the new tail's "next" to null. #LeetCode #BitManipulation #Java #Coding #Efficiency #TechLearning
To view or add a comment, sign in
-
-
🚀 Day 71 — LeetCode Practice 🚀 Today’s problem focused on strings and hashing techniques. ✅ Problem Solved: 🔹 First Letter to Appear Twice 💡 What I learned today: • Understood how to track characters using a HashSet • Learned how to detect duplicates efficiently in a single pass • Improved thinking on early return conditions • Practiced writing clean and optimized code 📊 Approach: • Traverse the string character by character • Store each character in a HashSet • If a character already exists → return it immediately • This ensures we find the first repeating character ⚡ Complexity: • Time: O(n) • Space: O(n) Consistency is slowly building confidence 💪 Every day, one step closer to becoming better at problem-solving 🚀 #Day71 #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 50/100 – LeetCode DSA Challenge 📌 Problem: Maximum 69 Number Today’s problem looked simple but taught an important concept of greedy thinking. 🧠 What I learned: How small changes in the leftmost digit can create the biggest impact The importance of choosing the optimal position rather than modifying everything Writing clean and efficient logic using strings and character arrays 💡 My Approach: Instead of checking all possibilities, I used a greedy strategy: Traverse the number from left to right Find the first occurrence of 6 Change it to 9 and stop immediately Because the leftmost digit contributes the most to the value, this guarantees the maximum possible number. 📊 Example Insight: 9669 → change first 6 → 9969 ✅ ✨ Key Takeaway: Not every problem needs brute force. Sometimes, identifying the most impactful change leads to the optimal solution. #LeetCode #DSA #100DaysOfCode #Java #CodingJourney #ProblemSolving #GreedyAlgorithm
To view or add a comment, sign in
-
-
🚀 Day 74 — LeetCode Practice 🚀 Today’s problem: Split a String in Balanced Strings 💡 What I learned today: • Understood the concept of balanced strings (equal number of 'L' and 'R') • Learned how to use a counter approach instead of extra space • Practiced greedy logic to split the string at the right moment • Improved my ability to track conditions while iterating through strings 🧠 Key Idea: Maintain a counter → Increment for 'L' Decrement for 'R' Whenever the counter becomes 0, we found a balanced substring ✅ 🔥 Takeaway: Simple logic + careful observation = efficient solution Consistency is the real game changer 💯 #Day74 #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
Day 61 of 100 Days of LeetCode 🚀 Solved First Unique Character in a String (LeetCode 387) today. 🔍 Key Learnings: Used frequency array to count character occurrences efficiently Importance of two-pass approach: Count frequencies Identify first unique character Reinforced concept of optimizing from brute force (O(n²)) to linear time (O(n)) ⚡ Complexity: Time: O(n) Space: O(1) (fixed 26 characters) 💡 Small problem, but a good reminder that clean logic and simplicity beat overcomplication. #Day61 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 90 — LeetCode Practice 🚀 Today’s focus was on bit manipulation and pattern recognition. ✅ Problem Solved: 🔹 Counting Bits 💡 Key Learnings from Today: • Learned how to count set bits (1s) in binary representation • Strengthened understanding of division by 2 and modulo operations • Explored how numbers behave in binary format • Improved problem-solving using iterative approach • Practiced writing clean and efficient logic 📌 Approach Used: Iterated through each number from 0 to n and counted the number of 1s in its binary form using a loop. 🎯 Next Step: Will optimize this using Dynamic Programming (DP) for better performance. Day by day improving consistency and logic 💪 #Day90 #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
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