|
| 1 | +/** |
| 2 | + * @param {number[][]} grid |
| 3 | + * @return {number} |
| 4 | + */ |
| 5 | +const minimumObstacles = function (g) { |
| 6 | + const m = g.length; const n = g[0].length |
| 7 | + const dx = [0, 0, 1, -1]; const dy = [1, -1, 0, 0] |
| 8 | + const valid = (x, y) => x >= 0 && x < m && y >= 0 && y < n |
| 9 | + |
| 10 | + const q = new Deque([[0, 0, 0]]); const v = {} |
| 11 | + while (q.size()) { |
| 12 | + const [i, j, cd] = q.shift() |
| 13 | + if (v[`${i}_${j}`]) continue |
| 14 | + v[`${i}_${j}`] = 1 |
| 15 | + if (i === m - 1 && j === n - 1) return cd |
| 16 | + for (let k = 0; k < 4; k++) { |
| 17 | + const x = i + dx[k]; const y = j + dy[k] |
| 18 | + if (valid(x, y) && !v[`${x}_${y}`]) { |
| 19 | + if (g[x][y] === 0) q.unshift([x, y, cd]) |
| 20 | + else q.push([x, y, cd + 1]) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class CircularDeque { |
| 27 | + constructor (N) { |
| 28 | + this.prev = this.next = null |
| 29 | + this.begin = this.end = 0 |
| 30 | + this.empty = true |
| 31 | + this.data = Array(N) |
| 32 | + } |
| 33 | + |
| 34 | + isFull () { |
| 35 | + return this.end === this.begin && !this.empty |
| 36 | + } |
| 37 | + |
| 38 | + isEmpty () { |
| 39 | + return this.empty |
| 40 | + } |
| 41 | + |
| 42 | + push (val) { |
| 43 | + if (this.isFull()) { return false } |
| 44 | + this.empty = false |
| 45 | + this.data[this.end] = val |
| 46 | + this.end = (this.end + 1) % this.data.length |
| 47 | + return true |
| 48 | + } |
| 49 | + |
| 50 | + front () { |
| 51 | + return this.isEmpty() ? void 0 : this.data[this.begin] |
| 52 | + } |
| 53 | + |
| 54 | + back () { |
| 55 | + return this.isEmpty() ? void 0 : this.data[this.end - 1] |
| 56 | + } |
| 57 | + |
| 58 | + pop () { |
| 59 | + if (this.isEmpty()) { return void 0 } |
| 60 | + const value = this.data[this.end - 1] |
| 61 | + this.end = (this.end - 1) % this.data.length |
| 62 | + if (this.end < 0) { this.end += this.data.length } |
| 63 | + if (this.end === this.begin) { this.empty = true } |
| 64 | + return value |
| 65 | + } |
| 66 | + |
| 67 | + unshift (val) { |
| 68 | + if (this.isFull()) { return false } |
| 69 | + this.empty = false |
| 70 | + this.begin = (this.begin - 1) % this.data.length |
| 71 | + if (this.begin < 0) { this.begin += this.data.length } |
| 72 | + this.data[this.begin] = val |
| 73 | + return true |
| 74 | + } |
| 75 | + |
| 76 | + shift () { |
| 77 | + if (this.isEmpty()) { return void 0 } |
| 78 | + const value = this.data[this.begin] |
| 79 | + this.begin = (this.begin + 1) % this.data.length |
| 80 | + if (this.end === this.begin) { this.empty = true } |
| 81 | + return value |
| 82 | + } |
| 83 | + |
| 84 | + * values () { |
| 85 | + if (this.isEmpty()) { return void 0 } |
| 86 | + let i = this.begin |
| 87 | + do { |
| 88 | + yield this.data[i] |
| 89 | + i = (i + 1) % this.data.length |
| 90 | + } while (i !== this.end) |
| 91 | + } |
| 92 | +} |
| 93 | +class Deque { |
| 94 | + constructor (collection = []) { |
| 95 | + this.head = new CircularDeque(128) |
| 96 | + this.tail = new CircularDeque(128) |
| 97 | + this.tail.empty = this.head.empty = false |
| 98 | + this.tail.prev = this.head |
| 99 | + this.head.next = this.tail |
| 100 | + this._size = 0 |
| 101 | + for (const item of collection) { this.push(item) } |
| 102 | + } |
| 103 | + |
| 104 | + size () { |
| 105 | + return this._size |
| 106 | + } |
| 107 | + |
| 108 | + push (val) { |
| 109 | + let last = this.tail.prev |
| 110 | + if (last.isFull()) { |
| 111 | + const inserted = new CircularDeque(128) |
| 112 | + this.tail.prev = inserted |
| 113 | + inserted.next = this.tail |
| 114 | + last.next = inserted |
| 115 | + inserted.prev = last |
| 116 | + last = inserted |
| 117 | + } |
| 118 | + last.push(val) |
| 119 | + this._size++ |
| 120 | + } |
| 121 | + |
| 122 | + back () { |
| 123 | + if (this._size === 0) { return } |
| 124 | + return this.tail.prev.back() |
| 125 | + } |
| 126 | + |
| 127 | + pop () { |
| 128 | + if (this.head.next === this.tail) { return void 0 } |
| 129 | + const last = this.tail.prev |
| 130 | + const value = last.pop() |
| 131 | + if (last.isEmpty()) { |
| 132 | + this.tail.prev = last.prev |
| 133 | + last.prev.next = this.tail |
| 134 | + } |
| 135 | + this._size-- |
| 136 | + return value |
| 137 | + } |
| 138 | + |
| 139 | + unshift (val) { |
| 140 | + let first = this.head.next |
| 141 | + if (first.isFull()) { |
| 142 | + const inserted = new CircularDeque(128) |
| 143 | + this.head.next = inserted |
| 144 | + inserted.prev = this.head |
| 145 | + inserted.next = first |
| 146 | + first.prev = inserted |
| 147 | + first = inserted |
| 148 | + } |
| 149 | + first.unshift(val) |
| 150 | + this._size++ |
| 151 | + } |
| 152 | + |
| 153 | + shift () { |
| 154 | + if (this.head.next === this.tail) { return void 0 } |
| 155 | + const first = this.head.next |
| 156 | + const value = first.shift() |
| 157 | + if (first.isEmpty()) { |
| 158 | + this.head.next = first.next |
| 159 | + first.next.prev = this.head |
| 160 | + } |
| 161 | + this._size-- |
| 162 | + return value |
| 163 | + } |
| 164 | + |
| 165 | + front () { |
| 166 | + if (this._size === 0) { return void 0 } |
| 167 | + return this.head.next.front() |
| 168 | + } |
| 169 | + |
| 170 | + * values () { |
| 171 | + let node = this.head.next |
| 172 | + while (node !== this.tail) { |
| 173 | + for (const value of node.values()) { yield value } |
| 174 | + node = node.next |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments