[Swift]LeetCode1042. 不鄰接植花 | Flower Planting With No Adjacent

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-wibaxtcz-mc.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

You have N gardens, labelled 1 to N.  In each garden, you want to plant one of 4 types of flowers.node

paths[i] = [x, y] describes the existence of a bidirectional path from garden x to garden y.git

Also, there is no garden that has more than 3 paths coming into or leaving it.github

Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.數組

Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)-th garden.  The flower types are denoted 123, or 4.  It is guaranteed an answer exists.微信

Example 1:app

Input: N = 3, paths = [[1,2],[2,3],[3,1]] Output: [1,2,3] 

Example 2:spa

Input: N = 4, paths = [[1,2],[3,4]] Output: [1,2,1,2] 

Example 3:code

Input: N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]] Output: [1,2,3,4]

Note:htm

  • 1 <= N <= 10000
  • 0 <= paths.size <= 20000
  • No garden has 4 or more paths coming into or leaving it.
  • It is guaranteed an answer exists.

有 N 個花園,按從 1 到 N 標記。在每一個花園中,你打算種下四種花之一。

paths[i] = [x, y] 描述了花園 x 到花園 y 的雙向路徑。

另外,沒有花園有 3 條以上的路徑能夠進入或者離開。

你須要爲每一個花園選擇一種花,使得經過路徑相連的任何兩個花園中的花的種類互不相同。

以數組形式返回選擇的方案做爲答案 answer,其中 answer[i] 爲在第 (i+1) 個花園中種植的花的種類。花的種類用  1, 2, 3, 4 表示。保證存在答案。

示例 1:

輸入:N = 3, paths = [[1,2],[2,3],[3,1]]
輸出:[1,2,3]

示例 2:

輸入:N = 4, paths = [[1,2],[3,4]]
輸出:[1,2,1,2]

示例 3:

輸入:N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
輸出:[1,2,3,4]

提示:

  • 1 <= N <= 10000
  • 0 <= paths.size <= 20000
  • 不存在花園有 4 條或者更多路徑能夠進入或離開。
  • 保證存在答案。

Runtime: 528 ms
Memory Usage: 22.3 MB
 1 class Solution {
 2     func gardenNoAdj(_ N: Int, _ paths: [[Int]]) -> [Int] {
 3         var G:[[Int]] = [[Int]](repeating: [Int](), count: N)
 4         for v in paths
 5         {
 6             let x:Int = v[0] - 1
 7             let y:Int = v[1] - 1
 8             G[x].append(y)
 9             G[y].append(x)
10         }
11         var res:[Int] = [Int](repeating: 0, count: N)
12         for v in 0..<N
13         {
14             var ss:Set<Int> = Set<Int>()
15             for u in G[v]
16             {
17                 ss.insert(res[u])
18             }
19             res[v] = 1
20             while(ss.contains(res[v]))
21             {
22                 res[v] += 1
23             }
24         }
25         return res
26     }
27 }

624ms
 1 class Solution {
 2     func gardenNoAdj(_ N: Int, _ paths: [[Int]]) -> [Int] {
 3         var g = [Int: Set<Int>]()
 4         for p in paths {
 5             g[p[0]-1, default: Set<Int>()].insert(p[1]-1)
 6             g[p[1]-1, default: Set<Int>()].insert(p[0]-1)
 7         }
 8         guard g.count > 1 else { return [Int](repeating: 1, count: N) }
 9         var ans = [Int](repeating: -1, count: N)
10         for c in 0..<N {
11             if ans[c] == -1 {
12                 ans[c] = 1
13                 recursive(g, c, &ans)
14             }
15         }
16         
17         for i in 0..<ans.endIndex {
18             if ans[i] == -1 {
19                 ans[i] = 1
20             }
21         }
22         
23         return ans
24     }
25     
26     private func recursive(_ paths: [Int: Set<Int>], _ cur: Int, _ ans: inout [Int]) {
27         guard paths[cur] != nil else { return }
28         for dest in paths[cur]!.sorted(by: {paths[$0]?.count ?? 0 > paths[$1]?.count ?? 0}) {
29             // print("cur: \(cur), dest: \(dest), color: \(ans)")
30             if ans[dest] == -1 {
31                 ans[dest] = selectColor(paths, dest, ans)
32             }
33         }
34     }
35     
36     private func selectColor(_ paths: [Int: Set<Int>], _ g: Int, _ ans: [Int]) -> Int {
37         var colors = Set<Int>([1, 2, 3, 4])
38         for s in paths[g]! {
39             if ans[s] != -1 {
40                 colors.remove(ans[s])
41             }
42         }
43         return colors.first!
44     }
45 }

708ms

 1 class Solution {
 2     func gardenNoAdj(_ N: Int, _ paths: [[Int]]) -> [Int] {
 3         var edges = [Int: [Int]]()
 4         
 5         for path in paths {
 6             let a = path[0]
 7             let b = path[1]
 8             
 9             if edges[a] == nil {
10                 edges[a] = [b]
11             } else {
12                 edges[a]?.append(b)
13             }
14             
15             if edges[b] == nil {
16                 edges[b] = [a]
17             } else {
18                 edges[b]?.append(a)
19             }
20         }
21         
22         var result = [Int](repeating: 0, count: N + 1)
23         var availableColors = Array(repeating: Set([1, 2, 3, 4]), count: N + 1)
24         var unvisited = Set(Array(1...N))
25         
26         while let first = unvisited.first {
27             var queue = Set([first])
28             while !queue.isEmpty {
29                 var nextQueue = Set<Int>()
30                 for node in queue {
31                     let color = availableColors[node].first!
32                     result[node] = color
33                     unvisited.remove(node)
34                     nextQueue.remove(node)
35                     for neighbor in edges[node, default: []] {
36                         availableColors[neighbor].remove(color)
37                         guard unvisited.contains(neighbor) else { continue }
38                         nextQueue.insert(neighbor)
39                     }
40                 }
41                 queue = nextQueue
42             }
43         }
44         
45         result.removeFirst()
46         
47         return result
48     }
49 }
相關文章
相關標籤/搜索