-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-113-breakingBoundaries.js
194 lines (152 loc) · 5.06 KB
/
structy-113-breakingBoundaries.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// https://structy.net/problems/premium/breaking-boundaries
// p: 5 nums
// r: num
// e: m 3 n 4:
// s x x x
// x x x x
// x x x x
// k = 2 => 4 ways
// recursion w MEMO
const breakingBoundaries = (m, n, k, r, c, count = 0, memo = {}) => {
const boundY = 0 <= r && r < m;
const boundX = 0 <= c && c < n;
const krc = k + "," + r + "," + c;
if (krc in memo) return memo[krc];
if (!boundY || !boundX) return 1;
if (k === 0) return count;
const sk = breakingBoundaries(m, n, k - 1, r + 1, c, count, memo);
const nk = breakingBoundaries(m, n, k - 1, r - 1, c, count, memo);
const ek = breakingBoundaries(m, n, k - 1, r, c + 1, count, memo);
const wk = breakingBoundaries(m, n, k - 1, r, c - 1, count, memo);
memo[krc] = count + nk + sk + ek + wk;
return count + nk + sk + ek + wk;
};
// // recursion wo MEMO
// const breakingBoundaries = (m, n, k, r, c, count = 0) => {
// const boundY = 0 <= r && r < m;
// const boundX = 0 <= c && c < n;
// if (!boundY || !boundX) return 1;
// if (k === 0) return count;
// const sk = breakingBoundaries(m, n, k - 1, r + 1, c, count);
// const nk = breakingBoundaries(m, n, k - 1, r - 1, c, count);
// const ek = breakingBoundaries(m, n, k - 1, r, c + 1, count);
// const wk = breakingBoundaries(m, n, k - 1, r, c - 1, count);
// return count + nk + sk + ek + wk;
// };
// recursion NOt working
// const breakingBoundaries = (m, n, k, r, c, count = 0) => {
// const boundY = 0 <= r && r < m;
// const boundX = 0 <= c && c < n;
// if (!boundY || !boundX) {
// console.log(r, c);
// return count + 1;
// }
// // if (boundY && boundX) return count;
// if (k === 0) return;
// breakingBoundaries(m, n, k - 1, r + 1, c, count);
// breakingBoundaries(m, n, k - 1, r - 1, c, count);
// breakingBoundaries(m, n, k - 1, r, c + 1, count);
// breakingBoundaries(m, n, k - 1, r, c - 1, count);
// return count;
// };
///////////////////////////////////////////////////////
// iteration no MEMO - time limit exceeded
// const const n = breakingBoundaries(m, n, k, r, c) => {
// // const visited = new Set();
// const memo = {}
// const queue = [[r, c, k]];
// let count = 0;
// while (queue.length > 0) {
// const [y, x, distance] = queue.shift();
// if (distance === 0) break;
// const deltas = [
// [-1, 0],
// [1, 0],
// [0, -1],
// [0, 1],
// ];
// for (const delta of deltas) {
// const [a, b] = delta;
// const neiY = y + a;
// const neiX = x + b;
// const nei = neiY + "," + neiX;
// const boundY = 0 <= neiY && neiY < m;
// const boundX = 0 <= neiX && neiX < n;
// console.log(neiY, neiX, count, k);
// if (!boundY || !boundX) {
// count++;
// // console.log(neiY, neiX, count, k);
// continue;
// }
// boundY && boundX && queue.push([neiY, neiX, distance - 1]);
// }
// }
// return count;
// };
// console.log(breakingBoundaries(3, 4, 2, 0, 0)); // -> 4
// e: m 3 n 4:
//
//
// 1 2 3
// 1 s 1 2 3
// 2 1 2 3 x
// 3 2 3 x x
// 3
// k = 3 => 11 ways
console.log(breakingBoundaries(3, 4, 3, 0, 0)); // -> 11
// Could someone please help me with this problem? https://structy.net/problems/premium/breaking-boundaries
// (I haven't seen the walkthrough yet. I want to work on it first.)
// So, how come the `test_02` return `11` intead of `7`: `breakingBoundaries(3, 4, 3, 0, 0); // -> 11`?
// This is how I understand the problem using BFS:
// ```
// // 1 2 3
// // 1 [s, 1, 2, 3]
// // 2 [1, 2, 3, x]
// // 3 [2, 3, x, x]
// // 3
// ```
// or maybe return `9` if these 2 paths are also counted:
// 1. `[0, 0]` -> `[1, 0]` -> `[0, 0]` -> `[-1, 0]`
// 2. `[0, 0]` -> `[0, 1]` -> `[0, 0]` -> `[0, -1]`
// How does it return `11`? Please let me know what I'm missing.
// Here is my code:
// ```
// const const n = breakingBoundaries(m, n, k, r, c) => {
// const visited = new Set();
// const queue = [[r, c, k]];
// let count = 0;
// while (queue.length > 0) {
// const [y, x, distance] = queue.shift();
// if (distance === 0) break;
// const deltas = [[-1, 0], [1, 0], [0, -1], [0, 1]];
// for (const delta of deltas) {
// const [a, b] = delta;
// const neiY = y + a;
// const neiX = x + b;
// const nei = neiY + "," + neiX;
// const boundY = 0 <= neiY && neiY < m;
// const boundX = 0 <= neiX && neiX < n;
// if (!boundY || !boundX) {
// if (visited.has(nei)) continue;
// visited.add(nei);
// count++;
// continue;
// }
// boundY && boundX && queue.push([neiY, neiX, distance - 1]);
// }
// }
// return count;
// };
// ```
// I appreciate it.
// -1 0 1 3 x
// 0 -1 2 3
// 1 -1 3 3
// -1 1 4 3
// -1 0 5 3 x
// 0 -1 6 3
// 3 0 7 3
// 2 -1 8 3
// -1 0 9 3 x
// 0 -1 10 3
// -1 2 11 3