Home > AI > Data Structure > Graph >

leetcode-1436-Destination City

Problem link: https://leetcode.com/problems/destination-city/

Solutions:

class Solution {
    var tmp: [String: Int] = [:]
    
    func destCity(_ paths: [[String]]) -> String {
        for p in paths {

            if tmp[p[0]] != nil {
                tmp[p[0]] = 0 // middle // since there is no loop, we can do so
            } else {
                tmp[p[0]] = 1 // start
            }
            
            if tmp[p[1]] != nil {
                tmp[p[1]] = 0 // middle
            } else {
                tmp[p[1]] = 2 // end
            }
        }
        for k in tmp.keys {
            if tmp[k] == 2 {
                return k
            }
        }
        return "Not found"
    }
}


let s = Solution()
let r = s.destCity([["B","C"],["D","B"],["C","A"]])
print(r)

Relevant tags:

Leave a Reply