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:
def solution(A, B)
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].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, B):
count_height = {}
max_count = 0
i = 0
while i < len(A):
if A[i] == B[i] and A[i] not in count_height:
count_height[A[i]] = 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
elif A[i] == B[i] and A[i] in count_height:
count_height[A[i]] += 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
else:
if A[i] not in count_height and B[i] not in count_height:
count_height[A[i]] = 1
count_height[B[i]] = 1
elif A[i] not in count_height and B[i] in count_height:
count_height[A[i]] = 1
count_height[B[i]] += 1
elif A[i] in count_height and B[i] not in count_height:
count_height[A[i]] += 1
count_height[B[i]] = 1
elif A[i] in count_height and B[i] in count_height:
count_height[A[i]] += 1
count_height[B[i]] += 1
if max(count_height[A[i]], count_height[B[i]]) > max_count:
max_count = max(count_height[A[i]], count_height[B[i]])
i += 1
return max_count
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, B):
count_height = {}
max_count = 0
i = 0
while i < len(A):
if A[i] == B[i] and A[i] not in count_height:
count_height[A[i]] = 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
elif A[i] == B[i] and A[i] in count_height:
count_height[A[i]] += 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
else:
if A[i] not in count_height and B[i] not in count_height:
count_height[A[i]] = 1
count_height[B[i]] = 1
elif A[i] not in count_height and B[i] in count_height:
count_height[A[i]] = 1
count_height[B[i]] += 1
elif A[i] in count_height and B[i] not in count_height:
count_height[A[i]] += 1
count_height[B[i]] = 1
elif A[i] in count_height and B[i] in count_height:
count_height[A[i]] += 1
count_height[B[i]] += 1
if max(count_height[A[i]], count_height[B[i]]) > max_count:
max_count = max(count_height[A[i]], count_height[B[i]])
i += 1
return max_count
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A, B):
count_height = {}
max_count = 0
i = 0
while i < len(A):
if A[i] == B[i] and A[i] not in count_height:
count_height[A[i]] = 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
elif A[i] == B[i] and A[i] in count_height:
count_height[A[i]] += 1
if count_height[A[i]] > max_count:
max_count = count_height[A[i]]
else:
if A[i] not in count_height and B[i] not in count_height:
count_height[A[i]] = 1
count_height[B[i]] = 1
elif A[i] not in count_height and B[i] in count_height:
count_height[A[i]] = 1
count_height[B[i]] += 1
elif A[i] in count_height and B[i] not in count_height:
count_height[A[i]] += 1
count_height[B[i]] = 1
elif A[i] in count_height and B[i] in count_height:
count_height[A[i]] += 1
count_height[B[i]] += 1
if max(count_height[A[i]], count_height[B[i]]) > max_count:
max_count = max(count_height[A[i]], count_height[B[i]])
i += 1
return max_count
pass
The solution obtained perfect score.