파이썬 알고리즘 283번 | [백준 2002번] 추월

Yunny.Log ·2023년 1월 1일
0

Algorithm

목록 보기
288/318
post-thumbnail

283. 큰 수 A+B

1) 어떤 전략(알고리즘)으로 해결?

2) 코딩 설명

  • in_dict[나] = 들어올 때 내 앞에 있는 애들을 저장
  • out_dict[나] = 나갈 때 내 앞에 있는 애들을 저장
    => in_dict에 있던 애들이 out_dict 에 없으면 내가 걔네 추월한 것임
    => in_dict -out_dict 했는데 길이가 0 이상이라면 내가 적어도 하나 앞지른 것

<내 풀이>


import sys
from collections import defaultdict

n = int(sys.stdin.readline().rstrip())
in_dict = defaultdict()
in_set = []

out = []
out_dict = defaultdict()
out_set = []

answer = 0

for j in range(n) :
    key = sys.stdin.readline().rstrip()
    in_dict[key] = in_set.copy() # 내 앞에 있던 애들 
    in_set.append(key) 

for j in range(n) :
    key = sys.stdin.readline().rstrip()
    out.append(key)
    out_dict[key] = out_set.copy()
    out_set.append(key)

for j in range(n) :
    if( len(set(in_dict[out[j]]) - set(out_dict[out[j]])) )>0:
        answer+=1
print(answer)

< 내 틀렸던 풀이, 문제점>

(1) 7프로에서 틀림

import sys
from collections import defaultdict
n = int(sys.stdin.readline().rstrip())
in_dict = defaultdict()
out = []
answer = 0
for j in range(n) :
    key = sys.stdin.readline().rstrip()
    in_dict[key] = n-j-1 
for j in range(n) :
    key = sys.stdin.readline().rstrip()
    out.append((key, n-j-1))
for j in range(n) :
    ok, ov = out[j]
    if in_dict[ok] < ov :
        answer+=1
print(answer)


  • 생각해보니 기존에 내 뒤에 있던 차량 갯수가 (들어올 때) 나갈 때 내 뒤에 있는 차량 갯수만 비교하는 것은 의미 x )

0개의 댓글