Difference between revisions of "LeetCode Solutions/Problem Group 1 (Team Chemistry)"

From ZCubes Wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== LeetCode Problem 1 – Team Chemistry ==
+
== Leetcode 2491. Divide Players Into Teams of Equal Skill ==
  
=== ZCUBES Format ===
+
=== In z^3 ===
 
<pre>
 
<pre>
 
skill = [3,4];
 
skill = [3,4];
Line 22: Line 22:
 
The attached file has .⋰.⁜ for sort and parts respectively and sigma symbol for sum and finally ≠ for not equal to. Also X symbol for product(zmultiply).
 
The attached file has .⋰.⁜ for sort and parts respectively and sigma symbol for sum and finally ≠ for not equal to. Also X symbol for product(zmultiply).
  
=== JavaScript Format ===
+
=== In JavaScript ===
 
<pre>
 
<pre>
 
var dividePlayers = function(skill) {
 
var dividePlayers = function(skill) {
Line 60: Line 60:
 
};
 
};
 
</pre>
 
</pre>
 +
'''See more:'''
 +
* [https://wiki.zcubes.com/Neet_Code_Problems Neetcode Problems] to see more leetcode style problems being solved.

Latest revision as of 13:52, 4 June 2025

Leetcode 2491. Divide Players Into Teams of Equal Skill

In z^3

skill = [3,4];
p = skill.⋰.⁜(2);
arr = p[0].mergerows(p[1].reverse());
tar = ∑(arr[0]);

if(arr.filter(r => ≠(∑(r), tar)).length > 0)
{
    result = "Not compatible(-1)";
}
else{
    result = ∑(arr.$$(✖))
}

PRINT(result);


The attached file has .⋰.⁜ for sort and parts respectively and sigma symbol for sum and finally ≠ for not equal to. Also X symbol for product(zmultiply).

In JavaScript

var dividePlayers = function(skill) {
    function find_nums(val, arr, target) {
        for (let i = 0; i < arr.length; i++) {
            if (val + arr[i] === target) {
                return [val, arr[i]];
            }
        }
        return null;
    }

    let sorted_arr = skill.sort((a, b) => b - a);
    let tar = sorted_arr[0] + sorted_arr[sorted_arr.length - 1];
    let total_chemistry = 0;

    for (let i = 0; i < sorted_arr.length - 1; i++) {
        if (i >= sorted_arr.length) {
            break;
        }

        let pair = find_nums(sorted_arr[i], sorted_arr.slice(i + 1), tar);

        if (pair) {
            total_chemistry += pair[0] * pair[1];
            let index1 = sorted_arr.indexOf(pair[0]);
            if (index1 !== -1) sorted_arr.splice(index1, 1);
            let index2 = sorted_arr.indexOf(pair[1]);
            if (index2 !== -1) sorted_arr.splice(index2, 1);
            i--;
        } else {
            return -1;
        }
    }

    return total_chemistry;
};

See more: