Skip to content

Keys and Rooms #1

@Jinsujin

Description

@Jinsujin

문제

There are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.

When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.

Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true if you can visit all the rooms, or false otherwise.

  • 0~ n-1 로 표시된 n 개의 룸. 0번룸 뺴고는 모두 잠겨있다. ⇒ 0번방부터 탐색 시작
  • 모든 룸을 방문해야 한다. 키 없이는 잠겨진 방에 방문할 수 없다.
  • 각 방에 방문시, 열쇠 뭉치(distict keys) 를 찾을 수도 있다. 열쇠에는 번호가 적혀있고 해당 방을 열 수 있다. (열쇠뭉치는 모두 가져갈 수 있고 여러번 사용 가능. 중복된 열쇠를 방들에서 획득할 수 있음)
  • 모든 룸을 방문할 수 있다면 true, 아니면 false 를 반환하라.

제약조건

  • n == rooms.length
  • 2 <= n <= 1000
    • 1000 은 10^3
    • O(n^2) 에 1000 을 넣으면 $10^6$
    • $10^6$$10^8$(1초 기준) 보다 작다 ⇒ O(n^2) 알고리즘으로 풀 수 있다!
  • 0 <= rooms[i].length <= 1000
  • 1 <= sum(rooms[i].length) <= 3000
    • 방 i 의 키 최대 개수 3000 개
  • 0 <= rooms[i][j] < n
  • All the values of rooms[i] are unique.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions