File tree Expand file tree Collapse file tree 5 files changed +188
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 5 files changed +188
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문자열에서 중복 문자 없는 가장 긴 부분 문자열의 길이를 반환하는 함수
3
+ * @param {string } s
4
+ * @return {number }
5
+ */
6
+ const lengthOfLongestSubstring = function ( s ) {
7
+ let start = 0 ;
8
+ let end = 0 ;
9
+
10
+ const set = new Set ( ) ;
11
+ let maxSize = 0 ;
12
+
13
+ while ( end < s . length ) {
14
+ while ( set . has ( s [ end ] ) ) {
15
+ set . delete ( s [ start ] ) ;
16
+ start ++ ;
17
+ }
18
+
19
+ set . add ( s [ end ] ) ;
20
+ maxSize = Math . max ( maxSize , set . size ) ;
21
+ end ++ ;
22
+ }
23
+
24
+ return maxSize ;
25
+ } ;
26
+
27
+ // 시간복잡도: O(n) (최대 end로 n번, start로 n번 이동하므로 2n만큼 소요)
28
+ // 공간복잡도: O(n)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 2차원 격자에서 섬의 개수를 반환하는 함수
3
+ * 시간복잡도: O(m * n) (모든 원소를 순회해야 함)
4
+ * 공간복잡도: O(m * n) (bfs의 공간복잡도에 따름)
5
+ * @param {character[][] } grid
6
+ * @return {number }
7
+ */
8
+ const numIslands = function ( grid ) {
9
+ let num = 0 ;
10
+
11
+ for ( let r = 0 ; r < grid . length ; r ++ ) {
12
+ for ( let c = 0 ; c < grid [ 0 ] . length ; c ++ ) {
13
+ if ( grid [ r ] [ c ] === '1' ) {
14
+ bfs ( grid , r , c ) ;
15
+ num += 1 ;
16
+ }
17
+ }
18
+ }
19
+
20
+ return num ;
21
+ } ;
22
+
23
+ // grid의 주어진 좌표에서 bfs를 수행해 방문했음을 표시
24
+ // 시간복잡도: O(m * n) (최악의 경우 모든 원소 순회)
25
+ // 공간복잡도: O(m * n) (최악의 경우 queue에 모든 원소 저장)
26
+ function bfs ( grid , x , y ) {
27
+ const queue = [ [ x , y ] ] ;
28
+ const dx = [ 0 , 0 , 1 , - 1 ] ;
29
+ const dy = [ 1 , - 1 , 0 , 0 ] ;
30
+ const rows = grid . length ;
31
+ const cols = grid [ 0 ] . length ;
32
+
33
+ while ( queue . length ) {
34
+ const [ r , c ] = queue . shift ( ) ;
35
+
36
+ for ( let i = 0 ; i < 4 ; i ++ ) {
37
+ const nr = r + dx [ i ] ;
38
+ const nc = c + dy [ i ] ;
39
+
40
+ if ( 0 <= nr && nr < rows && 0 <= nc && nc < cols && grid [ nr ] [ nc ] === '1' ) {
41
+ queue . push ( [ nr , nc ] ) ;
42
+ grid [ nr ] [ nc ] = '0' ;
43
+ }
44
+ }
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ function ListNode ( val , next ) {
2
+ this . val = ( val === undefined ? 0 : val )
3
+ this . next = ( next === undefined ? null : next )
4
+ }
5
+
6
+ // 첫 번째 시도
7
+ // 시간복잡도: O(n)
8
+ // 공간복잡도: O(n)
9
+ /**
10
+ * 단방향 연결 리스트를 reverse하여 반환하는 함수
11
+ * @param {ListNode } head
12
+ * @return {ListNode }
13
+ */
14
+ const reverseList = function ( head ) {
15
+ const newHead = new ListNode ( ) ;
16
+
17
+ function _reverseList ( head ) {
18
+ if ( ! head ) {
19
+ return newHead ;
20
+ }
21
+
22
+ const reversedHead = _reverseList ( head . next ) ;
23
+ reversedHead . next = new ListNode ( head . val , null ) ;
24
+
25
+ return reversedHead . next ;
26
+ }
27
+
28
+ _reverseList ( head ) ;
29
+ return newHead . next ;
30
+ } ;
31
+
32
+
33
+ // 두 번째 시도
34
+ // 시간복잡도: O(n)
35
+ // 공간복잡도: O(1)
36
+ const reverseList = function ( head ) {
37
+ let current = head ;
38
+ let prev = null ;
39
+
40
+ while ( current ) {
41
+ const next = current . next ;
42
+ current . next = prev ;
43
+ prev = current ;
44
+ current = next ;
45
+ }
46
+
47
+ return prev ;
48
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 주어진 격자에서 원소가 0인 행과 열의 값을 0으로 수정하는 함수
3
+ * @param {number[][] } matrix
4
+ * @return {void } Do not return anything, modify matrix in-place instead.
5
+ */
6
+ const setZeroes = function ( matrix ) {
7
+ const m = matrix . length ;
8
+ const n = matrix [ 0 ] . length ;
9
+ const rows = new Set ( ) ;
10
+ const cols = new Set ( ) ;
11
+
12
+ for ( let r = 0 ; r < m ; r ++ ) {
13
+ for ( let c = 0 ; c < n ; c ++ ) {
14
+ if ( matrix [ r ] [ c ] === 0 ) {
15
+ rows . add ( r ) ;
16
+ cols . add ( c ) ;
17
+ }
18
+ }
19
+ }
20
+
21
+ rows . forEach ( ( row ) => matrix [ row ] = Array ( n ) . fill ( 0 ) ) ;
22
+ cols . forEach ( ( col ) => {
23
+ for ( let r = 0 ; r < m ; r ++ ) {
24
+ matrix [ r ] [ col ] = 0 ;
25
+ }
26
+ } ) ;
27
+ } ;
28
+
29
+ // 시간복잡도: O(m * n)
30
+ // 공간복잡도: O(m + n)
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