📌 Day 23/100 – Squares of a Sorted Array (LeetCode 977) 🔹 Problem: Given a sorted integer array (may contain negatives), return a new array of the squares of each number, sorted in non-decreasing order. 🔹 Approach (Two Pointers): Since negative values become positive when squared, the largest values will be at either end of the array. So we: Use left and right pointers Compare squares → place the larger one at the end of result array Move pointers inward and fill result from back to front This avoids sorting again and keeps the solution O(n). 🔹 Time & Space Complexity: ⏱️ Time: O(n) 💾 Space: O(n) (result array) 🔹 Key Learnings: ✅ Two-pointer technique helps avoid extra sorting ✅ Always think about value transformation (like negative → squared) ✅ Filling from the back is a smart trick when maintaining sorted order #100DaysOfCode #LeetCode #Java #DSA #ProblemSolving #CodingJourney #TwoPointers #Programming
Solving LeetCode 977 with Two Pointers Technique
More Relevant Posts
-
🔥 Day 55 of #100DaysOfCode 🔥 🔹 Problem: Add Strings ✨ Approach: Used the power of BigInteger to handle large numeric strings efficiently. Converted both string inputs to BigIntegers, performed direct addition, and converted the sum back to a string — no manual digit handling required! 💡 ⚡ Complexity Analysis: Time Complexity: O(n) — proportional to the length of the longer string Space Complexity: O(1) — constant auxiliary space 📊 Result: ✅ Runtime: 20 ms (Beats 12.14%) ✅ Memory: 44.13 MB (Beats 13.83%) 💡 Key Insight: Sometimes, simplicity wins — leveraging built-in libraries like BigInteger can make your solution both elegant and powerful. 🚀 #100DaysOfCode #LeetCode #Java #BigInteger #StringManipulation #ProblemSolving #DSA #CodingJourney #Programming #CleanCode #LogicBuilding
To view or add a comment, sign in
-
-
🌟 Day 144 of 150 – LeetCode Challenge 📌 Problem #63: Unique Paths II 🔗 Category: Dynamic Programming 🧩 Problem Brief: ✔ Given an $m \times n$ grid with obstacles (marked as 1) and empty spaces (marked as 0), find the number of unique paths from the top-left to the bottom-right corner. The robot can only move down or right. 🧠 Approach Summary: ➤ This is an extension of the classic Unique Paths problem, solved efficiently using Dynamic Programming with Space Optimization. ➤ A 1D DP array, $\text{dp}$, is used to store the number of unique paths to reach the cells in the current row. The size of $\text{dp}$ is $n$ (the number of columns). ➤ Initialization: * $\text{dp}[0]$ is set to 1, representing one path to the first column's first cell (if it's not an obstacle). ➤ DP Transition: The grid is iterated row by row. For each cell $(i, j)$: * If the cell contains an obstacle ($\text{obstacleGrid}[i][j] == 1$), $\text{dp}[j]$ is set to 0, as no path can go through it. * If the cell is not an obstacle and $j > 0$, the number of paths to reach it is the sum of paths from the cell above it ($\text{dp}[j]$ from the previous row) and the cell to its left ($\text{dp}[j-1]$ from the current row). * If the cell is the first column ($j=0$) and not an obstacle, its path count only depends on the cell above it (which is already stored in $\text{dp}[0]$ before the update). ➤ The final answer is the value at the last column of the DP array, $\text{dp}[n-1]$. ⚡ Result: ✔ Runtime: 0 ms (Beats 100.00%) ✔ Memory: 41.76 MB (Beats 66.83%) #LeetCode #150DaysOfCode #CodingChallenge #Java #DSA #DynamicProgramming #ProblemSolving #CodeEveryday
To view or add a comment, sign in
-
💻 Day 69 of #LeetCode100DaysChallenge Solved LeetCode 264: Ugly Number II a dynamic programming problem that improves sequence generation and pointer-based optimization skills. 🧩 Problem: An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer n, return the n-th ugly number. 💡 Approach — Dynamic Programming with Three Pointers: 1️⃣ Maintain an array dp where dp[i] stores the i-th ugly number. 2️⃣ Use three pointers p2, p3, and p5 to track multiples of 2, 3, and 5 respectively. 3️⃣ At each step, choose the smallest of dp[p2]*2, dp[p3]*3, and dp[p5]*5 as the next ugly number. 4️⃣ Increment the corresponding pointer(s) to avoid duplicates. 5️⃣ Continue until the nth ugly number is found. ⚙️ Complexity: Time: O(N) — one pass to generate all ugly numbers Space: O(N) — for storing the sequence ✨ Key Takeaways: ✅ Strengthened understanding of pointer-based DP techniques. ✅ Learned how to efficiently generate sorted multiplicative sequences. ✅ Practiced optimization over brute-force factor checking. #LeetCode #100DaysOfCode #Java #DynamicProgramming #TwoPointers #Math #DSA #ProblemSolving #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
✅Day 74 of #100DaysOfLeetCode 1.📌Problem: Reverse Vowels of a String 2.🟩 Difficulty: Easy 3.📍Topic: Two Pointers 4.🎯 Goal: Given a string, reverse only the vowels in the string and return the modified string. Vowels include 'a', 'e', 'i', 'o', 'u' in both lower and upper cases. 5.🧠key idea: Approach 1: Use two pointers, one starting from the beginning and one from the end. Move towards each other, swap the vowels found at each pointer, and continue until all vowels are reversed. #LeetCode #CodingChallenge #100DaysOfCode #Algorithms #TechCareers #CodeNewbie #WomenWhoCode #Java #Programming #DailyCoding #Developer #InterviewPrep #TwoPointers #DataStructures #LearnToCode #ProblemSolving
To view or add a comment, sign in
-
-
✅Day 62 of #100DaysOfLeetCode 📌Problem: You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. 🟠 Difficulty: Medium 📍Topic: Array, Binary Search 🎯 Goal: Return the single element that appears only once in O(log n) time and O(1) space. 🧠key idea: Approach 1: The problem can be efficiently solved using a modified binary search. The core logic relies on the properties of the sorted array. If we are at an even index, its duplicate pair should be at the next (odd) index. If we are at an odd index, its pair should be at the previous (even) index. By examining the middle element and its neighbor, we can determine if the single, non-duplicated element lies in the left or right half of the array, allowing us to discard one half in each step and achieve a logarithmic time complexity. #100DaysOfCode #LeetCode #CodingChallenge #Java #DataStructures #Algorithms #BinarySearch #ProblemSolving #SoftwareDeveloper #TechJobs #DeveloperLife #CodeNewbie #Programming #LearnToCode #JobSeekers
To view or add a comment, sign in
-
-
✅Day 80 of #100DaysOfLeetCode 1.📌Problem: Set Mismatch You are given an array that represents a set of integers from 1 1 to n n, but due to an error, one number is duplicated and another is missing. The task is to find the duplicated number and the missing number and return them as an array. 2.🟢 Difficulty: Easy 3.📍Topic: Hashing,Array 4.🎯 Goal: Find the number that occurs twice and the number that is missing, then return them in an array. 5.🧠 key idea: Approach 1: Use a HashMap to count occurrences of each number in the input array. Identify the duplicated number by finding the one which appears twice. Compute the total sum expected (n(n+1)/2 n(n+1)/2) and compare with the actual sum (excluding the duplicate). The missing number equals the difference between the expected total and the corrected actual sum. Return the duplicate and missing numbers as the result array. #LeetCode #100DaysOfCode #CodingChallenge #Java #Programming #InterviewPrep #Algorithms #DataStructures #Hashing #ProblemSolving #Array #SoftwareEngineering #TechCareers #LearnToCode #CodeNewbie #DailyCode #ChallengeYourself
To view or add a comment, sign in
-
-
✅ Day 63 of LeetCode Medium/Hard Edition Today’s challenge was “Count Vowel Permutation” — a fascinating Dynamic Programming problem that explores state transitions across vowels 🔤✨ 📦 Problem: Given an integer n, we need to count how many strings of length n can be formed using only vowels (a, e, i, o, u), following these adjacency rules: 'a' → may only be followed by 'e' 'e' → may only be followed by 'a' or 'i' 'i' → may not be followed by another 'i' 'o' → may only be followed by 'i' or 'u' 'u' → may only be followed by 'a' Since the result can be very large, return it modulo 10⁹ + 7. 🔗 Problem Link: https://lnkd.in/gW7GMVUh ✅ My Submission: https://lnkd.in/g8t7mddT 💡 Thought Process: The problem models a finite-state machine, where each vowel transitions only to specific next vowels. We can use Dynamic Programming with memoization to count all possible sequences of length n, given a last-used vowel. Steps: 1️⃣ Define a recursive DP: dp[n][vowel] = number of valid strings of length n ending with that vowel. 2️⃣ Use transition rules to decide valid next vowels for each step. 3️⃣ Memoize results to avoid recomputation and take modulo 10⁹ + 7. 4️⃣ The total number of strings = sum of all dp[n][vowel] values. ⚙️ Complexity: ⏱ Time: O(n × 5) — one state per vowel per length 💾 Space: O(n × 5) — or O(1) if optimized iteratively #LeetCodeMediumHardEdition #100DaysChallenge #DynamicProgramming #ProblemSolving #Java #CodingChallenge #LeetCodeDay63
To view or add a comment, sign in
-
-
📌 Day 18/100 - Valid Palindrome (LeetCode 125) 🔹 Problem: Determine whether a string reads the same forward and backward, ignoring case and non-alphanumeric characters. 🔹 Approach: Implemented a two-pointer technique. Skipped all non-alphanumeric characters. Compared characters from both ends in lowercase. Returned true if all matched, otherwise false. 🔹 Key Learning: Two-pointer method keeps logic clean and efficient. Character handling is key when data isn’t uniform. Time complexity: O(n), Space complexity: O(1). Sometimes, solving elegantly is better than solving fast. ✨ #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
🎯 Day 7 of #365DaysofDSA Today I solved “Maximum Depth of Binary Tree” (LeetCode #104) 🌳 💡 Concepts used: • Recursion • Depth Calculation in Binary Trees ✨ Approach Summary: Used a recursive approach to find the longest path from the root node down to the farthest leaf node. For each node: 1️⃣ Recursively find the maximum depth of its left and right subtrees. 2️⃣ The current node’s depth = max(leftDepth, rightDepth) + 1. This elegant recursive solution divides the problem into smaller subproblems — calculating depth for subtrees and combining the results. 🚀 Key takeaway: Recursive thinking simplifies complex tree problems by leveraging the natural hierarchical structure of binary trees. 🌱 #Day7 #DSA #LeetCode #Java #BinaryTree #Recursion #ProblemSolving #CodingJourney #Programming #LearnByDoing #MaximumDepthOfBinaryTree
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