Answered step by step
Verified Expert Solution
Link Copied!

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 4 arrays of data. The arrays cat/n] and cnt[n] are each of n integers, and cat[i] 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 1-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=5
q=2
cat =[1,2,1,1,3]
cnt =[5,3,4,5,2]
l=[1,1]
r =[3,5]
Category
1
2
1
1
3
Count
5
3
4
5
2
First Query [1,3]
category 1 has two occurrences with a maximum count of 5 category 2 occurs once with a count of 3 the answer to the first query is 5+3=8. delete the values within range and go to next query.
second query [1,5],
categories 1 and 3 occurs once with counts of 5 and 2 the answer to the second query is 5+2=7 delete the values within range
there are no more queries so return the answer array [8,7]
Function Description
Complete the function segmentMax in the editor below.
segmentMax has the feilowing parameters: int cat[n]: categories int cnt[n]: counts
int /[q]: left indices of queries (1-based) int r[q]: right indices of queries (1-based)
Return
int[q]: the sums of maxima by type for each query range
Constraints
1<= n, q <=10^5
1<= cat[i]<=10^4
1<= cntil <=10^5
1<= l[i]<= r[i]<= 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 0<= 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 0<= 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 0<= i < q) contains an integer, I[i].
The next line contains an integer, q, the number of elements in r.
Each line i of the q subsequent lines (where 0<= i < q) contains an integer, r[i].
#!/bin/python3"
# Complete the 'segmentMax' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY cat
# 2. INTEGER_ARRAY cnt
# 3. INTEGER_ARRAY L
#4. INTEGER_ARRAY r
#
def segmentMax (cat, cht, Lem:
# Write your code here
if __name__=='__main__':
fptr = open (os. environ['OUTPUT_PATH'],'w')
cat_count = int(input(). strip ())
cat =[]
for - in range (cat_count) :
cat_item = int(input ().strip())
cat. append (cat_item)
cnt_count = int(input).strip ())
cnt =[]
for - in range (cnt_counti) :
cnt_item = int (input(). strip ())
cnt. append ( cnt_item)
1_count = int(input().strip())
1=
for
- in range (l_count) :
L_item = int(input().strip ())
l. append (l_item)
r_ count = int (input(). strip ())
r =[]
for - in range(r_count) :
r_item = int(input(). strip())
r. append (r_item)
result = segmentMax (cat, cnt, l, r)
fptr. write('
'.join(map(str, result)))
fptr. write('
')
fptr. close ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions

Question

Explain Coulomb's law with an example

Answered: 1 week ago

Question

What is operating system?

Answered: 1 week ago

Question

What is Ohm's law and also tell about Snell's law?

Answered: 1 week ago