Skip to content

Commit 5782e8d

Browse files
committed
added problem 1601
1 parent ab7c074 commit 5782e8d

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

problems/problem1601/max_transfers.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package problem1601
2+
3+
/*
4+
We have n buildings numbered from 0 to n - 1. Each building has a number of employees.
5+
It's transfer season, and some employees want to change the building they reside in.
6+
You are given an array requests where requests[i] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.
7+
All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero.
8+
This means the number of employees leaving is equal to the number of employees moving in.
9+
For example if n = 3 and two employees are leaving building 0, one is leaving building 1, and one is leaving building 2,
10+
there should be two employees moving to building 0, one employee moving to building 1, and one employee moving to building 2.
11+
Return the maximum number of achievable requests.
12+
*/
13+
14+
func maximumRequests(n int, reqs [][]int) int {
15+
var res int
16+
var diffs = make([]int, n)
17+
calcRequests(reqs, diffs, 0, &res)
18+
return res
19+
}
20+
21+
func calcRequests(reqs [][]int, diffs []int, cur int, res *int) {
22+
if len(reqs) == 0 {
23+
// If we processed all the requests, check that there is no surplus
24+
if allZeros(diffs) {
25+
*res = max(*res, cur)
26+
}
27+
return
28+
}
29+
30+
// Reject a request
31+
// Find the max starting from the next request
32+
calcRequests(reqs[1:], diffs, cur, res) // cur stays the same because we rejected
33+
34+
// Accept a request
35+
diffs[reqs[0][0]]--
36+
diffs[reqs[0][1]]++
37+
// Find the max starting from the next request
38+
calcRequests(reqs[1:], diffs, cur+1, res) // cur increases because we accepted
39+
40+
// Undo the request
41+
diffs[reqs[0][0]]++
42+
diffs[reqs[0][1]]--
43+
44+
}
45+
46+
func allZeros(inc []int) bool {
47+
for i := range inc {
48+
if inc[i] != 0 {
49+
return false
50+
}
51+
}
52+
return true
53+
}
54+
55+
func max(a, b int) int {
56+
if a > b {
57+
return a
58+
}
59+
return b
60+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem1601
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
N int
12+
Requests [][]int
13+
Expected int
14+
}
15+
16+
var TestCases = []TestCase{
17+
{5, [][]int{{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}}, 5},
18+
{3, [][]int{{0, 0}, {1, 2}, {2, 1}}, 3},
19+
{4, [][]int{{0, 3}, {3, 1}, {1, 2}, {2, 0}}, 4},
20+
{3, [][]int{{1, 2}, {1, 2}, {2, 2}, {0, 2}, {2, 1}, {1, 1}, {1, 2}}, 4},
21+
}
22+
23+
func TestMaxTransfers(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
for _, tc := range TestCases {
27+
want := tc.Expected
28+
got := maximumRequests(tc.N, tc.Requests)
29+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
30+
}
31+
}
32+
33+
func BenchmarkMaxTransfers(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
for _, tc := range TestCases {
36+
maximumRequests(tc.N, tc.Requests)
37+
}
38+
}
39+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ Each problem is in it's own directory, with test files. There are helper package
439439
| 1575 | [Count All Possible Routes](https://leetcode.com/problems/count-all-possible-routes) | [My Solution](./problems/problem1575) ||
440440
| 1578 | [Minimum Time to Make Rope Colorful](https://leetcode.com/problems/minimum-time-to-make-rope-colorful) | [My Solution](./problems/problem1578) ||
441441
| 1579 | [Remove Max Number of Edges to Keep Graph Fully Traversable](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable) | [My Solution](./problems/problem1579) ||
442+
| 1601 | [Maximum Number of Achievable Transfer Requests](https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests) | [My Solution](./problems/problem1601) ||
442443
| 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system) | [My Solution](./problems/problem1603) ||
443444
| 1609 | [Even Odd Tree](https://leetcode.com/problems/even-odd-tree) | [My Solution](./problems/problem1609) ||
444445
| 1626 | [Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts) | [My Solution](./problems/problem1626) ||

0 commit comments

Comments
 (0)