알고리즘/프로그래머스

LEVEL 3 : [1차] 추석 트래픽

webmaster 2021. 11. 24. 21:13
728x90

https://programmers.co.kr/learn/courses/30/lessons/17676

 

코딩테스트 연습 - [1차] 추석 트래픽

입력: [ "2016-09-15 20:59:57.421 0.351s", "2016-09-15 20:59:58.233 1.181s", "2016-09-15 20:59:58.299 0.8s", "2016-09-15 20:59:58.688 1.041s", "2016-09-15 20:59:59.591 1.412s", "2016-09-15 21:00:00.464 1.466s", "2016-09-15 21:00:00.741 1.581s", "2016-09-1

programmers.co.kr

 

  • 문제
    • 문제
    • 예시1
    • 예시2

풀이

  • 로그 시간을 INT 형식으로 바꾸는 것이 핵심인 문제이다.
  • Fn_init 함수
    • 시 분 초 밀리세컨드 형식을 로그를 통해 각각의 밀리 세컨드로 바꿔준다.
    • 시 = 시간 * 60 * 60 * 1000
    • 분 = 분 * 60 * 1000
    • 초 = 초 * 1000
    • 밀리세컨드 = 밀리세컨드 * 1000 
    • 시작 시간 = 시+분+초+밀리 세컨드 - 프로그램 동작 시간 + 1
    • 종료 시간 = 시+분+초+밀리 세컨드 
  • 이중 for문을 돈다.
    • 섹션을 시작 위치 = (start 시간, end시간) , 섹션을 종료 위치 = (섹션을 시작 위치 + 1000(1초)) 까지 범위를 설정 후 for문을 돌며, 체크
      • 경우 1 : 섹션을 시작 위치 <= j의 시작시간 && j의 시작시간 <= 섹션을 종료 위치 
        • 노란색 경우
           
        • count값을 더해준다.
      • 경우 2 : 섹션을 시작위치 <= j의 종료시간 && j의 종료시간 <= 섹션을 종료 위치
        • 노란색 경우
        • count값을 더해준다.
      • 경우 3 : j의 시작시간 <= 섹션을 시작 위치 && 섹션을 종료 위치 <= j의 종료시간
        • 노란색 경우
           
        • count값을 더해준다.
    • Count값이 가장 클 때를 리턴한다.
  • public static int solution(String[] lines) {
    	int answer = 0;
        int[] startOfTimes = new int[lines.length];//시작 시간 +1 값을 담을 배열
        int[] endOfTimes = new int[lines.length]; //종료 시간 값을 담을 배욜
        fn_init(lines,startOfTimes,endOfTimes); //시작시간, 종료시간 배열에 담기
        for(int i=0;i<lines.length;++i) {
          int startSection = startOfTimes[i]; //시작시간
          int endSection = startSection + 1000;
          int count = 0;
          for(int j=0;j<lines.length;++j) {
          	if(startSection <= startOfTimes[j] && startOfTimes[j] < endSection) {//시작 시간 [j]가 시작 섹션 보다 크고 종료센션보다 작다면
            	count++;
            }else if(sta11111tion <= endOfTimes[j] && endOfTimes[j] < endSection) {//시작 섹션이 종료세션보다 작고 종료 시간이 종료 섹션보다 작을 경우
            	count++;
           	else if(startOfTimes[j] <= startSection && endSection <= endOfTimes[j]) {//시작 섹션보다 종료시작이 앞에 있고 종료 섹션보다 작업 죵료시간이 길 경우
            	count++;
            }
          }
          answer = Math.max(answer, count);
       }
       for(int i=0;i<lines.length;++i) {
       	int startSection = endOfTimes[i];//종료시간
       	int endSection = startSection + 1000;
       	int count = 0;
       	for(int j=0;j<lines.length;++j) {
       		if(startSection <= startOfTimes[j] && startOfTimes[j] < endSection) {
       			count++;
       		}else if(startSection <= endOfTimes[j] && endOfTimes[j] < endSection) {
      			 count++;
            }else if(startOfTimes[j] <= startSection && endSection <= endOfTimes[j]) {
       			count++;
       		}
       }
       answer = Math.max(answer, count);
       }
       return answer;
        }
    	private static void fn_init(String[] lines, int[] startOfTimes, int[] endOfTimes) {
    		for(int i=0;i<lines.length;++i) {
    			String[] times = lines[i].split(" ");
    			int endTime = 0;
    			String[] splitTime = times[1].split(":");
    			endTime += Integer.parseInt(splitTime[0]) * 60 * 60 * 1000;//시 * 60 * 60 * 1000
    			endTime += Integer.parseInt(splitTime[1]) * 60 *  1000;//분 * 60 * 1000
    			endTime += (int)(Double.parseDouble(splitTime[2]) * 1000);//초 * 1000
    			int processTime = (int)(Double.parseDouble(times[2].substring(0,times[2].length()-1)) * 1000);
                //프로세스 진행시간 - 
    			int startTime = endTime - processTime + 1;//시작 시간 + 0.001초
    			startOfTimes[i] = startTime;
    			endOfTimes[i] = endTime;//종료시간
    		}
    	}
728x90

'알고리즘 > 프로그래머스' 카테고리의 다른 글

LEVEL 3 : 리틀 프렌즈 사천성  (0) 2021.11.25
LEVEL 3 : 디스크컨트롤러  (0) 2021.11.25
LEVEL 3 : 등굣길  (0) 2021.11.23
LEVEL 3 : N으로 표현  (0) 2021.11.23
LEVEL 3 : 가장 먼 노드  (0) 2021.11.22