개발자 톡

연습문제 톡 [HSAT 7회 정기 코딩 인증평가 기출] 자동차 테스트

시간초과 문제 질문드립니다.

등록일
2023-11-03 13:15:17
조회수
640
작성자
resilient

아래 코드에서 시간초과를 해결하지 못한 거 같은데 어디를 수정하면 좋을까요?


input = sys.stdin.readline


n,q = map(int,input().split())


data = list(map(int,input().split()))
data.sort()
for _ in range(q):
  num = int(input())
  start = 0
  end = n-1
  left = 0
  right = 0
  ans = 0
  while start <= end:
    if start == end:
      if data[start] == num:
        ans = left * right
      break
    if data[start] < num:
      left += 1
      start += 1
    if data[end] > num:
      right += 1
      end -= 1


  print(ans)
  # if num not in set(data):
  #   print(0)
  # else:
  #   idx = bisect.bisect_left(data,num)
  #   if n != idx and data[idx] == num:
  #     print(idx * (n-idx-1))
  #   else:
  #     print(0)
  
#[hsat_7회_정기_코딩_인증평가_기출]_자동차_테스트
#pyhon

이 카테고리의 톡 더보기