File tree Expand file tree Collapse file tree 2 files changed +67
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 2 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 중복된 문자가 없는 가장 긴 부분 문자열 구하기
3
+ * 달레 알고리즘 해설을 참고하여 작성했습니다
4
+ * 슬라이딩 윈도우 방식 적용
5
+ * 알고리즘 복잡도
6
+ * - 시간 복잡도: O(n)
7
+ * - 공간 복잡도: O(n)
8
+ * @param s
9
+ */
10
+ function lengthOfLongestSubstring ( s : string ) : number {
11
+ const set = new Set < string > ( ) ;
12
+ let start = 0 ;
13
+ let end = 0 ;
14
+ let maxLength = 0 ;
15
+
16
+ while ( end < s . length ) {
17
+ if ( set . has ( s [ end ] ) ) {
18
+ set . delete ( s [ start ] )
19
+ start ++
20
+ } else {
21
+ set . add ( s [ end ] )
22
+ maxLength = Math . max ( maxLength , set . size )
23
+ end ++
24
+ }
25
+ }
26
+ return maxLength
27
+ }
28
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 2차 배열의 1로된 섬 구하기
3
+ * 달레 알고리즘 해석을 참고하여 작성했습니다.
4
+ * DFS(깊이 우선 알고리즘)
5
+ * 알고리즘 복잡도
6
+ * - 시간 복잡도: O(m x n)
7
+ * - 공간 복잡도: O(m x n)
8
+ * @param grid
9
+ */
10
+ function numIslands ( grid : string [ ] [ ] ) : number {
11
+ const rows = grid . length
12
+ const cols = grid [ 0 ] . length
13
+ let islands = 0
14
+
15
+ // 인접한 땅의 1을 찾아야 함 - 상하좌우
16
+ const dfs = ( i : number , j : number ) : void => {
17
+ if ( i < 0 || i >= rows || j < 0 || j >= cols || grid [ i ] [ j ] !== '1' ) {
18
+ return
19
+ }
20
+
21
+ grid [ i ] [ j ] = '0' // 한번 확인한 경우 물로 바꿔줌
22
+
23
+ dfs ( i + 1 , j )
24
+ dfs ( i - 1 , j )
25
+ dfs ( i , j + 1 )
26
+ dfs ( i , j - 1 )
27
+ }
28
+
29
+ for ( let i = 0 ; i < rows ; i ++ ) {
30
+ for ( let j = 0 ; j < cols ; j ++ ) {
31
+ if ( grid [ i ] [ j ] === '1' ) {
32
+ dfs ( i , j )
33
+ islands ++
34
+ }
35
+ }
36
+ }
37
+
38
+ return islands
39
+ }
You can’t perform that action at this time.
0 commit comments