Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There are 4 arrays of data. The arrays cat / n ] and cnt [ n ] are each of n integers, and cat [
There are arrays of data. The arrays catn and cntn are each of n integers, and cati relates to cnti These represent a category and count at each index. There are also two arrays of integers that represent q queries. The arrays q and
q contain based indices for the left and right ends of subarrays to consider in cat and cnt
Process the queries in order. Create an array of q values where answer i relates to query i
For each query i:
Within the subarrays in cat and cnt determine the maximum cnt for each cat.
Store the sum of these maxima as the answer to the query.
Clear the cnt values in the range.
After all queries are processed, return the answer array.
Example:
n
q
cat
cnt
l
r
Category
Count
First Query
category has two occurrences with a maximum count of category occurs once with a count of the answer to the first query is delete the values within range and go to next query.
second query
categories and occurs once with counts of and the answer to the second query is delete the values within range
there are no more queries so return the answer array
Function Description
Complete the function segmentMax in the editor below.
segmentMax has the feilowing parameters: int catn: categories int cntn: counts
int q: left indices of queries based int rq: right indices of queries based
Return
intq: the sums of maxima by type for each query range
Constraints
n q
cati
cntil
li ri n
Input Format For Custom Testing The first line contains an integer, n the number of elements in cat.
Each line i of the n subsequent lines where i n contains an integer, catf.
The next line contains an integer, n the number of elements in cnt
Efch line i of the n subsequent lines where i n contains an integer, cnt
The first line contains an integer, q the number of elements in
Each line i of the q subsequent lines where i q contains an integer, Ii
The next line contains an integer, q the number of elements in r
Each line i of the q subsequent lines where i q contains an integer, ri
#binpython
# Complete the 'segmentMax' function below.
#
# The function is expected to return an INTEGERARRAY.
# The function accepts following parameters:
# INTEGERARRAY cat
# INTEGERARRAY cnt
# INTEGERARRAY L
# INTEGERARRAY r
#
def segmentMax cat cht Lem:
# Write your code here
if namemain:
fptr open os environOUTPUTPATH'w
catcount intinput strip
cat
for in range catcount :
catitem intinput strip
cat. append catitem
cntcount intinputstrip
cnt
for in range cntcounti :
cntitem int input strip
cnt append cntitem
count intinputstrip
for
in range lcount :
Litem intinputstrip
l append litem
r count int input strip
r
for in rangercount :
ritem intinput strip
r append ritem
result segmentMax cat cnt l r
fptr write
joinmapstr result
fptr write
fptr close
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started