목표 : 주문을 모두 처리하기 위해 사용되는 원두의 양을 구하기
해결방법 : 원두의 양 = 원두의 산미*(1+에스프레소 샷) 으로 원두의 양을 구할 수 있다. 이 때 n_coffee개를 만들면 원두의 산미를 *2를 한다. 단, 산미 > 10이 되면 다음 원두로 넘어가서 이를 반복한다. 하지만 주문을 모두 처리하지 못할 시 -1을 리턴해준다.
class Solution {
public int solution(int n_coffee, int[] beans, int[] orders) {
int answer = 0;
int cnt=0, idx=0, orderCnt=0;;
for(int i=0; i<orders.length; i++) {
if(cnt==n_coffee) {
beans[idx] = beans[idx]*2;
cnt=0;
}
if(beans[idx]>10) {
idx++;
if(idx==beans.length) break;
}
answer += beans[idx]*(1+orders[i]);
cnt++;
orderCnt++;
}
return orderCnt<orders.length ? -1 : answer;
}
}
목표 : 1부터 N까지의 자연수 중 3 or 7인 것의 갯수의 총 합을 구하기
해결방법 : 33, 37, 73, 777 등 하나의 숫자에 여러 개의 3 or 7이 들어올 수 있기 때문에 String 형으로 변환을 해서 숫자를 하나씩 비교해 3 or 7과 같을 때 카운팅을 해줘 구했다.
class Solution {
public int solution(int N) {
int answer = 0;
for(int i=1; i<=N; i++) {
String s = String.valueOf(i);
for(int j=0; j<s.length(); j++) {
if(s.charAt(j)=='3' || s.charAt(j)=='7') {
answer++;
}
}
}
return answer;
}
}
목표 : 반드시 2명이 함께 관람해야하는 설치물로, 관람한 인원 수를 구하기
해결방법 : 먼저 도착한 순으로 정렬 후 도착 시간이 같다면 대기할 수 있는 시간이 짧은 순으로 정렬했다.
✏️ 대기할 수 있는 시간이 지나면 지루해서 떠나버리기 때문!
list에 담고 list를 1부터 돌며 전 사람의 도착 시간+대기 시간이 현 사람의 도착 시간과 같거나 크다면 2명이서 관람을 할 수 있으므로 2명을 추가하고 이 두사람은 관람을 했기 때문에 현 사람이 전 사람으로 비교되면 안되므로 i를 한번 더 증가시켜줘서 제외시켜줬다.
import java.util.*;
class Solution {
static class Time implements Comparable<Time> {
int arrive, time;
public Time(int arrive, int time) {
this.arrive = arrive;
this.time = time;
}
@Override
public int compareTo(Time o) {
if(this.arrive==o.arrive) return this.time - o.time;
return this.arrive - o.arrive;
}
}
public int solution(int[] arrive, int[] patience) {
int answer = 0;
ArrayList<Time> list = new ArrayList<>();
for(int i=0; i<arrive.length; i++) {
list.add(new Time(arrive[i], patience[i]));
}
Collections.sort(list);
for(int i=1; i<list.size(); i++) {
if(list.get(i-1).arrive+list.get(i-1).time>=list.get(i).arrive) {
answer += 2;
i++;
}
}
return answer;
}
}