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

From ZCubes Wiki
Jump to navigation Jump to search
(Created page with "== LeetCode Problem 1 – Team Chemistry == === ZCUBES Format === <pre> skill = [3,4]; p = skill.⋰.⁜(2); arr = p[0].mergerows(p[1].reverse()); tar = ∑(arr[0]); if(arr....")
 
Line 18: Line 18:
 
PRINT(result);
 
PRINT(result);
 
</pre>
 
</pre>
 +
 +
 +
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 ===
 
=== JavaScript Format ===

Revision as of 17:35, 3 June 2025

LeetCode Problem 1 – Team Chemistry

ZCUBES Format

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).

JavaScript Format

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;
};