-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0994-rotting-oranges.swift
86 lines (76 loc) · 1.99 KB
/
0994-rotting-oranges.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Question Link: https://leetcode.com/problems/rotting-oranges/
*/
class ListNode {
let val: [Int]
var next: ListNode?
init(_ val: [Int]) {
self.val = val
}
}
class Deque {
var head: ListNode?
var tail: ListNode?
var size = 0
func enqueue(_ val: [Int]) {
let node = ListNode(val)
if head == nil {
head = node
tail = node
} else {
tail?.next = node
tail = tail?.next
}
size += 1
}
func dequeue() -> [Int]? {
if head == nil {
return nil
}
let node = head
head = head?.next
if head == nil {
tail = nil
}
size -= 1
return node?.val
}
}
class Solution {
func orangesRotting(_ grid: [[Int]]) -> Int {
let rows = grid.count
let cols = grid[0].count
var deque = Deque()
var visit = Set<[Int]>()
var fresh = 0
var minutes = 0
for r in 0..<rows {
for c in 0..<cols {
if grid[r][c] == 1 {
fresh += 1
}
if grid[r][c] == 2 {
deque.enqueue([r, c])
}
}
}
while deque.size > 0 && fresh > 0 {
for i in 0..<deque.size {
let val = deque.dequeue()!
let r = val[0]
let c = val[1]
let directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for (dr, dc) in directions {
if r + dr < 0 || c + dc < 0 || r + dr == rows || c + dc == cols || grid[r + dr][c + dc] != 1 || visit.contains([r + dr, c + dc]) {
continue
}
deque.enqueue([r + dr, c + dc])
visit.insert([r + dr, c + dc])
fresh -= 1
}
}
minutes += 1
}
return fresh == 0 ? minutes : -1
}
}