Skip to content

Commit 6dcb918

Browse files
authored
Update 1601-maximum-number-of-achievable-transfer-requests.js
1 parent b47b14d commit 6dcb918

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1601-maximum-number-of-achievable-transfer-requests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
// O(n * 2 ^ r)
2+
// r: number of requests
3+
/**
4+
* @param {number} n
5+
* @param {number[][]} requests
6+
* @return {number}
7+
*/
8+
const maximumRequests = function(n, requests) {
9+
const arr = Array(n).fill(0)
10+
let res = 0
11+
bt(requests, 0, arr, 0)
12+
return res
13+
function bt(r, idx, arr, num) {
14+
if(idx === r.length) {
15+
for(let i = 0; i < n; i++) {
16+
if(arr[i] !== 0) return
17+
}
18+
res = Math.max(res, num)
19+
return
20+
}
21+
const [from, to] = r[idx]
22+
arr[from]++
23+
arr[to]--
24+
bt(r, idx + 1, arr, num + 1)
25+
arr[from]--
26+
arr[to]++
27+
28+
bt(r, idx + 1, arr, num)
29+
}
30+
};
31+
32+
33+
// another
34+
135
/**
236
* @param {number} n
337
* @param {number[][]} requests

0 commit comments

Comments
 (0)