Line data Source code
1 : // Copyright (c) 2024 The Authors. All rights reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : // Authors: liubang (it.liubang@gmail.com)
16 :
17 : #include <gtest/gtest.h>
18 :
19 : #include <vector>
20 :
21 : namespace {
22 : class Solution {
23 : public:
24 3 : int uniquePaths(int m, int n) {
25 3 : std::vector<std::vector<int>> dp(m, std::vector<int>(n, 0));
26 19 : for (int i = 0; i < m; ++i) {
27 113 : for (int j = 0; j < n; ++j) {
28 97 : dp[i][j] = (i == 0 || j == 0) ? 1 : dp[i - 1][j] + dp[i][j - 1];
29 : }
30 : }
31 6 : return dp[m - 1][n - 1];
32 3 : }
33 : };
34 : } // namespace
35 :
36 4 : TEST(Leetcode, unique_paths) {
37 1 : Solution s;
38 1 : EXPECT_EQ(28, s.uniquePaths(3, 7));
39 1 : EXPECT_EQ(3, s.uniquePaths(3, 2));
40 1 : EXPECT_EQ(5005, s.uniquePaths(10, 7));
41 1 : }
|