Skip to content

Commit 5ee97df

Browse files
authored
Added task 2073.
1 parent 2703d35 commit 5ee97df

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g2001_2100.s2073_time_needed_to_buy_tickets;
2+
3+
// #Easy #Array #Simulation #Queue #2022_05_27_Time_0_ms_(100.00%)_Space_41.9_MB_(45.92%)
4+
5+
public class Solution {
6+
public int timeRequiredToBuy(int[] tickets, int k) {
7+
int res = 0;
8+
for (int i = 0; i < tickets.length; i++) {
9+
if (i <= k) {
10+
res += Math.min(tickets[k], tickets[i]);
11+
} else {
12+
res += Math.min(tickets[k] - 1, tickets[i]);
13+
}
14+
}
15+
return res;
16+
}
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2073\. Time Needed to Buy Tickets
2+
3+
Easy
4+
5+
There are `n` people in a line queuing to buy tickets, where the <code>0<sup>th</sup></code> person is at the **front** of the line and the <code>(n - 1)<sup>th</sup></code> person is at the **back** of the line.
6+
7+
You are given a **0-indexed** integer array `tickets` of length `n` where the number of tickets that the <code>i<sup>th</sup></code> person would like to buy is `tickets[i]`.
8+
9+
Each person takes **exactly 1 second** to buy a ticket. A person can only buy **1 ticket at a time** and has to go back to **the end** of the line (which happens **instantaneously**) in order to buy more tickets. If a person does not have any tickets left to buy, the person will **leave** the line.
10+
11+
Return _the **time taken** for the person at position_ `k`**_(0-indexed)_ **_to finish buying tickets_.
12+
13+
**Example 1:**
14+
15+
**Input:** tickets = [2,3,2], k = 2
16+
17+
**Output:** 6
18+
19+
**Explanation:**
20+
21+
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
22+
23+
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
24+
25+
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
26+
27+
**Example 2:**
28+
29+
**Input:** tickets = [5,1,1,1], k = 0
30+
31+
**Output:** 8
32+
33+
**Explanation:**
34+
35+
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
36+
37+
- In the next 4 passes, only the person in position 0 is buying tickets.
38+
39+
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
40+
41+
**Constraints:**
42+
43+
* `n == tickets.length`
44+
* `1 <= n <= 100`
45+
* `1 <= tickets[i] <= 100`
46+
* `0 <= k < n`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2001_2100.s2073_time_needed_to_buy_tickets;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void timeRequiredToBuy() {
11+
assertThat(new Solution().timeRequiredToBuy(new int[] {2, 3, 2}, 2), equalTo(6));
12+
}
13+
14+
@Test
15+
void timeRequiredToBuy2() {
16+
assertThat(new Solution().timeRequiredToBuy(new int[] {5, 1, 1, 1}, 0), equalTo(8));
17+
}
18+
}

0 commit comments

Comments
 (0)