Time Needed to Buy Tickets - Leetcode 2073 - Python

  Рет қаралды 14,243

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 27
@yang5843
@yang5843 6 ай бұрын
The problem had a solution that was more optimal than the simulation so I knew you were going to make a video. Here's the java solution class Solution { public int timeRequiredToBuy(int[] tickets, int k) { int rc = 0; for (int i=0;i
@yuriaragao1910
@yuriaragao1910 6 ай бұрын
Pretty neat solution! I solved it with a different logic, maybe yours is more straightforward, but I will share here anyways: The idea is to start res as the length of the queue multiplied by the number of tickets the kth person wants to buy (this is the upper bound of the result), then we traverse the tickets array decreasing res checking two conditions: 1. If tickets[i] is smaller than tickets[k] then decrease res by their difference (we counted tickets[i] too many times) 2. if i > k and tickets[i] >= tickets[k] decrease the result by 1, since the ith person is behind the kth person in the queue Here is the solution in Python: class Solution: def timeRequiredToBuy(self, tickets: List[int], k: int) -> int: res = len(tickets) * tickets[k] for i in range(len(tickets)): if tickets[i] < tickets[k]: res += tickets[i] - tickets[k] elif i > k: res -= 1 return res
@Logeshwar-s7m
@Logeshwar-s7m 6 ай бұрын
please make a playlist for leetcode database problems
@dilmurodabdusamadov5572
@dilmurodabdusamadov5572 6 ай бұрын
I tried to come up with mathy approach but it didn't fit well :) class Solution: def timeRequiredToBuy(self, tickets: List[int], k: int) -> int: more, less = 0, 0 for i in range(len(tickets)): if tickets[i] < tickets[k]: less += tickets[i] else: more += 1 return tickets[k] * more + less Thank you for the explanation tho!
@DebopriyoBasu
@DebopriyoBasu 6 ай бұрын
Great explanation! Here is my JavaScript solution using the same logic: const timeRequiredToBuy = (tickets, k) => { let time = 0; const len = tickets.length; for (let i = 0; i < len; i++) { if (i
@CrabGuyy
@CrabGuyy 6 ай бұрын
amazing, in javascript you could also do it in a more functional way with the "reduce" function of arrays, if you didn't know i would suggest you look into it since its pretty useful for this case
@rauffares3558
@rauffares3558 6 ай бұрын
Great Explanation
@AbhijeetMuneshwar
@AbhijeetMuneshwar 6 ай бұрын
Thanks for this easy explaination 🙏🏼
@indianundergraddiaries
@indianundergraddiaries 6 ай бұрын
Here's my Python code, I feel this maybe more intuitive and straight forward class Solution: def timeRequiredToBuy(self, tickets: List[int], k: int) -> int: count = 0 while True: for i in range(len(tickets)): if tickets[k] == 0: return count elif tickets[i] == 0: continue tickets[i] -= 1 count += 1
@MP-ny3ep
@MP-ny3ep 6 ай бұрын
Great explanation as always. Thank you
@deepakkanshetti1413
@deepakkanshetti1413 18 күн бұрын
Really neat solution 👍
@adrianharo6586
@adrianharo6586 6 ай бұрын
I just did this on Java. Using a while and nested for loop
@comatosesperrow
@comatosesperrow 6 ай бұрын
I feel like there's a O(1) math solution somewhere here. Any ideas?
@deathbombs
@deathbombs 6 ай бұрын
I solved 450 qs, couldn't optimize past simulation. Have tips or im just dumb?
@yang5843
@yang5843 6 ай бұрын
Reflect on the problem, every person in front of k has to be able to buy student[k] tickets, and every person after k must be able to buy student[k] - 1 tickets.
@s8x.
@s8x. 6 ай бұрын
how did u learn everything
@spsc07
@spsc07 6 ай бұрын
someone already said life lessons and hashmap
@s8x.
@s8x. 6 ай бұрын
@@spsc07 neet
@MaazMalik-c9o
@MaazMalik-c9o 6 ай бұрын
Here is my solution in java : class Solution { public int timeRequiredToBuy(int[] tickets, int k) { int l=tickets.length; int val=tickets[k]; int time=0; for(int i=0;i
@dragon_id_
@dragon_id_ 6 ай бұрын
did every1 use "nums" instead of tickets at first !!
@servantofthelord8147
@servantofthelord8147 4 ай бұрын
same lol
@ayushw
@ayushw 6 ай бұрын
JS One-liner: var timeRequiredToBuy = function(tickets, k) { return tickets.reduce((acc, cur, i) => acc + Math.min(cur, tickets[k] - (i > k)), 0); };
Largest Local Values in a Matrix - Leetcode 2373 - Python
7:37
Score After Flipping Matrix - Leetcode 861 - Python
22:30
NeetCodeIO
Рет қаралды 10 М.
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 18 МЛН
Who’s the Real Dad Doll Squid? Can You Guess in 60 Seconds? | Roblox 3D
00:34
Я сделала самое маленькое в мире мороженое!
00:43
Кушать Хочу
Рет қаралды 4,3 МЛН
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 180 М.
950. Reveal Cards In Increasing Order | Queue | Simulation
14:27
Aryan Mittal
Рет қаралды 7 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 315 М.
Python Programming Fundamentals | Python Loops
20:23
Dan Kornas
Рет қаралды 298
How I Failed the Google Coding Interview (and lessons I learned)
14:24
Rabin Karp - Shortest Palindrome - Leetcode 214
22:07
NeetCodeIO
Рет қаралды 16 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 174 М.
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 355 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 514 М.
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 18 МЛН