Javascript Backtracking
\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function (rating) {\n  const res = [];\n  btk(rating, res);\n  return res.length;\n
```\n/**\n * @param {number[]} rating\n * @return {number}\n */\nvar numTeams = function (rating) {\n  const res = [];\n  btk(rating, res);\n  return res.length;\n};\n\nfunction btk(rating, res, arr = [], index = 0) {\n  if (arr.length === 3) {\n    res.push(arr)\n    return;\n  }\n\n  for (let i = index; i < rating.length; i++) {\n    const last = arr[arr.length - 1];\n    const first = arr[0];\n    if (!last || last < rating[i] && last >= first) {\n      arr.push(rating[i])\n      btk(rating, res, arr.concat(), i + 1)\n      arr.pop();\n    } else if (first && last > rating[i] && last <= first) {\n      arr.push(rating[i])\n      btk(rating, res, arr.concat(), i + 1)\n      arr.pop();\n    }\n  }\n}\n```
['Backtracking', 'JavaScript']