穷举DFS算法

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
  </head>
  <body>
    <h1>
      1.篮球赛,10人,每个人有一个战斗力n1-n10
      分成两组(人数相等),战斗力差值最小
    </h1>
    <script type="text/javascript">
      //输入:10 9 8 7 6 5 4 3 2 1
      // 输出:1
      let arr = "10 9 8 7 6 5 4 3 2 1 8 2".split(" ");
      let total = arr.reduce((acc, item) => {
        return acc + +item;
      }, 0);
      let res = total;
      function dfs(doArr, used, arr) {
        if (doArr.length === arr.length / 2) {
          let tmpTotal = doArr.reduce((acc, item) => {
            return acc + +item;
          }, 0);
          res = Math.min(Math.abs(tmpTotal * 2 - total), res);
        } else {
          for (let i = 0; i < arr.length; i++) {
            if (!used[i]) {
              used[i] = true;
              doArr.push(arr[i]);
              dfs(doArr, used, arr);
              used[i] = false;
              doArr.pop();
            }
          }
        }
      }
      dfs([], {}, arr);
      console.log(res);
    </script>
  </body>
</html>