🚀 Day 41 / 100 | Spiral Matrix II -Intuition: -The goal is to generate an n × n matrix filled with numbers from 1 to n^2 in spiral order. -Start from the top row, move right, then move down the right column, then move left across the bottom row, and finally move up the left column. -After completing one layer, shrink the boundaries and repeat the process until the matrix is filled. -Approach: O(n²) -Initialize an empty n × n matrix. -Maintain four boundaries: top, bottom, left, and right. -Start filling numbers from 1 to n^2. -Fill the top row from left -> right and move the top boundary down. -Fill the right column from top -> bottom and move the right boundary left. -Fill the bottom row from right ->left and move the bottom boundary up. -Fill the left column from bottom -> top and move the left boundary right. -Repeat the process until all elements are filled. -Complexity: Time Complexity: O(n²) Space Complexity: O(n²) #100DaysOfCode #Java #DSA #LeetCode #Matrix
Generating Spiral Matrix in Java with O(n²) Complexity
More Relevant Posts
-
🚀 Day 31 of #100DaysOfCode Solved 240. Search a 2D Matrix II on LeetCode 🔍📊 🧠 Key insight: In this matrix, each row is sorted left → right and each column is sorted top → bottom. Starting from the top-right corner lets us eliminate an entire row or column at each step. ⚙️ Approach: 🔹Start at top-right element 🔹If value equals target → return true 🔹If value is smaller → move down (increase row) 🔹If value is larger → move left (decrease column) 🔹Continue until the target is found or boundaries are crossed ⏱️ Time Complexity: O(m + n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Matrix #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 43 / 100 | Largest Number Intuition: Given a list of non-negative integers and need to arrange them to form the largest possible number. Normal numeric sorting does not work for this problem. Instead, Compare numbers based on their concatenation order. For two numbers a and b, we compare "ab" and "ba". The order that forms the larger concatenated value should come first. Approach: O(n log n) Convert all integers into strings. Sort the array using a custom comparator. For two strings a and b, compare (b + a) with (a + b). If (b + a) is larger, b should come before a. After sorting, concatenate all strings to form the final result. In case the array contains only 0's, the largest element will be "0". Complexity: Time Complexity: O(n log n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #Sorting #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 44 / 100 | Subsets Intuition: The task is to generate all possible subsets of a given array. Each element has two choices: either include it in the subset or exclude it. By exploring every possible combination of these choices, we can generate all subsets. Backtracking helps us build subsets step by step and explore all possibilities. Approach: O(n * 2^n) Start with an empty subset. Use backtracking to explore all subset combinations. At each step, add the current subset to the result list. Iterate through the remaining elements of the array. Include the current element in the subset and move forward recursively. After recursion, remove the element (backtrack) to explore other possibilities. Continue this process until all subsets are generated. Complexity: Time Complexity: O(n * 2^n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #Backtracking
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 | Median of Two Sorted Arrays Intuition: We are given two sorted arrays and need to find the median of the combined numbers. Since both arrays are already sorted, we can merge them in sorted order. Once we have the merged array, finding the median becomes simple. If the total number of elements is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. Approach: Use two pointers to traverse both arrays. Compare the elements and insert the smaller one into a new array. Continue this process until all elements are merged. Finally, calculate the median based on the length of the merged array. Complexity: Time Complexity: O(n + m) Space Complexity: O(n + m) #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟔𝟖/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐌𝐚𝐭𝐫𝐢𝐱 𝐒𝐢𝐦𝐢𝐥𝐚𝐫𝐢𝐭𝐲 𝐀𝐟𝐭𝐞𝐫 𝐂𝐲𝐜𝐥𝐢𝐜 𝐒𝐡𝐢𝐟𝐭𝐬 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Each row is cyclically shifted: Even-indexed rows → left shift Odd-indexed rows → right shift Instead of actually shifting rows, compare elements using index mapping with modulo arithmetic. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Matrix traversal with cyclic index mapping. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐦 × 𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝟏) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Cyclic shift problems can often be solved without actual shifting by using modular index calculations. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Matrix #Simulation #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
🚀 Day 49 of #100DaysOfCode Solved 86. Partition List on LeetCode 🔗 🧠 Key Insight: We need to rearrange a linked list such that: 🔹All nodes < x come before nodes ≥ x 🔹While maintaining the original relative order This is a classic linked list partitioning problem. ⚙️ Approach: 1️⃣ Create two dummy lists: 🔹small → nodes with values < x 🔹large → nodes with values ≥ x 2️⃣ Traverse the original list: 🔹Append each node to the appropriate list 3️⃣ Connect both lists: 🔹small → large 4️⃣ Return the head of the new list 🎯 This ensures: 🔹Stable ordering 🔹Clean and efficient separation ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (no extra nodes, just pointers) #100DaysOfCode #LeetCode #DSA #LinkedList #Algorithms #Java #CodingPractice #InterviewPrep
To view or add a comment, sign in
-
-
⏰ Temporary Field: When instance variables only show up for certain operations, cluttering your class interface with null-initialized fields. Here's how Extract Class refactoring transforms your code: ✅ Move temporary fields into dedicated classes ✅ Eliminate null-initialized field clutter ✅ Create clearer class interfaces ✅ Make object state predictable at every stage 🎯 Key takeaway: Extract Class refactoring organizes temporary state into focused objects that only exist when they're needed. What's your experience with refactoring Temporary Fields? Share your favorite techniques in the comments. #CleanCode #Refactoring #Java #TemporaryField #DeveloperTips https://lnkd.in/gJQeWPJ9
To view or add a comment, sign in
-
-
🚀 Day 47 / 100 | Combination Sum Intuition: We are given a list of numbers and a target value. The goal is to find all combinations of numbers that add up to the target. we can use the same number multiple times. So instead of moving forward immediately, we can stay at the same index and keep using that number until the target is reached or exceeded. Approach: O(2^n) Use backtracking to explore all possible combinations. Maintain a list that stores the current combination. If the target becomes 0, we found a valid combination. If the target becomes negative or we reach the end of the array, return. At each step we have two choices: Pick the current number and reduce the target. Skip the current number and move to the next index. After exploring a choice, remove the number to try other possibilities (backtracking). Complexity: Time Complexity: O(2^n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #Backtracking
To view or add a comment, sign in
-
-
Day 20/100: The "Cheat Code" for String Rotations 🔄 I’m back on the grind! Today’s challenge was checking if one string is a rotation of another (e.g., "waterbottle" and "erbottlewat"). The Strategy: Instead of writing complex loops to shift characters, I used the Concatenation Trick: 1️⃣ Check if lengths are equal. 2️⃣ Create a new string by adding the first string to itself (s1 + s1). 3️⃣ Check if the second string exists inside that combined string. It’s a simple, elegant O(n) solution that shows how sometimes "working smarter" with data structures beats "working harder" with loops. 20% of the way there. Let's keep moving! 🚀 #100DaysOfCode #Java #DSA #Strings #ProblemSolving #Unit2 #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🔥 Day 60 / 100 – LeetCode Challenge ✅ Solved: 160. Intersection of Two Linked Lists Today’s problem was all about understanding pointer behavior and memory references in linked lists. 💡 Key Insight: Instead of comparing values, we compare node references. If two linked lists intersect, they will share the same tail nodes. 🚀 Approach Used (Optimal): Two pointers (pA, pB) Traverse both lists When one reaches the end, switch to the other list They eventually meet at the intersection node (or null) 🧠 Why it works: Both pointers travel equal distance → LengthA + LengthB, aligning perfectly without extra space. ⏱ Complexity: Time: O(m + n) Space: O(1) 💻 Result: ✔️ Accepted (41/41 test cases) ⚡ Runtime: 1 ms (Beats 99.94%) 📌 Takeaway: Sometimes the smartest solution is not about extra data structures, but about clever traversal. #Day60 #100DaysOfCode #LeetCode #Java #DSA #LinkedList #CodingJourney
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