Keys and Rooms

Sett·2021년 8월 8일
0

문제

https://leetcode.com/problems/keys-and-rooms/

문제 접근

  1. 이게 왜 dfs, bfs야..
  2. 그냥 배열있는거 다 들어가보면 되는 거 아닌가?
  3. 일단 다 들어가 봄. 들어갈 때마다 FLAG 세워서 표시할까 하다가...
  4. flag 세울 때 별로 좋은 방법이 안 떠오르는 거임.. 들어간 방을 순차적으로 정렬해서 1,2,3,4가 다 있는지 따지거나 뭐 그런거 밖에 안 떠올라서,
  5. 보니까 방의 개수 비교하는 굉장한 방법이 있더라.

소스 코드

func canVisitAllRooms(_ rooms: [[Int]]) -> Bool {
        var findedRooms: [Int] = []
        findRoom(rooms: rooms, roomNumber: 0, findedRooms: &findedRooms)
        return findedRooms.count == rooms.count
    }
    
    func findRoom(rooms: [[Int]], roomNumber: Int, findedRooms: inout [Int]) {
        findedRooms.append(roomNumber)
        for i in 0..<rooms[roomNumber].count {
            if !findedRooms.contains(rooms[roomNumber][i]) {
                findRoom(rooms: rooms, roomNumber: rooms[roomNumber][i], findedRooms: &findedRooms)
            }
        }
    }
profile
안녕하세요

0개의 댓글

관련 채용 정보