-
Notifications
You must be signed in to change notification settings - Fork 3
/
DividePlayersIntoTeamsOfEqualSkill2491.kt
69 lines (51 loc) · 1.7 KB
/
DividePlayersIntoTeamsOfEqualSkill2491.kt
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
62
63
64
65
66
67
68
69
package medium
object DividePlayersIntoTeamsOfEqualSkill2491 {
/**
* TIME COMPLEXITY O(N.LOG(N))
*/
fun dividePlayers(skill: IntArray): Long {
// Soring the array
skill.sort()
val n = skill.size
var totalChemistry = 0L
// Calculate the target sum
val targetTeamSkill = skill[0] + skill[n-1]
// Iterate through half of the array, pairing players from both ends
for(i in 0 until n/2)
{
val currentTeamSkill = skill[i]+ skill[n-i-1]
if(currentTeamSkill != targetTeamSkill)
return -1
totalChemistry+=skill[i]* skill[n-i-1]
}
return totalChemistry
}
/**
* TIME COMPLEXITY O(N)
*/
fun dividePlayersSolution2(skill: IntArray): Long {
val n = skill.size
var totalSkill = 0
val skillFrequency = IntArray(1001)
// Calculate total skill and skill frequency
for( playerSkill in skill)
{
totalSkill += playerSkill
skillFrequency[playerSkill]++
}
// Check if total skill can be evenly distributed among teams
if(totalSkill % (n/2) !=0) return -1
val targetTeamSkill = totalSkill/ (n/2)
var totalChemistry = 0L
// Calculate total chemistry while verifying valid team formations
for(playerSkill in skill)
{
val partnerSkill = targetTeamSkill - playerSkill
// Check if a valid partner exists
if(skillFrequency[partnerSkill]==0) return -1
totalChemistry += playerSkill * partnerSkill
skillFrequency[partnerSkill]--
}
return totalChemistry/2
}
}