💡 Day 43 Solved "House Robber" Problem #198 using Java! Today I worked on a classic Dynamic Programming problem on LeetCode – House Robber 🏠💰 🔍 Problem: Find the maximum amount of money you can rob without robbing two adjacent houses. 🧠 Approach: At every step, we decide: ✔️ Rob current house → add value + profit from i-2 ✔️ Skip current house → take profit from i-1 We choose the maximum of these two options. 🚀 Optimization: Instead of using a DP array, I used two variables to reduce space complexity to O(1). ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem is a great example of how dynamic programming simplifies complex decisions! #LeetCode #Java #DynamicProgramming #CodingJourney #ProblemSolving
Solving House Robber Problem 198 with Java Dynamic Programming
More Relevant Posts
-
Did you know Java doesn't always crash when you divide by zero? 🤯 If you try to divide a regular whole number by zero, Java will immediately throw a red flag (an ArithmeticException) and crash your program. 🛑 But add a decimal, and the rules completely change! If you try to run 1.0 / 0.0, Java follows the global standard for floating-point math. Instead of crashing, your console will calmly print out: Infinity 🚀♾️ Why does this happen? Floating-point numbers in Java are specifically designed to handle extreme math scenarios without breaking the system. They have built-in safety nets for values like infinity or even "Not a Number" (NaN). Save this post to keep in your back pocket for your next coding interview! 💻✨ #Java #CodingTips #SoftwareDevelopment #ProgrammingLanguages #TechTrivia #DeveloperLife
To view or add a comment, sign in
-
-
Day 95 - LeetCode Journey Solved LeetCode 1700: Number of Students Unable to Eat Lunch in Java ✅ At first it looks like a simulation problem using queue + stack… but the real trick is to avoid simulation completely. Instead of rotating the queue again and again, I simply counted preferences and matched them with sandwiches. Smart counting beats brute force 💡 Key takeaways: • Avoid unnecessary simulation • Use counting instead of queue operations • Greedy thinking simplifies the problem • Writing clean and optimal logic ✅ All test cases passed ⚡ O(n) time and O(1) space Sometimes the best solution is not doing what the problem is asking literally 🔥 #LeetCode #DSA #Java #Greedy #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
Spent 15 minutes wondering why my @Value annotation wasn't injecting the property. The code: @Component public class AppConfig { @Value("${app . name}") private String appName; public AppConfig() { System.out.println(appName); } } It printed null every time. The problem: @Value injection happens after the constructor runs. In the constructor, Spring hasn't injected anything yet. The fix: Use @PostConstruct instead: @PostConstruct public void init() { System.out.println(appName); } Or use constructor injection: public AppConfig(@Value("${app . name}") String appName) { this.appName = appName; } Constructor runs first. Injection happens after. Simple timing issue. Easy to miss. #SpringBoot #Java #BackendDevelopment #Programming
To view or add a comment, sign in
-
100 Days of Coding Challenge – Day 32 📌 Problem: Subarray Product Less Than K 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window (Two Pointers) 1. Initialize two pointers (left and right) and a variable to track product 2. Expand the window by moving right and multiply elements 3. If product becomes ≥ k, shrink the window from the left 4. For each valid window, count subarrays using right - left + 1 5. Accumulate the count to get the final answer 👉 Key Insight: If a window is valid, all its subarrays ending at right are also valid Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gFWbBHeF 🔗 Code Link: https://lnkd.in/gxZF2_JA #100DaysOfCode #Day32 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #TwoPointers
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 33 🚀 📌 Problem: Longest Mountain in Array 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Peak Expansion / Two Pointers 1. Traverse the array and identify a peak element (greater than both neighbors) 2. From the peak, expand left while elements are increasing 3. Expand right while elements are decreasing 4. Calculate the length of the mountain (right - left + 1) 5. Track the maximum length across all valid peaks Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gS3Uzs3t 🔗 Code Link: https://lnkd.in/gG8gq2KU #100DaysOfCode #Day33 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #TwoPointers #Array
To view or add a comment, sign in
-
-
Day 93 - LeetCode Journey Solved LeetCode 9: Palindrome Number in Java ✅ At first glance, it feels like a string problem… but the real challenge is solving it without converting to string. Instead of reversing the whole number, I reversed only half of it and compared both parts. This avoids overflow and keeps it efficient. Smart approach > brute force 💡 Key takeaways: • Handling edge cases (negative numbers, trailing zeroes) • Reversing only half of the number • Avoiding extra space (no string conversion) • Writing optimized mathematical logic ✅ All test cases passed ⚡ O(log n) time and O(1) space Sometimes the best solutions are the simplest ones, just a different way of thinking 🔥 #LeetCode #DSA #Java #Math #ProblemSolving #CodingJourney #InterviewPrep #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
100 Days of Coding Challenge – Day 38 📌 Problem: Max Consecutive Ones III 💻 Language: Java 🔍 Platform: LeetCode Approach Used: Sliding Window (Two Pointers) 1. Use two pointers (left and right) to maintain a window 2. Count the number of zeros in the current window 3. Expand the window by moving right 4. If zeros exceed k, shrink the window from the left 5. Track the maximum window size (right - left + 1) Time Complexity: O(n) Space Complexity: O(1) 🔗 Problem Link: https://lnkd.in/gcryD9Np 🔗 Code Link: https://lnkd.in/g44uWAsX #100DaysOfCode #Day38 #Java #DSA #LeetCode #ProblemSolving #CodingJourney #SlidingWindow #TwoPointers
To view or add a comment, sign in
-
-
Crossed 90+ DSA problems on LeetCode — but focusing on something more important than just numbers. • Solving problems with complete understanding (no shortcuts) • Practicing patterns like Binary Search, Hashing, Sliding Window • Re-solving to improve recall under pressure • Applying this in real OAs and contests Recently attempted OAs involving Graphs and Dynamic Programming — a strong reminder that real performance matters more than problem count. Alongside DSA, I’m building backend systems using Java & Spring Boot to stay aligned with real-world development. Consistently working on improving problem-solving speed, clarity, and correctness. #DSA #Java #BackendDevelopment #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 3 of My Java Full Stack Journey Today was all about loops — one of the most important concepts in programming 🔁 📌 What I learned: 🔹 While Loop * Used to repeat a block of code until a condition becomes false 🔹 Syntax: while(condition) { // code } 🔹 My Progress: * Practiced 50+ questions on while loop 💪 * Learned how iteration works step-by-step 👉 Loops help reduce repetition and make code efficient Consistency is the key — small steps daily 🚀 #LearningJourney #WhileLoop #CProgramming #Consistency
To view or add a comment, sign in
-
More from this author
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