Lv 3. 입국심사

2024. 11. 5. 20:32·Algorithm & Data Structures/Programers

https://school.programmers.co.kr/learn/courses/30/lessons/43238

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

 

최소 시간 0 최대시간 ( 가장 오래걸리는 심사관 * 인원수)로 

특정하고 2분탐색을 통해서 시간을 찾아 내었다.

 

calculate method로 시간을 input 하면 output으로 인원수를 return 해주는 method 구현하였고

 

binarySearch method로 이분탐색 method 구현하였다.

 

binarySearch method에서 calculate method 호출하여 return 받는 인원수가 n 보다 최소한으로 클때의 순간을 포착하여 완성하였다.

 

 

 

import java.util.*;

class Solution {
    //time 들어오면 그 시간에 몇명 가능
    private long calculate(int[] times, long time){
        long sum = 0;
        for(int i = 0 ; i < times.length; i++)
            sum += time / times[i];
        return sum ;
    }
    // 시작과 끝이 정해지면 이분탐색
    private long binarySearch(int n, int[] times){
        Arrays.sort(times);
        long end = times[times.length-1] * (long)n;
        long start = 0, answer = 0;
        while(start <= end){
            long mid = (start + end) / 2;
            long cal = calculate(times,mid);
            if(cal < n){
                start = mid + 1;
            }else{
                end = mid - 1;
                answer = mid;
            }
        }
        return answer;
    }
    public long solution(int n, int[] times) {
        long answer = 0;
        return binarySearch(n, times);
    }
}

'Algorithm & Data Structures > Programers' 카테고리의 다른 글

Lv 3. 디스크 컨트롤러  (1) 2024.11.11
Lv 3. 연속 펄스 부분 수열의 합  (0) 2024.11.07
Lv 3. 여행경로  (0) 2024.11.01
Lv 3. 징검다리 건너기  (0) 2024.10.30
Lv 3. 섬연결하기  (2) 2024.10.28
'Algorithm & Data Structures/Programers' 카테고리의 다른 글
  • Lv 3. 디스크 컨트롤러
  • Lv 3. 연속 펄스 부분 수열의 합
  • Lv 3. 여행경로
  • Lv 3. 징검다리 건너기
Geisha
Geisha
개발 일기
  • Geisha
    Geisha
    Geisha
  • 전체
    오늘
    어제
    • 분류 전체보기 (343)
      • Algorithm & Data Structures (261)
        • BOJ (119)
        • SWEA (1)
        • Programers (137)
        • Data Structures (3)
      • DB (29)
        • SQL (23)
        • RDBMS (2)
      • Java (1)
        • Class (1)
      • Spring (5)
        • Spring MVC (1)
        • Annotations (1)
      • CS (36)
        • 운영체제 (13)
        • 네트워크 (5)
      • Tool (6)
        • Git (5)
        • AWS (1)
      • Project (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    이분탐색
    백트래킹
    구현
    BFS
    골드
    PriorityQueue
    SQL
    자바
    경로압축
    Dijkstra
    프로그래머스
    dp
    baekjoon
    스택
    binarySearch
    동적계획법
    algorithm
    programmers
    Union-Find
    다익스트라
    Java
    후위순회
    DynamicProgramming
    다이나믹 프로그래밍
    알고리즘
    투포인터
    dfs
    Stack
    유니온파인드
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
Geisha
Lv 3. 입국심사
상단으로

티스토리툴바