- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find total duration of K most watched shows in Python

Suppose we have a list of list of strings called shows, also have a list of integers called durations, and another value k, here shows[i] and durations[i] represent a show and its duration watched by the ith man, we have to find the total duration watched of the k most watched shows.

So, if the input is like shows = ["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"] durations = [10, 8, 10, 18, 9] k = 2, then the output will be 38, as the top 2 most watched shows are "Jokers Company" and "The BGT" and total durations is 18 and 10 + 10 = 20.

To solve this, we will follow these steps −

if shows is empty or durations is empty or k is 0, then

return 0

d := an empty dictionary

for i in range 0 to size of shows, do

d[shows[i]] := d[shows[i]] + durations[i]

l := a new list

for each i in d, do

insert d[i] at the end of l

sort the list l in reverse order

i := 0

answer := 0

while i < k, do

answer := answer + l[i]

i := i + 1

return answer

## Example

Let us see the following implementation to get better understanding

from collections import defaultdict def solve(shows, durations, k): if not shows or not durations or not k: return 0 d = defaultdict(int) for i in range(len(shows)): d[shows[i]] += durations[i] l = [] for i in d: l.append(d[i]) l.sort(reverse=True) i = 0 answer = 0 while i < k: answer += l[i] i += 1 return answer shows = ["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"] durations = [10, 8, 10, 18, 9] k = 2 print(solve(shows, durations, k))

## Input

["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"], [10, 8, 10, 18, 9], 2

## Output

38

- Related Questions & Answers
- Program to find total unique duration from a list of intervals in Python
- Program to find most occurring number after k increments in python
- Program to find sum of rectangle whose sum at most k in Python
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Program to find maximum sum by performing at most k negate operations in Python
- Program to find min length of run-length encoding after removing at most k characters in Python
- Java Program to convert this duration to the total length in nanoseconds
- Java Program to convert this duration to the total length in milliseconds
- Program to find minimum cost to reach final index with at most k steps in python
- Program to find frequency of the most frequent element in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Find the k most frequent words from data set in Python
- Program to find list that shows closest distance of character c from that index in Python
- Program to count number of ways to win at most k consecutive games in Python
- Program to find the most competitive subsequence in Python