Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- database
- Stack
- Class
- 구현
- two pointers
- Tree
- geometry
- bit manipulation
- Method
- Number Theory
- sorting
- 파이썬
- 코테
- Math
- simulation
- dynamic programming
- implement
- Data Structure
- greedy
- SQL
- Binary Tree
- Counting
- Binary Search
- array
- 코딩테스트
- 자바
- string
- java
- hash table
- Matrix
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2144. Minimum Cost of Buying Candies With Discount 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 2144. Minimum Cost of Buying Candies With Discount
무지맘 2023. 6. 21. 12:381. Input
1) int[] cost
- cost[i] = i번째 사탕의 가격
2. Output
1) 모든 캔디를 사는 데 필요한 최소 비용을 반환
- 캔디를 a, b 2개 사면 3번째 사는 캔디는 무료이다.
- 이때 무료로 살 수 있는 캔디의 가격은 a와 b의 가격 이하여야 한다.
3. Constraint
1) 1 <= cost.length <= 100
2) 1 <= cost[i] <= 100
4. Example
Input: cost = [6,5,7,9,2,2] -> Output: 23
설명:
- 7짜리와 9짜리 사탕을 사고, 6짜리를 무료로 받는다.
- 5짜리와 2짜리 사탕을 사고, 2짜리를 무료로 받는다.
- 필요한 비용은 7 + 9 + 5 + 2 = 23이다.
5. Code
1) 첫 코드(2023/06/21)
class Solution {
public int minimumCost(int[] cost) {
Arrays.sort(cost);
int answer = 0;
for(int i=cost.length-1; i>=0 ; i--){
if((cost.length-i)%3!=0)
answer += cost[i];
}
return answer;
}
}
- 97%, 75%
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2231. Largest Number After Digit Swaps by Parity (0) | 2023.06.21 |
---|---|
[LeetCode/Easy] 2164. Sort Even and Odd Indices Independently (0) | 2023.06.21 |
[LeetCode/Easy] 2085. Count Common Words With One Occurrence (0) | 2023.06.21 |
[LeetCode/Easy] 1971. Find if Path Exists in Graph (0) | 2023.06.21 |
[LeetCode/Easy] 1957. Delete Characters to Make Fancy String (0) | 2023.06.21 |