Files
freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/project-euler/problem-15-lattice-paths.md
2022-10-18 12:59:49 +05:30

1.3 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5900f37b1000cf542c50fe8e 问题 15网格路径 1 301780 problem-15-lattice-paths

--description--

从一个 2 × 2 的网格左上角出发,规定只允许向右或向下移动,则到达右下角的路径恰好有 6 条。

图片展示了到达 2 × 2 网格右下角的所有 6 条路径

给定网格大小为 gridSize,请求出这样的路径有多少条?

--hints--

latticePaths(4) 应该返回一个数字。

assert(typeof latticePaths(4) === 'number');

latticePaths(4) 应该返回 70。

assert.strictEqual(latticePaths(4), 70);

latticePaths(9) 应该返回 48620。

assert.strictEqual(latticePaths(9), 48620);

latticePaths(20) 应该返回 137846528820。

assert.strictEqual(latticePaths(20), 137846528820);

--seed--

--seed-contents--

function latticePaths(gridSize) {

  return true;
}

latticePaths(4);

--solutions--

function latticePaths(gridSize) {
  let paths = 1;

  for (let i = 0; i < gridSize; i++) {
    paths *= (2 * gridSize) - i;
    paths /= i + 1;
  }
  return paths;
}