File tree Expand file tree Collapse file tree 4 files changed +170
-0
lines changed
longest-palindromic-substring
validate-binary-search-tree Expand file tree Collapse file tree 4 files changed +170
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * TC: O(N^2)
3+ * 주어진 s 문자열이 한 종류의 문자로 이루어져있다면 for문에서 O(N), while문에서 O(N) 이므로 O(N * 2N)
4+ *
5+ * SC: O(1)
6+ */
7+
8+ /**
9+ * @param {string } s
10+ * @return {string }
11+ */
12+ var longestPalindrome = function ( s ) {
13+ let result = "" ;
14+
15+ for ( let index = 0 ; index < s . length ; index ++ ) {
16+ const [ start1 , end1 ] = getPalindromicSubstringLength ( index , index ) ;
17+ const [ start2 , end2 ] = getPalindromicSubstringLength ( index , index + 1 ) ;
18+
19+ if ( result . length < end1 - start1 + 1 ) {
20+ result = s . substring ( start1 , end1 + 1 ) ;
21+ }
22+
23+ if ( result . length < end2 - start2 + 1 ) {
24+ result = s . substring ( start2 , end2 + 1 ) ;
25+ }
26+ }
27+
28+ return result ;
29+
30+ function getPalindromicSubstringLength ( start , end ) {
31+ while ( 0 <= start && end < s . length && s [ start ] === s [ end ] ) {
32+ start -= 1 ;
33+ end += 1 ;
34+ }
35+
36+ return [ start + 1 , end - 1 ] ;
37+ }
38+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 전치(행과 열을 교환) 후 행 반전(뒤집기)
3+ *
4+ * TC: O(N^2)
5+ * SC: O(1)
6+ */
7+
8+ /**
9+ * @param {number[][] } matrix
10+ * @return {void } Do not return anything, modify matrix in-place instead.
11+ */
12+ var rotate = function ( matrix ) {
13+ const N = matrix . length ;
14+
15+ for ( let row = 0 ; row < N ; row ++ ) {
16+ for ( let column = row ; column < N ; column ++ ) {
17+ [ matrix [ row ] [ column ] , matrix [ column ] [ row ] ] = [
18+ matrix [ column ] [ row ] ,
19+ matrix [ row ] [ column ] ,
20+ ] ;
21+ }
22+ }
23+
24+ for ( let row = 0 ; row < N ; row ++ ) {
25+ matrix [ row ] . reverse ( ) ;
26+ }
27+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * tree를 순회하면서 subRoot의 시작값과 동일한 노드를 찾습니다.
3+ * 찾으면 동일한 트리인지 확인합니다.
4+ *
5+ * TC: O(N)
6+ * 최악의 경우, root tree의 모든 node를 순회합니다.
7+ *
8+ * SC: O(N)
9+ * 최악의 경우, root tree의 모든 node를 순회하기 위해 queue를 사용합니다.
10+ */
11+
12+ /**
13+ * Definition for a binary tree node.
14+ * function TreeNode(val, left, right) {
15+ * this.val = (val===undefined ? 0 : val)
16+ * this.left = (left===undefined ? null : left)
17+ * this.right = (right===undefined ? null : right)
18+ * }
19+ */
20+ /**
21+ * @param {TreeNode } root
22+ * @param {TreeNode } subRoot
23+ * @return {boolean }
24+ */
25+ var isSubtree = function ( root , subRoot ) {
26+ const queue = [ root ] ;
27+
28+ while ( queue . length > 0 ) {
29+ const current = queue . shift ( ) ;
30+
31+ if ( ! current ) {
32+ continue ;
33+ }
34+
35+ if ( current . val === subRoot . val && isSameTree ( current , subRoot ) ) {
36+ return true ;
37+ }
38+
39+ if ( current . left ) {
40+ queue . push ( current . left ) ;
41+ }
42+
43+ if ( current . right ) {
44+ queue . push ( current . right ) ;
45+ }
46+ }
47+
48+ return false ;
49+
50+ function isSameTree ( rootA , rootB ) {
51+ if ( rootA === null && rootB === null ) {
52+ return true ;
53+ }
54+
55+ if ( rootA === null || rootB === null ) {
56+ return false ;
57+ }
58+
59+ return (
60+ rootA . val === rootB . val &&
61+ isSameTree ( rootA . left , rootB . left ) &&
62+ isSameTree ( rootA . right , rootB . right )
63+ ) ;
64+ }
65+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * TC: O(N)
3+ * SC: O(N)
4+ * N: total count of tree nodes
5+ */
6+
7+ /**
8+ * Definition for a binary tree node.
9+ * function TreeNode(val, left, right) {
10+ * this.val = (val===undefined ? 0 : val)
11+ * this.left = (left===undefined ? null : left)
12+ * this.right = (right===undefined ? null : right)
13+ * }
14+ */
15+ /**
16+ * @param {TreeNode } root
17+ * @return {boolean }
18+ */
19+ var isValidBST = function ( root ) {
20+ return isValidBSTWithBoundary (
21+ root ,
22+ Number . MIN_SAFE_INTEGER ,
23+ Number . MAX_SAFE_INTEGER
24+ ) ;
25+
26+ function isValidBSTWithBoundary ( current , min , max ) {
27+ if ( ! current ) {
28+ return true ;
29+ }
30+
31+ if ( current . val <= min || max <= current . val ) {
32+ return false ;
33+ }
34+
35+ return (
36+ isValidBSTWithBoundary ( current . left , min , current . val ) &&
37+ isValidBSTWithBoundary ( current . right , current . val , max )
38+ ) ;
39+ }
40+ } ;
You can’t perform that action at this time.
0 commit comments