Time Needed to Buy Tickets | 3 Approaches | Leetcode 2073 | codestorywithMIK

Subscribers:
92,300
Published on ● Video Link: https://www.youtube.com/watch?v=r2LPa779amQ



Duration: 37:29
3,443 views
232


Whatsapp Community Link : https://www.whatsapp.com/channel/0029Va6kVSjICVfiVdsHgi1A
This is the 31st Video of our Playlist "Leetcode Easy: Popular Interview Problems".

In this video we will try to solve an easy but a very good practice problem : Time Needed to Buy Tickets | Leetcode 2073

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Time Needed to Buy Tickets | 3 Approaches | Leetcode 2073 | codestorywithMIK
Company Tags : Will update soon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Interview_DS_Algo/blob/master/Arrays/Leetcode%20Easy/Time%20Needed%20to%20Buy%20Tickets.cpp
Leetcode Link : https://leetcode.com/problems/time-needed-to-buy-tickets/


My DP Concepts Playlist : https://youtu.be/7eLMOE1jnls
My Graph Concepts Playlist : https://youtu.be/5JGiZnr6B5w
My Recursion Concepts Playlist : https://www.youtube.com/watch?v=pfb1Zduesi8&list=PLpIkg8OmuX-IBcXsfITH5ql0Lqci1MYPM
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Interview_DS_Algo
Instagram : https://www.instagram.com/codestorywithmik/
Facebook : https://www.facebook.com/people/codestorywithmik/100090524295846/
Twitter : https://twitter.com/CSwithMIK
Subscribe to my channel : https://www.youtube.com/@codestorywithMIK

Approach Summary :
Approach-1 - The provided C++ code implements a solution to find the time required for a person to buy all their tickets in a queue.
1. It initializes a queue (`que`) with indices representing people in the queue.
2. It iterates through the queue, simulating each person buying tickets one at a time until they have bought all of their tickets.
3. If the person at index `k` buys all their tickets, the function returns the time taken.
4. The process continues until all people have bought their tickets.
This approach efficiently tracks the passage of time and updates the queue based on the number of tickets each person still needs to purchase.

Approach-2 - The approach iterates through each person in the queue repeatedly until the targeted person (`k`) has purchased all their tickets.
Here's a summary of the approach:
1. It initializes the time counter and checks if the targeted person (`k`) only needs to buy one ticket. If so, it returns the time taken for them to buy that ticket.
2. It enters a loop that continues until the targeted person (`k`) has bought all their tickets.
3. Within each iteration of the loop, it iterates through each person in the queue. For each person:
4. If the targeted person (`k`) has not bought all their tickets after iterating through all persons, the loop continues until they do.
5. Finally, it returns the total time taken for the targeted person (`k`) to purchase all their tickets.

Approach-3 - Here's a summary of the approach:
1. It initializes the time counter to zero.
2. It iterates through each person in the queue using a loop.
3. For each person (`i`), it checks if their index is less than or equal to the index of the targeted person (`k`). If so, it adds the minimum of the number of tickets the targeted person (`k`) needs and the number of tickets the current person (`i`) has to the total time. This represents the scenario where the current person (`i`) and the targeted person (`k`) are both before or at the same position in the queue.
4. If the index of the current person (`i`) is greater than `k`, it adds the minimum of one less than the number of tickets the targeted person (`k`) needs and the number of tickets the current person (`i`) has to the total time. This accounts for the scenario where the current person (`i`) is after the targeted person (`k`) in the queue.
5. After iterating through all persons in the queue, it returns the total time taken for the targeted person (`k`) to purchase all their tickets.

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

✨ Timelines✨
00:00 - Introduction

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024




Other Videos By codestorywithMIK


2024-04-20Find All Groups of Farmland | DFS | BFS | Brute Force | Leetcode 1992 | codestorywithMIK
2024-04-18Number of Islands | DFS | BFS | Same as Island Perimeter | Leetcode 200 | codestorywithMIK
2024-04-17Island Perimeter | 3 Approaches | Google | Leetcode 463 | codestorywithMIK
2024-04-16Smallest String Starting From Leaf | DFS | BFS | Google | Leetcode 988 | codestorywithMIK
2024-04-14Edit Distance | Recursion | Memo | Bottom Up | DP On Strings | Leetcode 72 | DP Concepts & Qns-21
2024-04-13Sum of Left Leaves | 2 Approaches | META | Leetcode 404 | codestorywithMIK
2024-04-12Maximal Rectangle | Multiple Hidden Problems | Intuition | Leetcode 85 | codestorywithMIK
2024-04-12One Love = CODING ❤️👨🏻‍💻
2024-04-10Remove K Digits | Intuition | Dry Run | Leetcode 402 | codestorywithMIK
2024-04-09Reveal Cards In Increasing Order | 2 Approaches | Leetcode 950 | codestorywithMIK
2024-04-09Time Needed to Buy Tickets | 3 Approaches | Leetcode 2073 | codestorywithMIK
2024-04-07Number of Students Unable to Eat Lunch | 2 Approaches | Leetcode 1700 | codestorywithMIK
2024-04-07Minimum Cost Walk in Weighted Graph | Thought Process | DSU | Leetcode 3108 | codestorywithMIK
2024-04-07Valid Parenthesis String | 4 Detailed Approaches | Leetcode 678 | codestorywithMIK
2024-04-06Print Shortest Common Supersequence | DP On Strings | Leetcode 1092 | DP Concepts & Qns-21
2024-04-05Minimum Remove to Make Valid Parentheses | 3 Detailed Approaches | Leetcode 1249 | codestorywithMIK
2024-04-03Maximum Nesting Depth of the Parentheses | 2 Approaches | Leetcode 1614 | codestorywithMIK
2024-04-01Isomorphic Strings | Simple Thought Process | Leetcode 205| LinkedIn | codestorywithMIK
2024-03-31Length of Last Word | 2 Ways | Leetcode 58 | Amazon | codestorywithMIK
2024-03-31April, we are coming 🔥🔥🔥 #leetcode #maths #leetcodequestionandanswers #codestorywithmik
2024-03-30Count Alternating Subarrays | Super Easy | Leetcode 3031 | Weekly Contest 391 | codestorywithMIK