[Programmers] #181837 - 커피 심부름 [Java][C++][Python]
[Programmers] #181837 - 커피 심부름 [Java][C++][Python]
1. 아이디어
아메리카노와 카페 라테만 판매하는 카페에 주문을 넣을 때, 최종 금액을 구하는 문제다. 가능한 케이스가 "iceamericano", "americanoice", "hotamericano", "americanohot", "icecafelatte", "cafelatteice", "hotcafelatte", "cafelattehot", "americano", "cafelatte", "anything" 총 11가지인데 메뉴명에 "cafelatte"가 들어가지 않으면 전부 아메리카노 가격, 들어가면 전부 카페 라테 가격이라는 점에서 두 가지 케이스로만 분류할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = order의 길이. 각 문자열은 11가지 고정 형식 중 하나라 길이가 상수로 유계이므로, 부분 문자열 탐색 비용이 order의 길이와 무관하게 상수다)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
class Solution {
public int solution(String[] order) {
int sum = 0;
for (String s : order) {
sum += s.contains("cafelatte") ? 5000 : 4500;
}
return sum;
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
int solution(vector<string> order) {
int sum = 0;
for (string& s : order) {
sum += s.find("cafelatte") != -1 ? 5000 : 4500;
}
return sum;
}
1
2
def solution(order):
return sum(5000 if "cafelatte" in s else 4500 for s in order)
This post is licensed under CC BY 4.0 by the author.