Skip to content

Commit 6e61696

Browse files
solves #2706: buy two chocolates in java
1 parent 05b144f commit 6e61696

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Diff for: README.md renamed to README.txt

+22-1
Original file line numberDiff line numberDiff line change
@@ -822,4 +822,25 @@
822822
| 2660 | [Determine the Winner of a Bowling Game](https://leetcode.com/problems/determine-the-winner-of-a-bowling-game) | [![Java](assets/java.png)](src/DetermineTheWinnerOfABowlingGame.java) | |
823823
| 2670 | [Find the Distinct Difference Array](https://leetcode.com/problems/find-the-distinct-difference-array) | [![Java](assets/java.png)](src/FindTheDistinctDifferenceArray.java) | |
824824
| 2678 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens) | [![Java](assets/java.png)](src/NumberOfSeniorCitizens.java) | |
825-
| 268 | [Number of Senior Citizens](https://leetcode.com/problems/number-of-senior-citizens) | [![Java](assets/java.png)](src/NumberOfSeniorCitizens.java) | |
825+
| 2682 | [Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game) | [![Java](assets/java.png)](src/FindTheLosersOfTheCircularGame.java) | |
826+
| 2696 | [Minimum String Length After Removing Substrings](https://leetcode.com/problems/minimum-string-length-after-removing-substrings) | [![Java](assets/java.png)](src/MinimumStringLengthAfterRemovingSubstrings.java) | |
827+
| 2697 | [Lexicographically Smallest Palindrome](https://leetcode.com/problems/lexicographically-smallest-palindrome) | [![Java](assets/java.png)](src/LexicographicallySmallestPalindrome.java) | |
828+
| 2706 | [Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) | [![Java](assets/java.png)](src/BuyTwoChocolates.java) | |
829+
| 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string) | | |
830+
| 2716 | [Minimize String Length](https://leetcode.com/problems/minimize-string-length) | | |
831+
| 2717 | [Semi-Ordered Permutation](https://leetcode.com/problems/semi-ordered-permutation) | | |
832+
| 2728 | [Count Houses in a Circular Street](https://leetcode.com/problems/count-houses-in-a-circular-street) | | |
833+
| 2729 | [Check if The Number is Fascinating](https://leetcode.com/problems/check-if-the-number-is-fascinating) | | |
834+
| 2733 | [Neither Minimum nor Maximum](https://leetcode.com/problems/neither-minimum-nor-maximum) | | |
835+
| 2739 | [Total Distance Traveled](https://leetcode.com/problems/total-distance-traveled) | | |
836+
| 2744 | [Find Maximum Number of String Pairs](https://leetcode.com/problems/find-maximum-number-of-string-pairs) | | |
837+
| 2748 | [Number of Beautiful Pairs](https://leetcode.com/problems/number-of-beautiful-pairs) | | |
838+
| 2760 | [Longest Even Odd Subarray With Threshold](https://leetcode.com/problems/longest-even-odd-subarray-with-threshold) | | |
839+
| 2765 | [Longest Alternating Subarray](https://leetcode.com/problems/longest-alternating-subarray) | | |
840+
| 2769 | [Find the Maximum Achievable Number](https://leetcode.com/problems/find-the-maximum-achievable-number) | | |
841+
| 2778 | [Sum of Squares of Special Elements](https://leetcode.com/problems/sum-of-squares-of-special-elements) | | |
842+
| 2784 | [Check if Array is Good](https://leetcode.com/problems/check-if-array-is-good) | | |
843+
| 2788 | [Split Strings by Separator](https://leetcode.com/problems/split-strings-by-separator) | | |
844+
| 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target) | | |
845+
| 2806 | [Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase) | | |
846+
| 2810 | [Faulty Keyboard](https://leetcode.com/problems/faulty-keyboard) | | |

Diff for: src/BuyTwoChocolates.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/buy-two-chocolates
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class BuyTwoChocolates {
6+
public int buyChoco(int[] prices, int money) {
7+
int minimum = Integer.MAX_VALUE, secondMinimum = Integer.MAX_VALUE;
8+
for (int price : prices) {
9+
if (price <= minimum) {
10+
secondMinimum = minimum;
11+
minimum = price;
12+
} else if (price < secondMinimum) {
13+
secondMinimum = price;
14+
}
15+
}
16+
if (minimum + secondMinimum > money) return money;
17+
return money - (minimum + secondMinimum);
18+
}
19+
}

0 commit comments

Comments
 (0)