Maximum Cost of Laptop Count - Problem Solving (Basic) certification | HackerRank

Maximum Cost of Laptop Count

A company manufactures a fixed number of laptops every day. However, if some defect is encountered during the testing of a laptop, it is labeled as "illegal" and is not counted in the laptop count of the day. Given the cost to manufacture each laptop, its label as "illegal" or "legal", and the number of legal laptops to be manufactured each day, find the maximum cost incurred by the company in a single day in manufacturing all the laptops.

Note that if a laptop is labeled as illegal, its manufacturing cost is still incurred by the company, even though it is not included in the laptop count. Also, days are only taken into when the daily laptop count has been completely met. If there are no such days, the answer is 0.

For example, let's say there are n = 5 laptops, where cost = [2,5, 3, 11, 1]. The labels for these laptops are labels = ["legal", "illegal", "legal", "illegal", "legal"). Finally, the daily Count = 2, which means that the company needs to manufacture 2 legal laptops each day. The queue of laptops can be more easily viewed as follows:

  • cost 2, "legal" 
  • cost 5, "illegal" 
  • cost 3, "legal" 
  • cost 11, "illegal" 
  • cost 1, "legal"
On the first day, the first three laptops are manufactured in order to reach the daily count of 2 legal laptops. The cost incurred on this day is 2 + 5 + 3 = 10. On the second day, the fourth and fifth laptops are manufactured, but because only one of them is legal, the daily count isn't met, so that day is not taken into consideration. Therefore, the maximum cost incurred on a single day is 10.


Function Description 

Complete the function maxCost in the editor below.
maxCost has the following parameter(s):
    int cost[n]: an array of integers denoting the cost to manufacture each laptop string labels[n] an         array of strings denoting the label of each laptop ("legal" or "illegal")
    int dailyCount: the number of legal laptops to be manufactured each day 
Returns:
    Int: the maximum cost incurred in a single day of laptop manufacturing

Constraints

• 1   105 
• O  cost[i]  104 
• 1 ≤ dailyCounts  n
• labels[i] E {"legal", "illegal"}

Input Format For Custom Testing

The first line contains an integer, n, denoting the number of laptops and the size of the array cost. Each line i of the n subsequent lines (where 0 si<n) contains an integer, cost[i], denoting the cost to manufacture each laptop. The next line again contains an integer, n, denoting the size of the array labels. Each line i of the n subsequent lines (where 0 si<n) contains an integer, labels[i], denoting the label of each laptop ("legal" or "illegal"). The last line contains an integer, dailyCount, denoting the number of legal laptops to be manufactured each day.

Solution in Python:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/python3

import math
import os
import random
import re
import sys



#
# Complete the 'maxCost' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. INTEGER_ARRAY cost
#  2. STRING_ARRAY labels
#  3. INTEGER dailyCount
#

def maxCost(cost, labels, dailyCount):
    ans = 0
    cur_cnt = 0
    cur_cost = 0
    for c, l in zip(cost, labels):
        cur_cost += c
        if l == "illegal":
            continue
        cur_cnt += 1
        if cur_cnt == dailyCount:
            ans = max(ans, cur_cost)
            cur_cnt = 0
            cur_cost = 0
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    cost_count = int(input().strip())

    cost = []

    for _ in range(cost_count):
        cost_item = int(input().strip())
        cost.append(cost_item)

    labels_count = int(input().strip())

    labels = []

    for _ in range(labels_count):
        labels_item = input()
        labels.append(labels_item)

    dailyCount = int(input().strip())

    result = maxCost(cost, labels, dailyCount)

    fptr.write(str(result) + '\n')

    fptr.close()
Labels : #hackerrank ,#hackerrank certification ,#problem solving ,#problem solving (Basic) ,#python ,

Post a Comment