File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * (0,0)에서 (m,n)에 도달할 수 있는 방법의 수를 반환하는 함수
3
+ * @param {number } m
4
+ * @param {number } n
5
+ * @return {number }
6
+ */
7
+ const uniquePaths = function ( m , n ) {
8
+ const grid = Array . from ( { length : m } , ( ) => Array ( n ) . fill ( 0 ) ) ;
9
+ let r = 0 ;
10
+ let c = 0 ;
11
+ const queue = [ [ r , c ] ] ;
12
+ grid [ r ] [ c ] = 1 ;
13
+
14
+ while ( queue . length ) {
15
+ const [ x , y ] = queue . shift ( ) ;
16
+
17
+ if ( x === m - 1 && y === n - 1 ) {
18
+ continue ;
19
+ }
20
+
21
+ if ( 0 <= x + 1 && x + 1 < m ) {
22
+ if ( grid [ x + 1 ] [ y ] === 0 ) queue . push ( [ x + 1 , y ] ) ;
23
+ grid [ x + 1 ] [ y ] += grid [ x ] [ y ] ;
24
+ }
25
+
26
+ if ( 0 <= y + 1 && y + 1 < n ) {
27
+ if ( grid [ x ] [ y + 1 ] === 0 ) queue . push ( [ x , y + 1 ] ) ;
28
+ grid [ x ] [ y + 1 ] += grid [ x ] [ y ] ;
29
+ }
30
+ }
31
+
32
+ return grid [ m - 1 ] [ n - 1 ] ;
33
+ } ;
34
+
35
+ // 시간복잡도: O(m * n)
36
+ // 공간복잡도: O(m * n)
You can’t perform that action at this time.
0 commit comments