๋ฌธ์ ์์ฒด๋ ๊ฐ๋จํ๋ค, ์ฝ๋๋์ด ๋ง์์ ๋ฟ.ใ
HashMap ์๋ฃํ์ ์ด์ฉํด์,
์๋์ฐจ๊ฐ IN ์ธ ๊ฒฝ์ฐ
[์๋์ฐจ ๋ฒํธ = key, ์๋์ฐจ ์ ์ฐจ์๊ฐ = value]
๋ก ์ ์ฅํ๋ค.
์๋์ฐจ๊ฐ OUT์ธ ๊ฒฝ์ฐ
map์์ ์ ์ฐจ์๊ฐ์ ์ถ์ถํ ๋ค, ์ด์ฉ์๊ฐ์ ๊ตฌํ๋ค.
์ด์ฉ์๊ฐ์, ๋ค๋ฅธ map์ [์๋์ฐจ ๋ฒํธ = key , ์ฃผ์ฐจ์ฅ ์ด์ฉ์๊ฐ = value ] ๋ก ๊ธฐ๋กํด๋๊ณ , ์ ์ฐจ์๊ฐ์ ๊ธฐ๋กํ๋ map์ ์๋ ๋ฐ์ดํฐ๋ ์ญ์ ํ๋ค.
์ญ์ ํ๋ ์ด์ ๋, ๋์ค์ ์ถ์ฐจ ๋ด์ญ์ด ์๋ outํ์ง ๋ชปํ ์ฐจ๋ฅผ ํ์ธํ๊ธฐ ์ํด์์ด๋ค.
๊ทธ๋ ๊ฒ, ์ด์ฉ์๊ฐ์ ๋ค ๊ธฐ๋กํ ๋ค, ์ถ์ฐจํ์ง ๋ชปํ ์ฐจ๊ฐ ์๋์ง ํ์ธํ๊ณ , ์ด์ฉ์๊ฐ์ ์ถ๊ฐํ๋ค.
๊ทธ๋ฆฌ๊ณ ์ฃผ์ด์ง ์๊ธ ์ ์ฐ ๋ฐฉ๋ฒ๋๋ก ๊ณ์ฐํ ๋ค, ์๋์ฐจ ๋ฒํธ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํด์ ๋ฐํํ๋ฉด ๋์ด๋ค
// ์
์ฐจ ์ดํ ์ถ์ฐจ ๋ด์ญ์ด ์๋ค๋ฉด 23:59 ์ ์ถ์ฐจ๋๊ฒ์ผ๋ก ๊ฐ์ฃผ
// ๋์ ์ฃผ์ฐจ ์๊ฐ์ด ๊ธฐ๋ณธ ์๊ฐ ์ดํ๋ฉด ๊ธฐ๋ณธ์๊ธ
// ๋จ์์๊ฐ์ผ๋ก ๋๋์ด๋จ์ด์ง์ง ์์ผ๋ฉด ์ฌ๋ฆผ
import java.util.*;
class Solution {
HashMap<String,String> map;
HashMap<String,Integer> times;
public int[] solution(int[] fees, String[] records) {
map = new HashMap<>();
times = new HashMap<>();
for(String record : records){
String[] info = record.split(" ");
if(info[2].equals("IN")){
map.put(info[1],info[0]);
}else{
int t = getTimeDiff(map.get(info[1]),info[0]);
times.put(info[1],times.getOrDefault(info[1],0)+t);
map.remove(info[1]);
}
}
for(Map.Entry<String,String> entry: map.entrySet()){
int t = getTimeDiff(entry.getValue(),"23:59");
times.put(entry.getKey(),times.getOrDefault(entry.getKey(),0)+t);
}
int[] ans = new int[times.size()];
List<int[]> list = new ArrayList<>();
for(Map.Entry<String,Integer> entry: times.entrySet()){
list.add(new int[]{Integer.valueOf(entry.getKey()),calculate(fees,entry.getValue())});
}
Collections.sort(list,(a,b)->a[0]-b[0]);
for(int i=0;i<ans.length;i++){
ans[i]=list.get(i)[1];
}
return ans;
}
// ์ด์ฉ์๊ฐ์ ๋ฐํ์ผ๋ก ์๊ธ์ ๊ตฌํจ
int calculate(int[] fees,int time){
int result = fees[1];
time-=fees[0];
if(time<=0){
return result;
}
result += (int)Math.ceil((double)time/fees[2])*fees[3];
return result;
}
// ์๊ฐ ์ฐจ์ด๋ฅผ ๊ตฌํจ
int getTimeDiff(String time1,String time2){
return convert(time2)-convert(time1);
}
// ์๊ฐ์ ๋ถ์ผ๋ก ๋จ์ ํต์ผ
int convert(String time){
String[] info = time.split(":");
return Integer.parseInt(info[0])*60+Integer.parseInt(info[1]);
}
}