[백준Java]BOJ2002_추월

Jcy·2022년 4월 20일
0

알고리즘

목록 보기
5/8

(출처 : https://www.acmicpc.net/problem/2002)

나의 풀이 생각

입력 N이 1000이어서 어떻게 하든 시간은 될 것이라 생각했다. 내가 생각한 규칙은 각 차 마다 들어온 순서를 적어놓고 맨 처음 나온 차부터 N까지 루프를 돌면서.. if(방금 나온 차의 순서 > i번째 나온 차의 순서)면 추월한 차량이라고 생각했다.
public class BOJ2002_추월 {

	static class Car {
		String carNum;
		int idx;

		public Car(String carNum, int idx) {
			super();
			this.carNum = carNum;
			this.idx = idx;
		}
	}

	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("input.txt"));
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int ans = 0;
		HashMap<String, Integer> hash = new HashMap<>();
		Car[] start = new Car[n];
		Car[] finish = new Car[n];
        
		for (int i = 0; i < n; i++) {
			String s = sc.next();
			start[i] = new Car(s,i);
			hash.put(s, i);
		}
        
		for (int i = 0; i < n; i++) {
			String s = sc.next();
			finish[i] = new Car(s,i);
		}
        
		for (int i = 0; i < n; i++) {
			int startOrder = hash.get(finish[i].carNum);
			for(int j=i;j<n;j++) {
				if(startOrder > hash.get(finish[j].carNum)) {
					ans++;
					break;
				}
			}
		}
		System.out.println(ans);
	}
}
profile
꾸준하게라도 써보겠읍니다..

0개의 댓글

관련 채용 정보