2491. Divide Players Into Teams of Equal Skill
Difficulty: Medium
Topics: Array, Hash Table, Two Pointers, Sorting
You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.
The chemistry of a team is equal to the product of the skills of the players on that team.
Return the sum of the chemistry of all the teams, or return -1 if there is no way to divide the players into teams such that the total skill of each team is equal.
Example 1:
- Input: skill = [3,2,5,1,3,4]
- Output: 22
-
Explanation:
- Divide the players into the following teams: (1, 5), (2, 4), (3, 3), where each team has a total skill of 6.
- The sum of the chemistry of all the teams is: 1 * 5 + 2 * 4 + 3 * 3 = 5 + 8 + 9 = 22.
Example 2:
- Input: skill = [3,4]
- Output: 112
-
Explanation:
- The two players form a team with a total skill of 7.
- The chemistry of the team is 3 * 4 = 12.
Example 3:
- Input: skill = [1,1,2,3]
- Output: -1
- Explanation: There is no way to divide the players into teams such that the total skill of each team is equal.
Constraints:
- 2 5
- skill.length is even.
- 1
Hint:
- Try sorting the skill array.
- It is always optimal to pair the weakest available player with the strongest available player.
Solution:
We can follow the hint provided and use a greedy approach. Here's the detailed breakdown of the solution:
Steps:
Sort the Skill Array: Sorting allows us to efficiently pair the weakest player (smallest value) with the strongest player (largest value).
Check for Valid Pairing: The sum of the skills of each team should be equal. After sorting, we will pair the smallest and the largest element, then the second smallest with the second largest, and so on. If at any point, the sum of a pair differs from the previous sums, it's impossible to divide the players into valid teams, and we should return -1.
Calculate the Chemistry: The chemistry of each team is the product of the two skills in that team. Sum up all the chemistry values for each valid team.
Return the Total Chemistry: If all the teams have the same total skill, return the sum of their chemistry.
Let's implement this solution in PHP: 2491. Divide Players Into Teams of Equal Skill
<p><?php <br> /**</p>
- @param Integer[] $skill
- @return Integer
/
function dividePlayers($skill) {
...
...
...
/*
- go to ./solution.php */ }
// Test cases
$skill1 = [3, 2, 5, 1, 3, 4];
$skill2 = [3, 4];
$skill3 = [1, 1, 2, 3];
echo dividePlayers($skill1) . "\n"; // Output: 22
echo dividePlayers($skill2) . "\n"; // Output: 12
echo dividePlayers($skill3) . "\n"; // Output: -1
?>
Explanation:
Sorting: The array skill is sorted to ensure that we can efficiently pair the smallest and largest values.
Two Pointers: We use two pointers ($i starting from the beginning and $j starting from the end). For each valid pair (smallest and largest), we check if their sum is the same as the expected teamSkillSum. If not, it's impossible to divide the players into teams.
Chemistry Calculation: If the pair is valid, the chemistry is calculated as the product of the two values ($skill[$i] * $skill[$j]), and we keep adding it to the total chemistry.
-
Edge Cases:
- If the team cannot be formed due to unequal sums, we return -1.
- The code handles cases with even length and ensures that all players are paired up correctly.
Time Complexity:
- Sorting the array takes O(n log n), and the two-pointer traversal takes O(n). Hence, the overall time complexity is O(n log n), which is efficient given the constraints.
This solution works within the given constraint of up to 105 players.
Contact Links
如果您發現本系列有幫助,請考慮在 GitHub 上給 存儲庫 一個星號或在您最喜歡的社交網絡上分享該帖子? 。您的支持對我來說意義重大!
如果您想要更多類似的有用內容,請隨時關注我:
- 領英
- GitHub
以上是將玩家分成技能相同的團隊的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Laravel使用其直觀的閃存方法簡化了處理臨時會話數據。這非常適合在您的應用程序中顯示簡短的消息,警報或通知。 默認情況下,數據僅針對後續請求: $請求 -

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

PHP日誌記錄對於監視和調試Web應用程序以及捕獲關鍵事件,錯誤和運行時行為至關重要。它為系統性能提供了寶貴的見解,有助於識別問題並支持更快的故障排除

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显著减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

記事本++7.3.1
好用且免費的程式碼編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器