Day 19 of Daily DSA 🚀 Solved LeetCode 2089: Find Target Indices After Sorting Array ✅ Approach: Instead of actually sorting the array, I counted: elements less than the target elements equal to the target The starting index is determined by how many elements are smaller than the target, and then indices are built for all equal elements. Simple counting → no extra sorting needed 💡 ⏱ Complexity: • Time: O(n) — single pass • Space: O(1) — excluding output list 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 44.83 MB (Beats 83.41%) A neat example of how thinking beyond “just sort it” can lead to cleaner and faster solutions. #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Consistency #Arrays
LeetCode 2089: Target Indices After Sorting Array
More Relevant Posts
-
Day 20 of Daily DSA 🚀 Solved LeetCode 179: Largest Number ✅ Approach: Converted all integers to strings and used a custom comparator while sorting. For two numbers a and b, we compare: a + b vs b + a This ensures the order that forms the largest possible number. Edge Case Handled 💡 If the highest element after sorting is "0", then the entire array contains zeros → return "0" instead of "000". ⏱ Complexity: • Time: O(n log n) — sorting with custom comparator • Space: O(n) — string array 📊 LeetCode Stats: • Runtime: 6 ms (Beats 96.21%) ⚡ • Memory: 44.96 MB (Beats 76.86%) Comparator-based problems really sharpen logical thinking 🔥 #DSA #LeetCode #Java #ProblemSolving #DailyCoding #Sorting #Consistency
To view or add a comment, sign in
-
-
Day 87/100 – LeetCode Challenge ✅ Problem: #867 Transpose Matrix Difficulty: Easy Language: Java Approach: Direct Matrix Swapping Time Complexity: O(m × n) Space Complexity: O(m × n) for result matrix Key Insight: Transpose swaps rows and columns: transposed[i][j] = matrix[j][i]. New matrix dimensions: col × row (original dimensions swapped). Solution Brief: Created result matrix with dimensions col × row. Nested loops assign each element to swapped position. Returned transposed matrix. #LeetCode #Day87 #100DaysOfCode #Matrix #Java #Algorithm #CodingChallenge #ProblemSolving #TransposeMatrix #EasyProblem #Array #2DArray #DSA
To view or add a comment, sign in
-
-
Day 91/100 – LeetCode Challenge ✅ Problem: #98 Validate Binary Search Tree Difficulty: Medium Language: Java Approach: Inorder Traversal with Stack Time Complexity: O(n) Space Complexity: O(h) where h = tree height Key Insight: Inorder traversal of BST produces sorted ascending sequence. Compare each visited node with previous node to detect violations. Solution Brief: Used stack for iterative inorder traversal. Maintained pre pointer to track previous visited node. While traversing: Push all left nodes to stack Pop node, check if root.val <= pre.val (violation) Update pre to current node Move to right subtree #LeetCode #Day91 #100DaysOfCode #Tree #BST #Java #Algorithm #CodingChallenge #ProblemSolving #ValidateBST #MediumProblem #InorderTraversal #Stack #DSA
To view or add a comment, sign in
-
-
Day 85/100 – LeetCode Challenge ✅ Problem: #226 Invert Binary Tree Difficulty: Easy Language: Java Approach: Recursive DFS (Divide and Conquer) Time Complexity: O(n) Space Complexity: O(h) where h = tree height Key Insight: Swap left and right child at every node, then recursively invert both subtrees. Classic recursion problem — made famous by Google interview story. Solution Brief: Base case: if root is null, return null. Swap left and right children using temporary variable. Recursively call invertTree on left and right subtrees. Return the root (now inverted). #LeetCode #Day85 #100DaysOfCode #Tree #Java #Algorithm #CodingChallenge #ProblemSolving #InvertBinaryTree #EasyProblem #DFS #Recursion #DSA
To view or add a comment, sign in
-
-
Day 82/100 – LeetCode Challenge ✅ Problem: #43 Multiply Strings Difficulty: Medium Language: Java Approach: Manual Multiplication with Result Array Time Complexity: O(n × m) Space Complexity: O(n + m) Key Insight: Multiply digits from right to left (least significant first). Store intermediate results in array where index i + j + 1 holds current digit. Handle carry by adding to previous index. Solution Brief: Edge case: if either number is "0", return "0". Created result array of size n1 + n2 (max possible digits). Nested loops multiply each digit of num1 with each digit of num2. Accumulated results with proper carry handling. Built final string skipping leading zeros. #LeetCode #Day82 #100DaysOfCode #Math #String #Java #Algorithm #CodingChallenge #ProblemSolving #MultiplyStrings #MediumProblem #Multiplication #Array #DSA
To view or add a comment, sign in
-
-
Day 88/100 – LeetCode Challenge ✅ Problem: #112 Path Sum Difficulty: Easy Language: Java Approach: Recursive DFS with Decreasing Target Time Complexity: O(n) Space Complexity: O(h) where h = tree height Key Insight: Subtract current node value from target as we traverse. When reaching leaf, check if remaining target equals zero. Solution Brief: Base case: if root is null → no path. Leaf node check: if both children null, verify targetSum - root.val == 0. Recursive case: subtract current value and check left or right subtree. #LeetCode #Day88 #100DaysOfCode #Tree #DFS #Java #Algorithm #CodingChallenge #ProblemSolving #PathSum #EasyProblem #Recursion #BinaryTree #DSA
To view or add a comment, sign in
-
-
Day 29 of Daily DSA 🚀 Solved LeetCode 287: Find the Duplicate Number ✅ Problem: Given an array containing n + 1 integers where each number is in the range [1, n], find the duplicate number. Approach: Used the index marking technique. Key Idea: Treat the value as an index Convert the value to absolute (Math.abs) Mark the visited index by making the number negative If we encounter an index that is already negative, that value is the duplicate This allows us to detect duplicates efficiently without extra space. ⏱ Complexity: • Time: O(n) • Space: O(1) 📊 LeetCode Stats: • Runtime: 4 ms (Beats 91.67%) ⚡ • Memory: 82.75 MB A clever trick that uses the array itself as a visited map. #DSA #LeetCode #Java #ProblemSolving #Algorithms #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 100/100 – LeetCode Challenge ✅ Problem: #41 First Missing Positive Difficulty: Hard Language: Java Approach: In-Place Index Marking Time Complexity: O(n) Space Complexity: O(1) Key Insight: Place each positive integer in its correct position (index i should contain i+1). Mark presence using negative values without extra space. Solution Brief: First pass: replace numbers outside range [1, n] with n+1 (ignore them) Second pass: treat each number as index, mark that index negative Third pass: first positive index = missing number If all marked, answer is n+1 #LeetCode #Day100 #100DaysOfCode #Array #Java #Algorithm #CodingChallenge #ProblemSolving #FirstMissingPositive #HardProblem #InPlace #Optimization #DSA
To view or add a comment, sign in
-
-
Day 96/100 – LeetCode Challenge ✅ Problem: #107 Binary Tree Level Order Traversal II Difficulty: Medium Language: Java Approach: DFS with Reverse Level Insertion Time Complexity: O(n) Space Complexity: O(n) Key Insight: Traverse tree with recursion while tracking level. Insert new level at beginning of list when encountering new depth. Add node values to appropriate level from bottom-up order. Solution Brief: Recursive levelMaker traverses tree with current level. When level >= list.size(), add new level at index 0 (bottom layer). Recursively process left and right subtrees. Add current node value to list.get(list.size() - level - 1) — mapping to correct bottom-up position. #LeetCode #Day96 #100DaysOfCode #Tree #DFS #Java #Algorithm #CodingChallenge #ProblemSolving #LevelOrderBottom #MediumProblem #BinaryTree #Recursion #DSA
To view or add a comment, sign in
-
-
Day 89/100 – LeetCode Challenge ✅ Problem: #459 Repeated Substring Pattern Difficulty: Easy Language: Java Approach: Substring Division Check Time Complexity: O(n²) worst case Space Complexity: O(n) Key Insight: A string can be formed by repeating its substring if the substring length divides the string length. Check all divisors of length from largest to smallest for efficiency. Solution Brief: Iterated i from l/2 down to 1 (larger substrings first). If l % i == 0, substring length divides string length. Constructed repeated pattern by appending substring m = l/i times. Compared constructed string with original. #LeetCode #Day89 #100DaysOfCode #String #Java #Algorithm #CodingChallenge #ProblemSolving #RepeatedSubstring #EasyProblem #Substring #Pattern #DSA
To view or add a comment, sign in
-
Explore related topics
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
I did by sorting + bs, I didn't get this approach tbh . Thanks a lot man!! Actually if i have x elements smaller than target then starts at x after sorting....and if I have y elements equal to target then target ends at x+y-1