Day 56/100 – LeetCode Challenge Problem: Minimum Cost For Tickets Today I solved the “Minimum Cost For Tickets” problem, which is a dynamic programming problem centered around making optimal decisions over a timeline. The goal is to minimize the total cost of travel tickets by choosing between 1-day, 7-day, and 30-day passes. Instead of making decisions greedily, I approached this by building a DP array where each day represents the minimum cost required up to that point. For each travel day, I considered three possibilities: buying a 1-day pass, a 7-day pass, or a 30-day pass. I then chose the minimum among these options by looking back into previously computed states. For non-travel days, I simply carried forward the previous cost, avoiding unnecessary purchases. This approach ensures that every decision is backed by previously computed optimal results, making the solution both efficient and reliable. The solution runs in O(n) time with O(n) space complexity. This problem reinforced how dynamic programming helps in breaking down decisions over time and choosing the most cost-effective path by evaluating all possible options. Fifty-six days in. The focus is now on making smarter decisions through structured thinking. #100DaysOfLeetCode #Java #DSA #DynamicProgramming #ProblemSolving #Consistency
Minimum Cost For Tickets LeetCode Challenge
More Relevant Posts
-
🚀 #100DaysOfCode – Day 16 Today’s focus: House Robber Problem (Dynamic Programming) 🏠💰 At first glance, the problem feels simple — just pick non-adjacent houses to maximize money. But the real challenge is choosing the optimal combination, not just alternating elements. 🔴 Brute Force Approach Try every possible combination of robbing or skipping houses. class Solution { public int rob(int[] nums) { return helper(nums, 0); } private int helper(int[] nums, int i) { if (i >= nums.length) return 0; int pick = nums[i] + helper(nums, i + 2); int notPick = helper(nums, i + 1); return Math.max(pick, notPick); } } ⛔ Time Complexity: O(2^n) (Exponential) 👉 Not efficient for large inputs 🟢 Optimized Approach (Dynamic Programming) Instead of recalculating, store previous results and build the solution. class Solution { public int rob(int[] nums) { int prev2 = 0; int prev1 = nums[0]; for (int i = 1; i < nums.length; i++) { int pick = nums[i] + prev2; int notPick = prev1; int curr = Math.max(pick, notPick); prev2 = prev1; prev1 = curr; } return prev1; } } ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) 🧠 Key Insight At every house, you have two choices: Rob it → add value + skip previous house Skip it → take previous maximum 👉 dp[i] = max(nums[i] + dp[i-2], dp[i-1]) 💡 Learning of the Day Dynamic Programming is all about: Breaking problems into smaller subproblems Storing results to avoid recomputation Making optimal decisions at each step #Day16 #100DaysOfCode #DSA #Java #DynamicProgramming #CodingJourney #LeetCode #ProblemSolving
To view or add a comment, sign in
-
Day 55/100 – LeetCode Challenge Problem: Triangle Today I solved the “Triangle” problem, which is a classic dynamic programming question focused on finding the minimum path sum from top to bottom. Instead of starting from the top and exploring all possible paths, I approached this problem from the bottom. I initialized a DP array with the values of the last row of the triangle. Then, I moved upward row by row, updating each element by adding the minimum of the two adjacent values from the row below. This bottom-up approach reduces the problem size at each step and avoids unnecessary recomputation. By the time I reached the top, the first element of the DP array represented the minimum path sum. This method is efficient because it reuses space and avoids building a full 2D DP table. The solution runs in O(n²) time with O(n) space complexity. This problem reinforced how changing the direction of thinking — bottom-up instead of top-down — can simplify dynamic programming problems significantly. Fifty-five days in. The focus is now on writing optimal solutions with better space efficiency. #100DaysOfLeetCode #Java #DSA #DynamicProgramming #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
LeetCode Challenge – Day 48 Today I solved the House Robber problem. Problem Insight: Given an array where each element represents money in a house, the goal is to find the maximum amount that can be robbed without robbing two adjacent houses. Approach: At each house, there are two choices: Rob the current house and add its value to the maximum from two houses back Skip the current house and take the maximum till the previous house Used a dynamic programming approach to make this decision efficiently Key Learning: Dynamic Programming becomes easier when we break the problem into small decisions. At each step, we only need to decide whether to pick or skip the current element. Complexity: Time: O(n) Space: O(1) This problem helped me understand how to make optimal decisions using previous results. #LeetCode #Java #DSA #CodingJourney
To view or add a comment, sign in
-
Find the minimum distance between a given start index and any occurrence of a target in the array. Leetcode problem link: https://lnkd.in/gZFkirHw 🔍 Key Takeaways: Used a two-pointer approach to scan from both ends. Kept updating the minimum absolute distance from start. Included the condition left != right to avoid checking the same element twice. ✅ Why this works: Instead of scanning only from one side, this approach checks both ends in each iteration, making the logic structured and efficient. 📘 What I like about this solution: Simple and readable Avoids redundant checks Great example of combining two pointers with distance calculation #Java #LeetCode #DSA #Programming #SoftwareEngineering #Developers #loveToCode
To view or add a comment, sign in
-
-
I recently encountered an ambiguous mapping error while working on a REST API. The stack trace was overwhelming, but the message was straightforward : I had two different methods competing for the same URL and HTTP verb. The issue happened because I had two methods, createEmployee and createNewEmployee, both annotated with @PostMapping, along with @RequestMapping on the controller class. Spring couldn't determine which method to use to handle the request. To resolve this, it's essential to keep the endpoints unique or utilize different HTTP methods to maintain organization. Happy coding! #Java #SpringBoot #Programming #SoftwareEngineering #Debugging
To view or add a comment, sign in
-
🚀 LeetCode Challenge 18/50 💡 Approach: Space-Optimized Dynamic Programming Recursion without memoization gives O(2ⁿ) — exponential! A full DP array gives O(n) space. But here's the trick: at every house we only ever look back TWO steps. So why store the entire array? 🔍 Key Insight: → At each house: either ROB it (prev2 + current) or SKIP it (prev1) → Take the maximum of both choices → Slide two variables forward — that's all we need! → No array, no extra memory 📈 Complexity: ❌ Brute Force → O(2ⁿ) Time ❌ DP Array → O(n) Time, O(n) Space ✅ Space-Optimized DP → O(n) Time, O(1) Space The best code isn't just correct — it's efficient. Sometimes the smartest move is knowing what NOT to store! 🧠 #LeetCode #DSA #DynamicProgramming #Java #ADA #PBL2 #LeetCodeChallenge #Day18of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #HouseRobber
To view or add a comment, sign in
-
-
🚀 Day 559 of #750DaysOfCode 🚀 🔍 LeetCode 3741: Minimum Distance Between Three Equal Elements II Today’s problem was an extension of yesterday’s question — but with larger constraints (n up to 1e5), making efficiency crucial ⚡ 💡 Problem Recap: Find three indices (i, j, k) such that: nums[i] == nums[j] == nums[k] Distance = |i - j| + |j - k| + |k - i| is minimized ✨ Approach I Used (Clean & Intuitive): ✔ Stored indices of each number using a HashMap ✔ For each number: If it appears ≥ 3 times Check consecutive triplets of indices ✔ Compute distance using: 👉 |i - j| + |j - k| + |k - i| 💻 Key Code Insight: Instead of checking all combinations, I only checked sliding windows of size 3 within index lists — reducing unnecessary work. 🧠 Learning: Even in medium problems, a simple structured approach (grouping + sliding window) can pass efficiently when applied correctly. ⚡ Complexity: Time: O(n) to build map + O(n) traversal Space: O(n) 💬 Takeaway: Don’t overcomplicate — sometimes the same idea from an easy problem scales well with just a small optimization in thinking. #LeetCode #DSA #Java #CodingJourney #ProblemSolving #Tech #Programming #Developers #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode Challenge – Day 49 Today I solved the Linked List Cycle problem. Problem Insight: Given a linked list, determine whether it contains a cycle (loop). A cycle occurs when a node points back to a previous node instead of reaching null. Approach: Used Floyd’s Cycle Detection Algorithm (Tortoise and Hare) Maintained two pointers: Slow pointer moves one step at a time Fast pointer moves two steps at a time If a cycle exists, both pointers will eventually meet If not, the fast pointer will reach the end of the list Key Learning: Using two pointers with different speeds helps efficiently detect cycles without extra space. Complexity: Time: O(n) Space: O(1) Understanding pointer movement made this problem much easier to solve. #LeetCode #Java #DSA #CodingJourney
To view or add a comment, sign in
-
🚀 Day 90 — LeetCode Practice 🚀 Today’s focus was on bit manipulation and pattern recognition. ✅ Problem Solved: 🔹 Counting Bits 💡 Key Learnings from Today: • Learned how to count set bits (1s) in binary representation • Strengthened understanding of division by 2 and modulo operations • Explored how numbers behave in binary format • Improved problem-solving using iterative approach • Practiced writing clean and efficient logic 📌 Approach Used: Iterated through each number from 0 to n and counted the number of 1s in its binary form using a loop. 🎯 Next Step: Will optimize this using Dynamic Programming (DP) for better performance. Day by day improving consistency and logic 💪 #Day90 #LeetCode #CodingJourney #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 3/50 💡 Approach: Two Pointer (In-Place) The straightforward way uses an extra array — but the problem specifically says no copying! So I used the Two Pointer technique to solve it in-place with a single pass. 🔍 Key Insight: → Use a 'left' pointer to track where the next non-zero element belongs → Traverse with 'right' pointer — place non-zeros at left, then increment → Fill remaining positions with 0s at the end 📈 Complexity: ✅ Time: O(n) — single pass ✅ Space: O(1) — no extra array, truly in-place Sometimes the constraint IS the optimization. Working within limits pushes us to think smarter! 🧠 #LeetCode #DSA #TwoPointer #Java #ADA #PBL2 #LeetCodeChallenge #Day3of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #MoveZeroes
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