This Python program is designed to solve a problem of job assignment. Given two lists of integers, one representing the IDs of crew members and the other representing job IDs, the task is to assign each job to a crew member in such a way that the total cost is minimized. The cost of assigning a job to a crew member is defined as the absolute difference between their IDs.
CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷
The solution works by first sorting both the crew_id and job_id lists. This ensures that the jobs and crew members with the closest IDs are paired together, minimizing the total cost. The cost for each pair is then calculated by finding the absolute difference between the crew member's ID and the job's ID. The total cost is the sum of these individual costs. This approach leverages the properties of sorted arrays and the mathematical concept of absolute difference to efficiently solve the problem.
import math
import os
import random
import re
import sys
def getMinCost(crew_id, job_id):
crew_id.sort()
job_id.sort()
return sum(abs(c - j) for c, j in zip(crew_id, job_id))
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
crew_id_count = int(input().strip())
crew_id = []
for _ in range(crew_id_count):
crew_id_item = int(input().strip())
crew_id.append(crew_id_item)
job_id_count = int(input().strip())
job_id = []
for _ in range(job_id_count):
job_id_item = int(input().strip())
job_id.append(job_id_item)
result = getMinCost(crew_id, job_id)
fptr.write(str(result) + '
')
fptr.close()
If you have a HackerRank Certification test or a coding interview coming up, you can use CodeRankGPT to your advantage. It will assist you during your test and help ensure you get the certification or the job.
AI is here now, and other candidates might be using it to get ahead and win the job. 🧐
The form has been successfully submitted.