There are N rectangles numbered from 0 to N-1. The K-th rectangle has size A[K] × B[K].
We want to arrange as many rectangles as possible into a strip. The rectangles can be arranged into a strip if they all share a side of the same length (which becomes the height of the strip). Note that rectangles can be rotated.
Write a function:
func Solution(A []int, B []int) int
that, given two arrays A and B of N integers each, returns the maximum number of rectangles that can be arranged into a strip.
Examples:
1. Given A = [2, 3, 2, 3, 5] and B = [3, 4, 2, 4, 2], the function should return 3. Choosing the 0th, 2nd and 4th rectangles we can obtain the following strip of height 2 (note that the 0th rectangle was rotated):
We can also choose the 0th, 1st and 3rd rectangles to obtain a strip of height 3. Here we have rotated 1st and 3rd rectangles:
2. Given A = [2, 3, 1, 3] and B = [2, 3, 1, 3], the function should return 2. We can choose the 1st and 3rd rectangles:
3. Given A = [2, 10, 4, 1, 4] and B = [4, 1, 2, 2, 5], the function should return 3. We can choose the 0th, 2nd and 4th rectangles to obtain a strip of height 4:
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- arrays A and B both consist of N integers;
- each element of arrays A and B is an integer within the range [1..1,000,000,000].
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func Solution(A []int, B []int) int {
s := make(map[int]bool)
for _, v := range A {
s[v] = true
}
for _, v := range B {
s[v] = true
}
maxMatch := 0
for n, _ := range s {
match := 0
for i, _ := range A {
if A[i] == n || B[i] == n {
match += 1
}
}
maxMatch = max(maxMatch, match)
}
return maxMatch
}
# solution workspace/src/solution/solution.go:27: undefined: max
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func max(a, b int) int {
if a > b {
return a
}
return b
}
func Solution(A []int, B []int) int {
s := make(map[int]bool)
for _, v := range A {
s[v] = true
}
for _, v := range B {
s[v] = true
}
maxMatch := 0
for n, _ := range s {
match := 0
for i, _ := range A {
if A[i] == n || B[i] == n {
match += 1
}
}
maxMatch = max(maxMatch, match)
}
return maxMatch
}
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func max(a, b int) int {
if a > b {
return a
}
return b
}
func Solution(A []int, B []int) int {
s := make(map[int]bool)
for _, v := range A {
s[v] = true
}
for _, v := range B {
s[v] = true
}
maxMatch := 0
for n, _ := range s {
match := 0
for i, _ := range A {
if A[i] == n || B[i] == n {
match += 1
}
}
maxMatch = max(maxMatch, match)
}
return maxMatch
}
package solution
// you can also use imports, for example:
// import "fmt"
// import "os"
// you can write to stdout for debugging purposes, e.g.
// fmt.Println("this is a debug message")
func max(a, b int) int {
if a > b {
return a
}
return b
}
func Solution(A []int, B []int) int {
s := make(map[int]bool)
for _, v := range A {
s[v] = true
}
for _, v := range B {
s[v] = true
}
maxMatch := 0
for n, _ := range s {
match := 0
for i, _ := range A {
if A[i] == n || B[i] == n {
match += 1
}
}
maxMatch = max(maxMatch, match)
}
return maxMatch
}
The following issues have been detected: timeout errors.
No rotations needed. Common side can be chosen from A. N = 100,000.
Killed. Hard limit reached: 6.000 sec.
No rotations needed. Common side can be chosen from B. N = 100,000.
Killed. Hard limit reached: 6.000 sec.
Array A contains shorter sides. N = 100,000. Points x 2.
Killed. Hard limit reached: 6.000 sec.
Array A contains longer sides. N = 100,000. Points x 2.
Killed. Hard limit reached: 6.000 sec.
All rectangles are squares. N = 100,000. Points x 2.
Killed. Hard limit reached: 6.000 sec.