注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
There are n types of items in a shop's inventory, where the quantity of the ith item, is denoted by quantity[i]. These items are to be shipped into 2 consignments, where the first consignment contains items of type [1,2,…j], and the second consignment contains the remaining item types, where j can be chosen such that 1<= j <= n. Noted that both consignments must be non-empty, and all items of a type must be in the same consignment.
The shopkeeper wants the item counts in each consignment to be equal. To achieve this, the shopkeeper can perform the following move any number of times: increase or decrease the quantity of any item type by 1. The quantity of each item type must remain positive throughout .
Find the minimum numbers of moves in which the total quantities of both consignments can be made equal if the item types are split optimally.
Example: n = 3, arr = [4,5,7]
Output: 2
4+1, 5 ,7-1, where j = 1, quantity[1] = 5
Example: n=5, arr=[3,3,6,3,9]
Output = 0
where j = 2, quantity[2] = 6
==================================================================================
Given an array of n positive int, assuming 0-based indexing, it's cost is:
Insert any int as case. These arrive at time 1,2,3,4. The first packet is sent to port 1 with no conflicts. Port 1 will be occupied at times 1 and 2 due to the transimssion time. So the second packet has a conflict is sent to port1 + 1 = 2. The 3rd packet wants to go to port 1 and arrives at time 3. Since port 1 is no longer transimtting packet , it receives the 3rd packet. The 4th pack goes to port 0 without conflicts. The return array is [1,2,1,0]
Output: [1,2,1,0]
Input:
numberOfPorts = 4
TransmissionTime = 2
Size of packetIDs[] n = 3
packetIDs = [0,2,6]
Output: [0,2,3]
Input:
numberOfPorts = 5
TransmissionTime = 3
Size of packetIDs[] n = 4
packetIDs = [1,3,11,16]
Output: [1,2,3,1] |