Day 3: The Reality of the "Hard" Label. 🧠 Today was a humbling reminder that growth isn't always a straight line. I tackled LeetCode #1411 (Number of Ways to Paint N × 3 Grid), and it took me the entire day to truly grasp the logic. As a beginner in Dynamic Programming, I faced multiple failed attempts and head-scratching moments. But after internalizing the state transitions, I finally got that "Accepted" screen. The Challenge: Find the number of ways to paint an n*3 grid using 3 colors such that no two adjacent cells have the same color. My Approach (State-Space DP): State Generation: First, I generated all possible valid configurations for a single row (out of 27 combinations, only 12 are valid). Adjacency Map: I built a neighbours list to determine which row configurations can follow each other without breaking the color constraints. Memoization: Used a recursive solve function with a memo table to store results of subproblems and avoid redundant calculations. Handling Large Numbers: Used long and MOD (10^9 + 7) to prevent integer overflow. Complexity: Time: O(n * S^2) where S is the number of valid states (12). Space: O(n * S) for the memoization table. It’s currently a "brute-force" DP approach, and I know there are further mathematical optimizations (O(log n)), but today was about understanding the concept over the shortcut. I'll be back to optimize this once my DP foundation is stronger! 📂 GitHub Repo: https://lnkd.in/g-RMsp6a If you're also struggling with DP, keep pushing. The "aha!" moment is worth the struggle. #Java #DynamicProgramming #LeetCode #BuildingInPublic #SoftwareEngineering #JadavpurUniversity #100DaysOfCode
LeetCode #1411: Dynamic Programming Challenge
More Relevant Posts
-
🔥 Day 90 of LeetCode Challenge ✅ Problem: Trapping Rain Water 🔗 https://lnkd.in/d9aP8z5k 💡 Approach: Two Pointers Use two pointers: left and right Track leftMax and rightMax to store the maximum height seen so far from both ends At each step, move the pointer with the smaller height Water trapped = min(leftMax, rightMax) - current height ⏳ Why this works: The water level at any index depends on the minimum of the tallest bars on both sides — two pointers help compute this in one pass. ⚡ Performance: ✅ All test cases passed (324 / 324) 🚀 Runtime: 0 ms (Beats 100%) 💾 Memory: 47.62 MB (Beats 69.76%) 📌 Key Learning: Optimizing space from prefix/suffix arrays to a two-pointer greedy approach reduces space complexity to O(1) while keeping time O(n). 🧠 Concepts Used: Arrays · Two Pointers · Greedy · Optimization 📈 90 days done — consistency > motivation. #LeetCode #Day90 #DSA #Java #TwoPointers #ProblemSolving #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Binary Tree Maximum Path Sum 🌳🧠 Just completed LeetCode – Binary Tree Maximum Path Sum, one of the classic Hard problems that truly tests your understanding of tree DFS + recursion + dynamic programming concepts. 🔍 Key Learnings: A path can start and end at any node (not necessarily the root). At each node, we must consider three possibilities: Path passing through the node (left + node + right) Path extending from one side only (to be returned to parent) Path consisting of the node alone (important for negative values) Maintained a global maximum to track the best path sum seen so far. 🧠 Core Insight: The value returned to the parent must be a single-sided path The global answer may include both children, but cannot be extended upward ⚙️ Complexity: Time: O(n) — each node visited once Space: O(h) — recursion stack (h = tree height) This problem significantly strengthened my understanding of: DFS traversal patterns Handling negative values in trees Separating global answers from recursive return values #LeetCode #DSA #BinaryTree #DynamicProgramming #Recursion #Java #ProblemSolving #CodingJourney #SoftwareEngineering #LearningByDoing
To view or add a comment, sign in
-
-
🚀 Day 47 | LeetCode Medium 🟩 Set Matrix Zeroes Today’s problem focused on matrix traversal + space trade-offs, and it was a great reminder that clarity beats complexity. 🧠 Core Idea If any cell in the matrix is 0, then its entire row and column must be set to 0. Instead of modifying the matrix immediately (which can break logic), I used an auxiliary tracking approach. 🛠️ Approach Used Traverse the matrix once Mark affected rows and columns using helper arrays Traverse again and update cells based on these markers This avoids incorrect cascading zeros and keeps the logic clean. ⚡ Why this works Separation of detection and update No accidental overwrites Easy to reason and debug ⏱️ Complexity Time: O(m × n) Space: O(m + n) 🔗 Code on GitHub 👉 https://lnkd.in/g-zxCztf 💡 Key Learning In matrix problems, when you update is just as important as what you update. 🔥 Consistency > Speed Another step forward in my daily DSA journey 🚀 #LeetCode #LeetCodeDailyProblem #SetMatrixZeroes #DSA #Java #Arrays #Matrix #ProblemSolving #CodingJourney #100DaysOfCode #TechCommunity
To view or add a comment, sign in
-
-
🚀 Day 50 of #100DaysOfCode 🎯 (Halfway there! 🔥) Today’s challenge was an array + dynamic programming twist problem — 📊 LeetCode: Maximum Product Subarray 📌 Problem Summary Given an integer array, find the contiguous subarray (containing at least one number) that has the largest product. At first glance, it looks similar to maximum subarray sum… but the presence of negative numbers changes everything ⚠️ 🧠 My Approach: Tracking Max & Min Products The key insight 👇 A negative number can turn the smallest product into the largest. So instead of tracking only the maximum, I tracked: ✅ max product ending at current index ✅ min product ending at current index At each step: Compute new max using (current, max*current, min*current) Compute new min similarly Update the global result This keeps everything in one pass 🚀 ⚙️ Complexity Analysis ⏱ Time: O(n) 💾 Space: O(1) Efficient and clean ✨ 🔥 Key Learning Negative numbers can flip the problem logic Some DP problems don’t need arrays — just smart state tracking Always think in terms of states, not just values ✅ Solution accepted with strong runtime performance Another powerful array pattern mastered 💪 Onward from Day 50 — the grind continues 🚀🔥 #100DaysOfCode #LeetCode #Java #DynamicProgramming #Arrays #ProblemSolving #CodingJourney #DSA
To view or add a comment, sign in
-
-
Why every developer should use a .env file If you’re still hard-coding secrets like API keys, database passwords, or debug settings in your code, it’s time to stop. The .env file helps you: ✅ Keep sensitive data secure ✅ Separate configuration from code ✅ Easily switch between development, testing, and production environments ✅ Avoid accidentally exposing secrets on GitHub In Python, tools like os.environ and libraries such as python-dotenv make working with environment variables simple and clean. Best practice: ✔ Add .env to .gitignore ✔ Share a .env.example instead ✔ Keep your code portable and secure Small habit. Big impact. #Python #SoftwareDevelopment #BestPractices #DevTips #EnvironmentVariables
To view or add a comment, sign in
-
#200DaysOfCode – Day 111 Word Search Problem: Given a 2D grid of characters and a word, determine whether the word exists in the grid. The word must be formed using adjacent cells (up, down, left, right), and each cell can be used only once. Example: Input: board = [[A, B, C, E], [S, F, C, S], [A, D, E, E]] word = "ABCCED" Output: true My Approach: Used DFS (Depth First Search) starting from every cell. Matched characters step-by-step with the given word. Marked cells as visited during the path to avoid reuse. Applied backtracking to explore all possible directions safely. Time Complexity: O(m × n × 4^L) Space Complexity: O(L) (recursion stack) Grid problems often look complex, but breaking them down with DFS + backtracking makes the logic clear and manageable. Patience and careful state handling are the real keys here #takeUforward #100DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #DFS #DataStructures #Algorithms #CodeNewbie #Consistency
To view or add a comment, sign in
-
-
🚀 Day 488 of #500DaysOfCode 📌 LeetCode Problem: 2976. Minimum Cost to Convert String I Today’s challenge was about minimizing the cost to convert one string into another using a set of given character transformation rules — each with an associated cost. 💡 Key Idea: Model this as a graph problem where each lowercase character is a node, and every transformation is a directed edge with a cost. Then use Floyd–Warshall to compute the minimum conversion cost between all pairs of characters. 🔍 Approach Summary: ✔ Build a 26×26 cost matrix for all character conversions ✔ Use Floyd–Warshall to find the cheapest paths ✔ For each position in the string, sum the cost of converting source[i] → target[i] ✔ Return the total cost — or -1 if impossible ⚙️ Time Complexity: 26³ for preprocessing + O(n) for string traversal, which is efficient even for large inputs. 💬 What I learned today: Graph applications go beyond nodes and edges — even string transformations can be mapped beautifully as a shortest path problem! 🔁 Keep coding. Keep growing. #java #leetcode #codingchallenge #graphalgorithms #floydwarshall
To view or add a comment, sign in
-
-
📅 Day 62 of #100DaysOfLeetCode 🔢 Problem: 1458. Max Dot Product of Two Subsequences 🟥 Difficulty: Hard 🧠 Problem Summary Given two integer arrays, find the maximum dot product between non-empty subsequences of equal length while preserving order. 💡 Key Insight This problem is a DP + subsequence variant similar to LCS, but with an important twist: 👉 The answer can be negative, so initializing DP with 0 will fail. 👉 We must ensure at least one pair is chosen. 🚀 Approach (Memoization / Top-Down DP) Use recursion with indices (i, j) At each step, consider: Pairing nums1[i] with nums2[j] Skipping an element from either array Use Math.max(0, previous) to avoid extending negative subsequences Memoize results to achieve O(n × m) complexity ⏱ Complexity Time: O(n × m) Space: O(n × m) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
🚀 Day 57 of #100DaysOfCode Today’s problem was a clean and classic two-pointer exercise — 🔁 LeetCode 344: Reverse String Simple on the surface, but a great reminder of in-place algorithms and pointer manipulation. 📌 Problem Summary You’re given a character array s. Your task is to reverse the array in-place, using O(1) extra space. 🧠 Approach Used: Two Pointers ✔️ Initialize: left = 0 right = s.length - 1 ✔️ Swap characters while left < right, then move pointers inward. This ensures: No extra memory Linear traversal Clean and readable logic ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) (in-place) ✔️ 477 / 477 test cases passed 🚀 Runtime: 0 ms (Beats 100%) 🔥 Key Learning Even the simplest problems reinforce core fundamentals: Two-pointer technique In-place operations Space optimization Mastering basics = dominating harder problems later 💪 Onward to Day 58 🚀 #100DaysOfCode #LeetCode #ReverseString #TwoPointers #Java #DSA #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 467 of #500DaysOfCode 📌 Problem Solved: 1458. Max Dot Product of Two Subsequences (Hard) Today’s problem was a reminder that not all subsequence problems are greedy-friendly. We’re given two arrays and asked to find non-empty subsequences whose dot product is maximized. Sounds simple at first — until negative numbers enter the picture. 💡 Key Realization: This problem is similar to LCS, but instead of matching characters, we’re: pairing numbers, multiplying them, and deciding whether to take or skip each element. The tricky part? 👉 We cannot choose empty subsequences, and sometimes the best answer is negative. 🧠 Approach Used: Used 2D Dynamic Programming dp[i][j] represents the maximum dot product using prefixes of both arrays At each step, we decide to: take the current pair, skip an element from the first array, or skip one from the second A key trick was using max(previous, 0) to safely start a new subsequence when needed ⚙️ Concepts Reinforced: Dynamic Programming on two sequences Handling negative values correctly Non-empty subsequence constraints Why initialization matters in DP 🔥 Hard problems like this really sharpen DP intuition and edge-case thinking. On to Day 468 🚀 #LeetCode #DynamicProgramming #HardProblems #ProblemSolving #Java #DSA #Consistency #CodingJourney #500DaysOfCode
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