Skip to content

Commit 11dbb09

Browse files
solves time needed to buy tickets
1 parent cc08eab commit 11dbb09

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@
492492
| 2057 | [Smallest Index With Equal Value](https://leetcode.com/problems/smallest-index-with-equal-value) | [![Java](assets/java.png)](src/SmallestIndexWithEqualValue.java) | |
493493
| 2062 | [Count Vowel Substrings of a String](https://leetcode.com/problems/count-vowel-substrings-of-a-string) | [![Java](assets/java.png)](src/CountVowelSubstringsOfAString.java) | |
494494
| 2068 | [Check Whether Two Strings are Almost Equivalent](https://leetcode.com/problems/check-whether-two-strings-are-almost-equivalent) | [![Java](assets/java.png)](src/CheckWhetherTwoStringsAreAlmostEquivalent.java) | |
495-
| 2073 | [Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets) | | |
495+
| 2073 | [Time Needed to Buy Tickets](https://leetcode.com/problems/time-needed-to-buy-tickets) | [![Java](assets/java.png)](src/TimeNeededToBuyTickets.java) | |
496496
| 2078 | [Two Furthest Houses With Different Colors](https://leetcode.com/problems/two-furthest-houses-with-different-colors) | | |
497497
| 2085 | [Count Common Words With One Occurrence](https://leetcode.com/problems/count-common-words-with-one-occurrence) | | |
498498
| 2089 | [Find Target Indices After Sorting Array](https://leetcode.com/problems/find-target-indices-after-sorting-array) | | |

Diff for: src/TimeNeededToBuyTickets.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// https://leetcode.com/problems/time-needed-to-buy-tickets
2+
// T: O(|tickets|)
3+
// S: O(1)
4+
5+
public class TimeNeededToBuyTickets {
6+
public int timeRequiredToBuy(int[] tickets, int k) {
7+
int time = 0;
8+
for (int i = 0 ; i < k ; i++) time += Math.min(tickets[i], tickets[k]);
9+
for (int i = k + 1 ; i < tickets.length ; i++) time += Math.min(tickets[i], tickets[k] - 1);
10+
return time + tickets[k];
11+
}
12+
}

0 commit comments

Comments
 (0)