문서 색인

2. 코드

아래 코드로는 오류가 발생하거나 타임아웃이 발생하였음. 더 좋은 코드가 있으면 공유하시기 바랍니다.

public class AirportConstruction {
  public int chooseCity(int n, int[][] city) {
    int answer = 0;
    int distance = 0;
    int minLocation = 0;
 
    for ( int inx = 0; inx < n; inx++ ) {
      int pivot = city[inx][0];
      int dist = 0;
 
      for ( int jnx = 0; jnx < n; jnx++ ) {
        if ( inx != jnx ) {
          dist += (Math.abs(city[jnx][0] - pivot) * city[jnx][1]);
        }
      }
 
      if ( inx == 0 || dist < distance || (dist == distance &amp;&amp; pivot < minLocation) ) {
        answer = pivot;
        minLocation = answer;
        distance = dist;
      }
    }
 
    return answer;
  }
 
  public static void main(String[] args) {
    AirportConstruction ac = new AirportConstruction();
    int tn = 4;
    int[][] tcity = { { 1, 5 }, { 2, 2 }, { 3, 3 } };
    System.out.println(ac.chooseCity(tn, tcity));
  }
}