Finally implemented the 0/1 Knapsack problem using Dynamic Programming in Java 🧠💻 While revisiting classic algorithms, I decided to go old school with Java — and this time, I went deep into the Knapsack problem. It’s fascinating how such a seemingly simple problem teaches you so much about: Optimal substructure Overlapping subproblems And most importantly, how dynamic programming can turn exponential recursion into an elegant bottom-up solution. Watching those arrays fill up and tracing back the solution always gives that satisfying “it finally clicked” moment. 😅 Sometimes, revisiting the basics is the best way to sharpen your fundamentals. Have you ever gone back to re-implement classic DSA problems after years of working in real-world projects? #Java #DynamicProgramming #Algorithms #DSA #ProblemSolving #Knapsack
Solved 0/1 Knapsack problem using DP in Java
More Relevant Posts
-
Day 23 of #50DaysOfCode – Java 💻 Today’s challenge was to check whether a number is a Perfect Number. A Perfect Number is a number that is equal to the sum of all its positive divisors (excluding itself). Example: 6 → 1 + 2 + 3 = 6 28 → 1 + 2 + 4 + 7 + 14 = 28 This problem helped me strengthen my understanding of loops, conditions, and mathematical logic 🔍✨ #Java #CodingChallenge #50DaysOfCode #LearnToCode #ProgrammingBasics #LogicBuilding #CodeDaily #ProblemSolving
To view or add a comment, sign in
-
🚀 Day 109: Today I Learned About Abstraction in Java Today I explored one of the core pillars of Object-Oriented Programming — Abstraction. 🔍 What is Abstraction? Abstraction means showing only the essential details and hiding the complex internal logic. Just like how we use a phone without knowing how the internal circuits work — in programming, abstraction helps us focus on what something does, not how it does it. 💡 Why is it useful? Makes code cleaner and easier to understand Hides unnecessary complexity Improves security by exposing only required features Helps in building scalable and maintainable systems 🧩 In Java: We achieve abstraction using: Abstract Classes Interfaces ✨ Learning abstraction helped me understand how real-world systems hide complexity and provide simple interfaces for users. Excited to dive deeper into OOP concepts! 🔥 #Java #OOPs #Abstraction #LearningInPublic #100DaysOfCode #Day109 #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 27 of 100 Days of LeetCode 📘 Problem: Generate Parentheses 💻 Language: Java ✅ Status: Accepted — Runtime: ⚡ 0 ms (Beats 100%) Today’s challenge focused on backtracking, one of the most elegant and powerful problem-solving techniques in DSA. The task was to generate all possible valid combinations of parentheses — a perfect test of recursion and logical constraints 🧩 ✨ Key Learnings: Backtracking is about exploring all possibilities, then undoing choices when needed 🔄 Understanding base cases clearly makes recursion much easier Clean recursive structures lead to elegant and efficient solutions This problem reinforced one important principle — 🧠 “Sometimes, going back is the only way to move forward — both in code and in life.” #Day27 #100DaysOfCode #LeetCode #Java #Backtracking #Recursion #ProblemSolving #CodingJourney #DSA #SoftwareDevelopment #KeepLearning #CodeEveryday
To view or add a comment, sign in
-
-
🚀 DSA Progress Update: Solved “House Robber” Problem in Java! 🏠💰 Today, I tackled another classic Dynamic Programming challenge — the House Robber problem — a beautiful blend of logic, recursion, and optimization. 🔹 Problem Statement: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money, but adjacent houses are connected with security systems — robbing two neighboring houses triggers the alarm! 👉 The goal: Find the maximum money you can rob without alerting the police. 🔹 Approach Used: Used Top-Down Dynamic Programming (Memoization) to optimize recursion. At each step, you face two choices — 1️⃣ Rob the current house and skip the next one. 2️⃣ Skip the current house and consider the next. By caching results for each index, redundant recalculations are avoided, achieving O(n) time complexity. 🔹 Connection to 0/1 Knapsack: This problem is conceptually a special case of the 0/1 Knapsack problem 🎒 Both involve binary choices (take or skip) to maximize a total value under specific constraints. In Knapsack, the constraint is total weight capacity. In House Robber, the constraint is adjacency — you can’t rob two neighboring houses. That’s why the House Robber problem is often called the “linear version” of Knapsack, showcasing the same decision-making pattern in a simplified setup. 🔹 Key Learnings: Recognized the “include vs. exclude” pattern — the foundation of many DP problems. Learned how memoization can turn exponential recursion into linear-time efficiency. Strengthened my understanding of optimal substructure and state transitions in DP. ✨ This problem was a great reminder that Dynamic Programming is not about memorizing formulas — it’s about recognizing patterns of choice and optimization. 💡 #DSA #DynamicProgramming #Java #ProblemSolving #Knapsack #HouseRobber #LeetCode #CodingJourney #SpringBoot #TechJourney #DailyLearning #CrackTheCode #GrowthMindset
To view or add a comment, sign in
-
-
29/30 days ✅ Solved a LeetCode Problem #231 Power of Two — Bit Manipulation Logic in Java Solved the Power of Two problem on LeetCode! The challenge is to determine whether a given integer n is a power of two. The key insight lies in understanding the binary representation of numbers that are powers of two — they contain exactly one set bit (1). So, when we perform the operation n & (n - 1), it turns off the only set bit of n. For any power of two, this result becomes 0. Therefore, the condition n > 0 && (n & (n - 1)) == 0 accurately checks if a number is a power of two. This simple yet powerful bitwise trick is both efficient and elegant — a great reminder of how understanding binary operations can make problem-solving in programming much faster! ⚡ #Java #LeetCode #ProblemSolving #BitManipulation #CodingJourney #LearningEveryday
To view or add a comment, sign in
-
-
Day 4 of my #100DaysOfDSA Challenge 💻 Today I solved a classic problem — Reverse of a String in Java. It’s one of the most basic yet fundamental questions that helps you understand how strings work in Java (immutable vs mutable). Here are 3 simple approaches I learned today 👇 🔹 Using loop (beginner-friendly) 🔹 Using StringBuilder (efficient way) 🔹 Using recursion (interview-level concept) Small steps every day → Big results soon 🚀 #Java #DSA #CodingChallenge #100DaysOfCode #Programming #DeveloperJourney #Learning
To view or add a comment, sign in
-
-
🚀 LeetCode Day 3: Reverse Integer Today’s challenge was all about reversing the digits of an integer while handling tricky edge cases — especially negative numbers and integer overflow. 🧠 💡 Problem: Given a signed 32-bit integer, reverse its digits. If the reversed integer overflows, return 0. 🧩 Key Takeaways: Extract digits using modulo and division Manage negative numbers gracefully Check for overflow before building the reversed number 💻 Language: Java ✅ Topic: Math / Integer Manipulation Every day is a new step toward mastering problem-solving and improving coding logic. #LeetCode #100DaysOfCode #Java #CodingChallenge #ProblemSolving #LeetCodeJourney
To view or add a comment, sign in
-
-
Why does 1 == 1 return true but 128 == 128 return false in Java? 🤔 Last week, I stumbled across this little Java “paradox” again — and it reminded me how deep even the simplest lines of code can go. I broke it down in my latest article — explaining what really happens under the hood with Integer caching and autoboxing. 👉 Check it out here: https://lnkd.in/ef2Zxadr #Java #Programming #Coding #SoftwareEngineering #JavaInternals
To view or add a comment, sign in
-
🚀 Day 53 | DSA with Java Today, I solved the Aggressive Cows Problem — another classic example of Binary Search on Answers. 🐄🏠 --- 🔹 Problem Statement: Given n stalls at different positions and c cows: Place the cows in stalls so that the minimum distance between any two cows is maximized. Find the largest minimum distance possible. Example: Input: stalls = [1,2,4,8,9], c = 3 Output: 3 ✅ (Place cows at positions 1, 4, 8) --- ⚙️ Approach – Binary Search on Answers 1. Sort the stalls array. 2. Search in the distance range [1, max(stalls) - min(stalls)]. 3. For each mid distance, check if it’s possible to place all cows maintaining at least mid distance. 4. Adjust the search space based on feasibility: If possible → try larger distance Else → try smaller distance Time Complexity: O(n × log(maxDistance)) Space Complexity: O(1) --- 🧠 Learning Outcomes: ✅ Practiced Binary Search on Answers in a spatial context ✅ Strengthened logical thinking with feasibility functions ✅ Similar patterns as Book Allocation and Painters Partition --- #DSA #Java #BinarySearch #AggressiveCows #ProblemSolving #Coding #Programming #100DaysOfCode #GitHub #LogicBuilding
To view or add a comment, sign in
-
-
Day 19 of #100DaysOfLeetCode 💻 Today’s challenge was about finding two unique numbers in an array where every other number appears twice. At first, I tried using an ArrayList — adding numbers if they weren’t present and removing them if they already existed. The logic worked, but I ran into type conversion issues (Object cannot be converted to int). That’s when I learned the importance of using generics in Java (ArrayList<Integer> instead of raw ArrayList). Small syntax details, but they make all the difference! 🔍 Key takeaways: Always specify the data type in collections. Understand how remove() behaves differently for index vs value. Even a brute-force approach can teach valuable debugging lessons. #Day19 #LeetCode #Java #CodingJourney #100DaysOfCode #Debugging #ProblemSolving
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