-
코테 연습[자바 코테 준비 7일차]카테고리 없음 2023. 7. 1. 14:29
오늘 풀 문제
1. 카카오 2022 기출 주차장 문제
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
예상 시간 20분 / 실제 풀이 : IDE + Math 사용 , 자료 구조 검색 , 1h 30 m
간단한 구현 이지만, 피지컬을 요한다.
로직이 어렵거나 복잡한거는 없다 . 자료구조 문제이다 .
연습이 필요하다라고 느낀점이 , 디버깅을 돌리면서 확인 하는 사항들이 많았는데 시간이
부족한 프로그래머스 환경에서는 불가능 할 것 같다.
놓친 것은
1. 반올림 여부
2.코드가 길어지니 나오는 실수 ( 변수명의 구분 불가) 등이 있다.
배운거
시간이 "01:11" 로 주어지면 , 빠르게 ":" 기준 split 후 , Integer.parseInt 를 과감하게 하자.
숫자로 잘 바꿔준다.
TreeMap : 키값을 오름 차순으로 정렬 해서 가진다.
ex ] 0000 -> 0148 -> ,,
"0000" -> parseInteger : 0
----------------------------------------------------------------------------------------------------------------------
1번 주차장 문제
@Test void sol0() { String feesGiven = "[180, 5000, 10, 600]"; String recordsGiven = "[05:34 5961 IN, 06:00 0000 IN, 06:34 0000 OUT, 07:59 5961 OUT, 07:59 0148 IN, 18:59 0000 IN," + " 19:09 0148 OUT, 22:59 5961 IN, 23:00 5961 OUT]"; int[] fees = pr.parseIntegerList(feesGiven); recordsGiven = recordsGiven.replace("[", " ").replace("]", ","); String[] recordsSplit = recordsGiven.split(","); String answerGiven = "[14600, 34400, 5000]"; int[] ans = pr.parseIntegerList(answerGiven); for(int i = 0 ; i< recordsSplit.length;i++) { recordsSplit[i] = recordsSplit[i].trim(); } assertThat(ans).containsExactly(solution0(fees, recordsSplit)); } public int[] solution0(int[] fees, String[] records) { System.out.println(Arrays.toString(records)); // [ 05:34 5961 IN, 06:00 0000 IN, 06:34 0000 OUT, 07:59 5961 OUT, 07:59 0148 IN, 18:59 0000 IN, 19:09 0148 OUT, 22:59 5961 IN, 23:00 5961 OUT] // carNumber : time ,state , stackTime TreeMap<Integer,List<String>> carRecord = new TreeMap<>(); for(String record : records) { String[] info = record.split(" "); Integer carNum = Integer.parseInt(info[1]); if(!carRecord.containsKey(carNum)) { List<String> infoPut = new ArrayList<>(); infoPut.add(info[0]); infoPut.add(info[2]); infoPut.add("0"); carRecord.put(carNum,infoPut); }else{ // info : 05:34 5961 IN List<String> recorded = carRecord.get(carNum); String time = recorded.get(0); String state = recorded.get(1); int stackTime = Integer.parseInt(recorded.get(2)); if(state.equals("IN")) { String gt = info[0]; String[] gtl = gt.split(":"); int h = Integer.parseInt(gtl[0]); int m = Integer.parseInt(gtl[1]); int givenTime = h*60 + m; String[] tl = time.split(":"); h = Integer.parseInt(tl[0]); m = Integer.parseInt(tl[1]); stackTime += givenTime - (h*60 + m); List<String> infoPut = new ArrayList<>(); infoPut.add(info[0]); infoPut.add(info[2]); infoPut.add(Integer.toString(stackTime)); carRecord.put(carNum,infoPut); }else{ List<String> infoPut = new ArrayList<>(); infoPut.add(info[0]); infoPut.add(info[2]); infoPut.add(Integer.toString(stackTime)); carRecord.put(carNum,infoPut); } } } String time = "23:59"; String[] tl = time.split(":"); int h = Integer.parseInt(tl[0]); int m = Integer.parseInt(tl[1]); int maxTime = (h*60 + m); // TreeMap<Integer,List<String>> carRecord = new TreeMap<>(); // info : time , state , valuetime // crRecord : keu :info for (Integer key : carRecord.keySet()) { List<String> info = carRecord.get(key); if(info.get(1).equals("IN")){ List<String> infoPut = new ArrayList<>(); infoPut.add(info.get(0)); infoPut.add("OUT"); int stored = Integer.parseInt(info.get(2)); String[] gt = info.get(0).split(":"); int hl = Integer.parseInt(gt[0]); int ml = Integer.parseInt(gt[1]); int tt = (hl*60 + ml); stored += (maxTime - tt); infoPut.add(Integer.toString(stored)); carRecord.put(key,infoPut); } } // carRecord : // carNumber : time ,state , stackTime int[] answer = new int[carRecord.size()]; int index = 0; //fees : [180, 5000, 10, 600] : 기본 분 , 기본 요금 , 단위 분 , 단위 금 int baseT = fees[0]; int baseM = fees[1]; int unitT = fees[2]; int unitM = fees[3]; for(List<String> info :carRecord.values()){ if(Integer.parseInt(info.get(2)) <= baseT) { answer[index] = baseM; } else { int tmpT = (Integer.parseInt(info.get(2)) - baseT); int t = (int) Math.ceil((double) tmpT / unitT); answer[index] = baseM + ((t) * unitM); } index++; } return answer; }