✏️ DSA Diary Day 14/100 📊➗: Solving LeetCode’s “Maximum Dot Product of Two Subsequences” 🚀✨ Today I worked on a Dynamic Programming + Recursion + Optimization problem on LeetCode 🔥👇 👉 Maximum Dot Product of Two Subsequences This problem is a great example of how DP helps handle complex choices efficiently 🧠💡 🔹 My Approach 🛠️🧠 I used Recursion + Memoization (Top-Down DP) 🔁👇 🔸 Step 1: Define DP State 📌 dp(i, j) = Maximum dot product using elements from nums1[i…] elements from nums2[j…] 🔸 Step 2: Choices at Each Step 🔍 At every (i, j) position, we have 4 options: 1️⃣ Take both elements & continue nums1[i] * nums2[j] + dp(i+1, j+1) 2️⃣ Take both & stop here nums1[i] * nums2[j] 3️⃣ Skip nums1[i] dp(i+1, j) 4️⃣ Skip nums2[j] dp(i, j+1) We take the maximum of all these options 🔝✨ 🔸 Step 3: Memoization 🧠 To avoid recomputation, I used a 2D memo array This reduces time complexity from exponential → O(n*m) 🚀 🔸 Why NEG_INF? ⚠️ To ensure at least one pair is selected, I returned a very small value when indices go out of bounds. 🔹 Key Learnings 📚✨ ✅ DP is perfect for subsequence + optimization problems ✅ Always consider all possible choices ✅ Memoization saves massive computation time ✅ Handling negative values carefully is crucial ✅ Clean recursion = clear logic 🧠💯 🔥 This problem felt like a perfect mix of: 📊 Arrays + 🔁 Recursion + 🧠 DP + 📈 Optimization #LeetCode 🚀 #Java ☕ #DSA 🧠 #DynamicProgramming 📊 #ProblemSolving 💡 #CodingChallenge 💻 #100DaysOfCode 🔥 #DSADiaryByRethanya ✨ #Recursion 🔁 #Memoization 🧩 #LearnInPublic 📢 #TechJourney 🚀
Max Dot Product of Two Subsequences with DP and Recursion
More Relevant Posts
-
🎯 #LeetcodePotd | Problem Solved with 99%+ Runtime Performance Sometimes the smartest solution isn’t fancy — it’s structured thinking: Just solved LeetCode 1984 – Minimum Difference Between Highest and Lowest of K Scores ✅ And honestly? This is a perfect example of how sorting + sliding window = instant clarity. 🧠 Problem in Simple Words You’re given student scores. Pick k students such that the difference between the highest and lowest score is as small as possible. 👉 Goal: Minimize (max − min) among any group of size k. 💡 Key Insight (Beginner Friendly) If the array is sorted, then: Any group of k students will be contiguous The difference becomes: nums[i + k − 1] − nums[i] So instead of brute force chaos, we: Sort the array Slide a window of size k Track the minimum difference Clean. Efficient. Scalable. ⏱️ Complexity Analysis Time Complexity: O(n log n) (sorting dominates) Space Complexity: O(1) (ignoring sorting internals) 📊 Submission Stats ✔ Accepted: 118 / 118 test cases ⚡ Runtime: 7 ms 🔥 Beats 99.17% of submissions 💾 Memory: 47.15 MB 🧠 Takeaway Sort first. Think visually. Then optimize. On to the next one. Consistency > motivation. 💪 #LeetCode #DSA #Java #ProblemSolving #Algorithms #SlidingWindow #Sorting #CodingJourney #BeginnersInTech #100DaysOfCode #SoftwareEngineering #InterviewPrep
To view or add a comment, sign in
-
-
🚀 LeetCode 43 — Multiply Strings | DSA Learning Journey Today I worked on a problem that looks simple at first but actually dives deep into strings, arrays, and math logic. 🧩 Problem We’re given two non-negative integers as strings, and we must return their product also as a string. ⚠️ The twist: We cannot directly convert the strings into integers because the numbers can be extremely large. 💡 What this problem really tests This problem is basically about simulating the multiplication method we learned in school — but through code. Instead of relying on built-in big number operations, we: ✅ Multiply digits one by one ✅ Handle carries manually ✅ Store intermediate results in an array ✅ Build the final answer as a string 🛠 The Key Insight If the first number has length m and the second has length n, the result can have at most m + n digits. So the approach becomes: • Use an array of size m + n • Multiply digits from right to left • Carefully place each digit and carry in the correct positions That positioning logic is what makes the solution work. 📊 Time Complexity O(m × n) — because every digit of the first number multiplies with every digit of the second. #LeetCode #DSA #Java #ProblemSolving #CodingJourney #SoftwareEngineering #Algorithms
To view or add a comment, sign in
-
-
✏️ DSA Diary Day 16/100 📚🔤: Solving LeetCode’s “Minimum ASCII Delete Sum for Two Strings” 🚀✨ Today I worked on a Dynamic Programming + String Optimization problem on LeetCode 🔥👇 👉 Minimum ASCII Delete Sum for Two Strings This problem beautifully shows how DP helps minimize deletion cost while preserving the maximum common structure between two strings 🧠💡 🔹 My Approach 🛠️🧠 I used Bottom-Up Dynamic Programming (Tabulation) 🔽👇 🔸 Step 1: Define the DP Idea 📌 Instead of directly calculating what to delete, I focused on maximizing the ASCII value of the common subsequence between both strings. The more valuable the common part is, the less we need to delete 🔁✨ 🔸 Step 2: Decision Making 🔍 At every character comparison: 1️⃣ If both characters match → Add their ASCII value to the total 2️⃣ If they don’t match → Carry forward the maximum value from previous comparisons This is similar to the Longest Common Subsequence (LCS) concept, but with weights (ASCII values) instead of length 📈 🔸 Step 3: Getting the Final Answer 🧮 We calculate the total ASCII value of both strings, then subtract twice the value of the common subsequence. This ensures: ✔ Only unnecessary characters are deleted ✔ The deletion cost is minimum #LeetCode 🚀 #Java ☕ #DSA 🧠 #DynamicProgramming 📊 #ProblemSolving 💡 #CodingChallenge 💻 #100DaysOfCode 🔥 #DSADiaryByRethanya ✨ #Strings 🔤 #Tabulation 🧩 #LearnInPublic 📢 #TechJourney 🚀
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟮𝟳/𝟭𝟬𝟬 | 𝗗𝗲𝗰𝗼𝗱𝗲 𝗦𝘁𝗿𝗶𝗻𝗴 Day 27 ✅ — When recursion makes everything elegant. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟯𝟵𝟰: Decode String (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Decode strings like "3[a2[c]]" → "accaccacc". Numbers indicate repetition, brackets show nested patterns. My first thought? Stack. But then I realized: nested brackets = 𝗿𝗲𝗰𝘂𝗿𝘀𝗶𝗼𝗻 territory. Each time I hit '[', recursively decode what's inside. When I hit ']', return to the outer level. The recursion naturally handles the nesting. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Build number from consecutive digits 👉 When '[' appears, recursively decode inner string 👉 Repeat the decoded string by the number 👉 When ']' appears, return result to caller 👉 Regular characters get appended directly Time: O(maxK × n) where maxK is max repeat count, Space: O(n) for recursion stack 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Recursion isn't just for trees and graphs—it's for any problem with 𝗻𝗲𝘀𝘁𝗲𝗱 𝘀𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲𝘀. The key: recognize when a subproblem looks exactly like the main problem. That's your recursion signal. 27 days in, and I'm getting more comfortable with recursive thinking. 𝗖𝗼𝗱𝗲: https://lnkd.in/gFfUH6bk 𝟮𝟳 𝗱𝗮𝘆𝘀 𝘀𝘁𝗿𝗮𝗶𝗴𝗵𝘁. 𝗔𝗹𝗺𝗼𝘀𝘁 𝗮 𝗺𝗼𝗻𝘁𝗵. Every problem teaches a pattern. Every pattern becomes a tool. 𝗗𝗮𝘆 𝟮𝟳/𝟭𝟬𝟬 ✅ | 𝟳𝟯 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #Recursion #StringManipulation #DataStructures #CodingInterview #TechnicalInterview #SoftwareEngineer #FAANG #ProblemSolving #Algorithms #Java #Programming #ComputerScience #DeveloperJourney #LearnInPublic #SoftwareDevelopment #CodingSkills #TechJobs #CareerGrowth
To view or add a comment, sign in
-
I solved today's LeetCode in 6 lines of C++. No binary search. No fancy algorithms. Just pure logic. Today's Problem: "Find Smallest Letter Greater Than Target" Everyone's screaming: "Use binary search! O(log n)!" Me: "Why overcomplicate?" Here's my solution that just... works: class Solution { public: char nextGreatestLetter(vector<char>& letters, char target) { for(int i=0; i<letters.size(); i++){ if((int)target < (int)letters[i]){ return letters[i]; } } return letters[0]; } }; That's it. Loop through. Find the first letter greater than target. If nothing? Return the first letter. Time Complexity: O(n) Lines of Code: 6 Mental overhead: Zero "But it's not optimal!" Sure, binary search is O(log n). But you know what else matters? → Code readability → Maintenance → Debugging speed → Actually shipping For an array of 26 letters max (the alphabet), the difference between O(n) and O(log n) is microseconds. The real optimization? Writing code your teammates can understand at 3 AM. Hot take: Sometimes "good enough" IS optimal. Do you agree? Or am I missing something? Drop your thoughts below Repost if you've been shamed for writing "simple" code #LeetCode #SoftwareEngineering #Coding #CPlusPlus #ProgrammingTips #CleanCode #DeveloperLife #TechCareers #CodingInterview #SoftwareDevelopment #CodeSimplicity #100DaysOfCode
To view or add a comment, sign in
-
-
✏️ DSA Diary Day 15/100 📚🔤: Solving LeetCode’s “Minimum ASCII Delete Sum for Two Strings” 🚀✨ Today I worked on a Dynamic Programming + String Optimization problem on LeetCode 🔥👇 👉 Minimum ASCII Delete Sum for Two Strings This problem shows how DP helps minimize deletion cost while keeping the maximum common structure between two strings 🧠💡 🔹 My Approach 🛠️🧠 I used Bottom-Up Dynamic Programming (Tabulation) 🔽👇 🔸 Step 1: Define the DP Idea 📌 Instead of directly calculating what to delete, I focused on maximizing the ASCII value of the common subsequence between both strings. The more valuable the common part is, the less we need to delete 🔁✨ 🔸 Step 2: Decision Making 🔍 At every character comparison: 1️⃣ If both characters match → Add their ASCII value to the total 2️⃣ If they don’t match → Carry forward the maximum value from previous comparisons This is similar to the Longest Common Subsequence (LCS) concept, but with weights (ASCII values) instead of length 📈 🔸 Step 3: Getting the Final Answer 🧮 We calculate the total ASCII value of both strings, then subtract twice the value of the common subsequence. This ensures: ✔ Only unnecessary characters are deleted ✔ The deletion cost is minimum 🔹 Key Learnings 📚✨ ✅ DP is powerful for string optimization problems ✅ LCS ideas can be extended with weights ✅ Tabulation avoids recursion overhead ✅ Maximizing common parts minimizes deletions ✅ Clean logic = efficient solutions 🧠💯 🔥 This problem felt like a perfect mix of: 🔤 Strings + 📊 DP + 🧮 Optimization + 🧠 Logic #LeetCode 🚀 #Java ☕ #DSA 🧠 #DynamicProgramming 📊 #ProblemSolving 💡 #CodingChallenge 💻 #100DaysOfCode 🔥 #DSADiaryByRethanya ✨ #Strings 🔤 #Tabulation 🧩 #LearnInPublic 📢 #TechJourney 🚀
To view or add a comment, sign in
-
-
# Solving the Puzzle: 0ms Backtracking There is a unique kind of adrenaline in solving a "Word Search" with perfect efficiency. I just implemented a Depth First Search (DFS) with Backtracking to navigate a 2D board and find target words. It’s a classic challenge that tests your ability to explore every possibility without getting lost in the grid. The Breakdown: * The Logic: Used a recursive DFS approach to explore adjacent cells while marking visited paths to avoid cycles. * Optimization: Implemented efficient backtracking to restore the board state, ensuring minimal overhead. * The Result: 0ms runtime, beating 100.00% of Java submissions. * Consistency: 88/88 test cases passed. In software development, we often deal with complex, interconnected data. Mastering backtracking isn't just about solving puzzles—it's about learning how to navigate decision trees efficiently in real-world applications. One grid at a time, one optimization at a time. Do you prefer iterative or recursive approaches for grid traversal? Let’s talk shop in the comments! 👇 #LeetCode #Java #Algorithms #Backtracking #SoftwareEngineering #CodingLife #Optimization
To view or add a comment, sign in
-
-
System Design – Expectation vs Reality Expectation: “Bro, it’s just one simple API" Reality: Load balancer because traffic might come🥴 Cache because the database will cry😭 Queue because async is “optional” until it’s not😌 Retry logic because networks have emotions🙂 Monitoring because production only breaks at 2 AM😵 And suddenly… system design round unlocked😫 At this point, you’re not coding — you’re negotiating with scale, failures, and future-you. Lesson learned: But design like things will scale and something will break. Still learning. Still building. Still redesigning. #SystemDesign #BackendDeveloper #DeveloperLife #SoftwareEngineering #Python #Django #FastApi
To view or add a comment, sign in
-
Claude Code recently added LSP support and it's a game-changer for developers. Before: Claude was doing fancy grep to understand your code. Burning tokens searching blindly. Now: It understands your code the SAME way IDE does. ✅ Real-time error detection after every edit ✅ Go-to-definition & find references instantly ✅ Traces call hierarchies like a pro IDE ✅ Actually UNDERSTANDS your codebase semantically The result? ~10x fewer wasted tokens. Less guessing. Fewer bugs. Smarter code generation. This is the moment AI coding stopped guessing and started truly understanding code. Enable it: ENABLE_LSP_TOOL=1 claude Supports: TypeScript, Python, Rust, Go, Java, C/C++ & more #ClaudeCode #LSP #AI #Developers #Anthropic
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