🚀 Daily DSA Practice – Day 58 | Dynamic Programming – Best Time to Buy and Sell Stock II (Java) Continuing my DSA journey, today I solved an extension of the stock trading problem where multiple transactions are allowed. 📌 Problem Solved (LeetCode): 122. Best Time to Buy and Sell Stock II (Medium) 🎯 Concept: Greedy + Dynamic Programming 🧠 Problem Idea: Unlike the previous stock problem where only one transaction was allowed, here we can perform multiple buy-sell transactions. The key idea is to capture every profitable price difference between consecutive days. Logic if (prices[i] > prices[i-1]) profit += prices[i] - prices[i-1]; This means we buy at the previous day and sell today whenever there is profit. 🔍 What I Practiced: ✔ Understanding multiple transaction stock problems ✔ Applying greedy optimization techniques ✔ Improving array traversal efficiency ✔ Strengthening the foundation for advanced stock DP problems This problem is an important step toward more complex stock trading problems such as: • Stock with Cooldown • Stock with Transaction Fee • Stock with Limited Transactions #DSA #LeetCode #DynamicProgramming #Java #ProblemSolving #InterviewPreparation #Consistency #StockProblems
Best Time to Buy and Sell Stock II Java Solution
More Relevant Posts
-
🚀 Daily DSA Practice – Day 59 | Dynamic Programming – Best Time to Buy and Sell Stock with Cooldown (Java) Continuing my DSA journey, today I solved a more advanced variation of the stock trading problem where an additional constraint is introduced. 📌 Problem Solved (LeetCode): 309. Best Time to Buy and Sell Stock with Cooldown (Medium) 🎯 Concept: Dynamic Programming with State Transitions 🧠 Problem Idea: You can buy and sell stocks multiple times, but after selling a stock, you must wait one day (cooldown) before buying again. To solve this, we track three possible states: • Buy State → Maximum profit when holding a stock • Sell State → Profit after selling the stock • Cooldown State → Rest period after selling DP State Idea buy[i] = max(buy[i-1], cooldown[i-1] - price[i]) sell[i] = buy[i-1] + price[i] cooldown[i] = max(cooldown[i-1], sell[i-1]) 🔍 What I Practiced: ✔ Understanding state-based dynamic programming ✔ Managing multiple DP states simultaneously ✔ Handling cooldown constraints in trading problems ✔ Strengthening logic for complex transition problems This problem helped me understand how DP can model real-world scenarios with multiple decision states. #DSA #LeetCode #DynamicProgramming #Java #ProblemSolving #InterviewPreparation #Consistency #StockProblems
To view or add a comment, sign in
-
🚀 Daily DSA Practice – Day 62 | Dynamic Programming – Coin Change II (Java) Continuing my Dynamic Programming journey, today I solved a problem focused on counting combinations instead of minimizing values. 📌 Problem Solved (LeetCode): 518. Coin Change II (Medium) 🎯 Concept: Unbounded Knapsack (Count of Ways) 🧠 Problem Idea: Given an amount and coin denominations, find the number of combinations that make up that amount. Unlike Coin Change I, where we minimize coins, here we count total ways. DP Relation dp[j] = dp[j] + dp[j - coin] 🔍 What I Practiced: ✔ Difference between Minimization DP vs Counting DP ✔ Applying Unbounded Knapsack pattern ✔ Using 1D DP optimization ✔ Understanding order-independent combinations This problem helped me clearly understand how DP problems can change significantly based on whether we count ways or optimize values. #DSA #LeetCode #DynamicProgramming #Java #Knapsack #ProblemSolving #InterviewPreparation #Consistency
To view or add a comment, sign in
-
Day 94/100 – LeetCode Challenge ✅ Problem: #139 Word Break Difficulty: Medium Language: Java Approach: Dynamic Programming with Max Length Optimization Time Complexity: O(n × m × k) where n = string length, m = max word length, k = avg word length comparison Space Complexity: O(n) Key Insight: dp[i] = true if substring s[0:i) can be segmented into dictionary words. Check all possible word endings ending at position i. Optimize by limiting check to maximum word length in dictionary. Solution Brief: Initialized dp[0] = true (empty string can be segmented). Found maximum word length in dictionary for pruning. For each position i, checked substrings ending at i with length ≤ max_len. If dp[j] is true and substring s[j:i) is in dictionary, set dp[i] = true. Return dp[n]. #LeetCode #Day94 #100DaysOfCode #DynamicProgramming #Java #Algorithm #CodingChallenge #ProblemSolving #WordBreak #MediumProblem #String #DP #Optimization #DSA
To view or add a comment, sign in
-
-
Why I still value my early C projects. 🛠️ High-level frameworks like Java Collections may be fast, efficient and powerful, but understanding the foundations is what makes a developer truly versatile. Back in 2022-23, I spent weeks building out core DSA models: Stacks, Queues, Graphs, and Dictionaries - using nothing but C. I decided to polish the README and make this personal archive a public repo. What’s inside: 🔹 Modular C logic (Models, Headers, and Runners). 🔹 Manual implementation of BST, Stack, Queue & Graph. 🔹 A straightforward look at memory and structure. It’s not over-engineered or complex; it’s just honest, foundational logic. If you’re practicing for interviews or just want to see how these structures look without the abstraction, feel free to explore and share your suggestions! Repo: https://lnkd.in/d9uspV6Z #Programming #ComputerScience #DSA #C
To view or add a comment, sign in
-
Most developers get this wrong. Do you? 🛑 Early in my Java journey, I thought inheritance was a feature to use freely. The more you extended, the more reusable the code, right? Wrong. Uncontrolled inheritance creates hierarchies nobody can reason about. And Java had no clean answer for it — until sealed classes. ━━━━━━━━━━━━━━━━━━━━━━━━━ Sealed Classes, introduced in Java 17, let you define exactly which classes are permitted to extend yours. No surprises. No rogue subclasses. Just clean, intentional design. public sealed class Shape permits Circle, Rectangle, Triangle {} Each permitted subclass must be one of: → final — no further extension → sealed — further restricted with its own permits → non-sealed — open again by choice And the bonus? Exhaustive pattern matching with switch. The compiler knows every possible subtype. You get compile-time safety for free. ━━━━━━━━━━━━━━━━━━━━━━━━━ I’ve written a full breakdown — how sealed classes work, the rules, and real use cases. Read it here 👇 https://lnkd.in/gsN6Dp7j Have you used sealed classes in your projects? I’d love to hear how! #Java #Java17 #Coding #SoftwareDevelopment #TechCommunity #LearningInPublic #SealedClasses #BackendDevelopment #SoftwareEngineering #CleanCode #OOP
To view or add a comment, sign in
-
-
Day 4 of my DSA journey Today’s focus was on Dynamic Programming and Linked Lists. Key learnings: • Longest Common Subsequence (LCS), Longest Palindromic Subsequence (LPS), and Longest Repeating Subsequence (LRS) • Understood how breaking problems into smaller subproblems and storing results reduces time complexity significantly. • Learned the transition from recursion to dynamic programming for optimal solutions. • Explored Linked Lists and practiced problems involving traversal and pointer manipulation. • Understood the slow and fast pointer technique to detect cycles efficiently. Big takeaway: The real improvement is not just solving problems, but understanding patterns and optimizing solutions. #DSA #100DaysOfCode #Java #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
🚀 Just implemented the Factory Method Design Pattern in Java! In this project, I focused on understanding how to decouple object creation from business logic by applying the Factory Method pattern. Instead of directly instantiating objects, I used a factory approach to make the code more flexible, scalable, and easier to maintain. 📌 What I learned: How Factory Method improves code structure The importance of abstraction in OOP Writing cleaner and more reusable code This is part of my journey as a Computer Engineering student to strengthen my software design and backend development skills. 💡 Always open to feedback and learning! 🔗 GitHub: https://lnkd.in/dMYjh8gT #Java #DesignPatterns #SoftwareEngineering #Backend #OOP
To view or add a comment, sign in
-
🔥 𝗗𝗮𝘆 𝟵𝟴/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟬. 𝗖𝗹𝗶𝗺𝗯𝗶𝗻𝗴 𝗦𝘁𝗮𝗶𝗿𝘀 | 🟢 Easy | Java The gateway problem to Dynamic Programming — and one of the most iconic problems in DSA. 🪜 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 You can climb 1 or 2 steps at a time. How many distinct ways can you reach the top of n stairs? 💡 𝗧𝗵𝗲 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 To reach step i, you either came from step i-1 (1 step) or step i-2 (2 steps). So ways(i) = ways(i-1) + ways(i-2) Sound familiar? It's literally the Fibonacci sequence. 🐇 ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗼𝘁𝘁𝗼𝗺-𝗨𝗽 𝗗𝗣 ✅ Base cases: dp[1] = 1, dp[2] = 2 ✅ Fill dp[i] = dp[i-1] + dp[i-2] for i from 3 to n ✅ Return dp[n] No recursion. No stack overflow. No repeated subproblems. Just a clean bottom-up table. 🧠 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(n) — single pass 📦 Space: O(n) — can be further optimised to O(1) with just two variables This problem teaches the most important DP lesson: identify overlapping subproblems, store results, build up from the base. Every hard DP problem starts with this same thinking. 𝗔𝗻𝗱 𝘆𝗲𝘀 — 𝗷𝘂𝘀𝘁 𝗹𝗶𝗸𝗲 𝘁𝗵𝗲𝘀𝗲 𝘀𝘁𝗮𝗶𝗿𝘀, 𝘁𝗵𝗶𝘀 𝗰𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝘄𝗮𝘀 𝗰𝗹𝗶𝗺𝗯𝗲𝗱 𝗼𝗻𝗲 𝗱𝗮𝘆 𝗮𝘁 𝗮 𝘁𝗶𝗺𝗲. 🏔️ 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gS3DX_YW 𝗝𝘂𝘀𝘁 𝟮 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝘀𝘂𝗺𝗺𝗶𝘁 𝗮𝘄𝗮𝗶𝘁𝘀! 🏁 #LeetCode #Day98of100 #100DaysOfCode #Java #DSA #DynamicProgramming #Fibonacci #CodingChallenge #Programming
To view or add a comment, sign in
-
𝗟𝗲𝘁'𝘀 𝗱𝗶𝘀𝗰𝘂𝘀𝘀 𝗝𝗘𝗣𝟱𝟮𝟲. 𝗙𝗿𝗼𝗺 𝗦𝘁𝗮𝗯𝗹𝗲𝗩𝗮𝗹𝘂𝗲 𝘁𝗼 𝗟𝗮𝘇𝘆𝗖𝗼𝗻𝘀𝘁𝗮𝗻𝘁: 𝗮 𝗰𝗵𝗮𝗻𝗴𝗲, 𝗮 𝗯𝗶𝗴 𝗶𝗺𝗽𝗿𝗼𝘃𝗲𝗺𝗲𝗻𝘁 𝗶𝗻 𝘂𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱𝗶𝗻𝗴 I really like the new name LazyConstant in JEP 526. LazyConstant is a name that actually shows how we use LazyConstant. That is important. 𝗠𝘆 𝗼𝗯𝘀𝗲𝗿𝘃𝗮𝘁𝗶𝗼𝗻 𝗳𝗿𝗼𝗺 𝗲𝘅𝗽𝗲𝗿𝗶𝗺𝗲𝗻𝘁𝗶𝗻𝗴 𝘄𝗶𝘁𝗵 𝗶𝘁 Before when we made a constant field in a class it was always initialized when the class was loaded. This means that: ● Even if we never use the value ● Even if you're just running a quick test ● If initializing the constant is expensive Our class loading time still takes a hit. I tried this out with a demo using: java -verbose:class DemoApp And I can see how initialization affects the time it takes to load the class. 𝗪𝗵𝗮𝘁 𝗶𝘀 𝗱𝗶𝗳𝗳𝗲𝗿𝗲𝗻𝘁 𝘄𝗶𝘁𝗵 𝗟𝗮𝘇𝘆𝗖𝗼𝗻𝘀𝘁𝗮𝗻𝘁? Now the constant LazyConstant: ● Only loads when we actually need it ● Does not slow down the class loading ● Still keeps the guarantee that it will not change The result is that our program starts up more smoothly and quickly. This is especially important for: ● Microservices (where the time it takes to start up matters a lot) ● Applications with a lot of configuration ● Logging or creating objects 𝗪𝗵𝘆 𝘁𝗵𝗶𝘀 𝗶𝘀 𝗮 𝗯𝗶𝗴 𝗱𝗲𝗮𝗹 𝗶𝗻 𝗿𝗲𝗮𝗹 𝗹𝗶𝗳𝗲: Before LazyConstant we could do lazy initialization but: ● It required a lot of code ● We had to be careful with concurrency ● Often used double-checked locking + synchronized ● It was easy to get it wrong 𝘄𝗶𝘁𝗵 𝗟𝗮𝘇𝘆𝗖𝗼𝗻𝘀𝘁𝗮𝗻𝘁? ● We do not need any extra code patterns ● We do not have to worry about concurrency ● Our code is clean and easy to read 𝗪𝗵𝗲𝗿𝗲 𝗜 𝘁𝗵𝗶𝗻𝗸 𝗟𝗮𝘇𝘆𝗖𝗼𝗻𝘀𝘁𝗮𝗻𝘁 𝘄𝗶𝗹𝗹 𝗵𝗮𝘃𝗲 𝘁𝗵𝗲 𝗶𝗺𝗽𝗮𝗰𝘁: ● The part of the JVM that loads classes ● Linking phase performance ● The overall time it takes to start up It is still a preview feature in JDK 26. Lazyconstant feels like one of those features that will quietly improve a lot of real-world systems. JEP 526: https://lnkd.in/gSVBkfQy #java #programming #engineering #jep #jvm
To view or add a comment, sign in
-
-
Why Spring Is Moving Back Toward Simplicity Reactive programming became popular because: - Threads were expensive - Blocking didn’t scale That led to code like this: Mono<User> user = service.getUser() .flatMap(this::validate) .flatMap(this::enrich); Powerful — but hard to: - Read - Debug - Maintain Virtual threads allow this again: User user = service.getUser(); validate(user); enrich(user); Same scalability. Much clearer flow. 💡 Takeaway: Spring and Java are converging toward readability. #Java #SpringBoot #BackendEngineering #Java25
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