From 76518783d44b9c94a60779978c2e66a8870d71c7 Mon Sep 17 00:00:00 2001 From: Yu Nakamura Date: Wed, 22 Apr 2026 10:32:59 +0900 Subject: [PATCH] add files --- 0062-unique-paths/memo.md | 11 +++++++++++ 0062-unique-paths/step1.cpp | 34 ++++++++++++++++++++++++++++++++++ 0062-unique-paths/step2.cpp | 14 ++++++++++++++ 0062-unique-paths/step3.cpp | 14 ++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 0062-unique-paths/memo.md create mode 100644 0062-unique-paths/step1.cpp create mode 100644 0062-unique-paths/step2.cpp create mode 100644 0062-unique-paths/step3.cpp diff --git a/0062-unique-paths/memo.md b/0062-unique-paths/memo.md new file mode 100644 index 0000000..aee21e1 --- /dev/null +++ b/0062-unique-paths/memo.md @@ -0,0 +1,11 @@ +### step1 + +BFSでマスを管理しながら左と上のマスの経路数を足し合わせた。 + +### step2 + +ChatGPTを参考に、DPで解く解き方に変更。 + +### step3 + +3回通すまで書き直し。 diff --git a/0062-unique-paths/step1.cpp b/0062-unique-paths/step1.cpp new file mode 100644 index 0000000..ea0dc75 --- /dev/null +++ b/0062-unique-paths/step1.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> ways(m, vector(n, 0)); + ways[0][0] = 1; + + queue> grids; + grids.emplace(0, 0); + + while (!grids.empty()) { + auto[row, column] = grids.front(); + grids.pop(); + + if (!(row == 0 && column == 0) && ways[row][column] != 0) { + continue; + } + + if (row - 1 >= 0) { + ways[row][column] += ways[row - 1][column]; + } + if (column - 1 >= 0) { + ways[row][column] += ways[row][column - 1]; + } + + if (row + 1 < m) { + grids.emplace(row + 1, column); + } + if (column + 1 < n) { + grids.emplace(row, column + 1); + } + } + return ways[m - 1][n - 1]; + } +}; diff --git a/0062-unique-paths/step2.cpp b/0062-unique-paths/step2.cpp new file mode 100644 index 0000000..1d27e45 --- /dev/null +++ b/0062-unique-paths/step2.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> ways(m, vector(n, 1)); + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + ways[i][j] = ways[i - 1][j] + ways[i][j - 1]; + } + } + + return ways[m - 1][n - 1]; + } +}; diff --git a/0062-unique-paths/step3.cpp b/0062-unique-paths/step3.cpp new file mode 100644 index 0000000..1d27e45 --- /dev/null +++ b/0062-unique-paths/step3.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int uniquePaths(int m, int n) { + vector> ways(m, vector(n, 1)); + + for (int i = 1; i < m; i++) { + for (int j = 1; j < n; j++) { + ways[i][j] = ways[i - 1][j] + ways[i][j - 1]; + } + } + + return ways[m - 1][n - 1]; + } +};