File tree Expand file tree Collapse file tree 3 files changed +119
-0
lines changed
binary-tree-level-order-traversal Expand file tree Collapse file tree 3 files changed +119
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } root
11+ * @return {number[][] }
12+ */
13+ var levelOrder = function ( root ) {
14+ if ( ! root ) {
15+ return [ ] ;
16+ }
17+
18+ const queue = [ root ] ;
19+ const answer = [ ] ;
20+
21+ while ( queue . length > 0 ) {
22+ const values = [ ] ;
23+
24+ const currentQueueLen = queue . length ;
25+
26+ for ( let i = 0 ; i < currentQueueLen ; i ++ ) {
27+ const head = queue . shift ( ) ;
28+
29+ values . push ( head . val ) ;
30+
31+ head . left && queue . push ( head . left ) ;
32+ head . right && queue . push ( head . right ) ;
33+ }
34+
35+ answer . push ( values ) ;
36+ }
37+
38+ return answer ;
39+ } ;
40+
41+ // 시간복잡도 O(n) -> 모든 노드를 한번씩 너비우선탐색으로 방문하므로
42+ // 공간복잡도 O(n) -> 큐에 모든 노드의 값을 저장하므로
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number[] }
4+ */
5+ var countBits = function ( n ) {
6+ const arr = [ 0 ] ;
7+
8+ for ( let i = 1 ; i <= n ; i ++ ) {
9+ const num = binary ( i ) ;
10+ arr . push ( num ) ;
11+ }
12+
13+ return arr ;
14+ } ;
15+
16+ /** 성능이 느리지만 간결한 함수 */
17+ function binary ( n ) {
18+ return n . toString ( 2 ) . split ( '' ) . filter ( ( el ) => el === '1' ) . length ;
19+ }
20+
21+ // 시간복잡도 O(n2) -> n을 이진수문자열로 변환하고 이를 벼열화하여 1인 원소만 필터링하고 그 결과의 길이를 구한다.
22+ // 여기서 filter를 사용하여 배열을 한 번 순회하기 때문에 for문과 중첩되어 2중 루프의 시간복잡도를 가짐
23+
24+ /** 성능이 빠르지만 복잡한 함수 */
25+ function binary ( n ) {
26+ let num = 1 ;
27+ let count = 0 ;
28+
29+ while ( num * 2 <= n ) {
30+ num = num * 2 ;
31+ }
32+
33+ while ( 0 <= n ) {
34+ if ( num <= n ) {
35+ n = n - num ;
36+ count ++ ;
37+ }
38+
39+ if ( num === 1 ) {
40+ break ;
41+ }
42+
43+ num = num / 2 ;
44+ }
45+
46+ return count ;
47+ }
48+
49+ // 시간복잡도 O(n2) -> for문 안에 while문이 돌면서 i가 이진수로 변환될 경우 1이 몇개인지 반환하기 때문에 2중 루프의 시간복잡도를 가짐
50+ // 공간복잡도 O(n) -> for문을 돌면서 arr에 i가 이진수로 변환될 경우 1이 몇 개인지 원소로 추가함
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var rob = function ( nums ) {
6+ if ( nums . length === 1 ) {
7+ return nums [ 0 ] ;
8+ }
9+
10+ const dp1 = [ 0 , nums [ 0 ] ] ;
11+ const dp2 = [ 0 , 0 ] ;
12+
13+ for ( let i = 1 ; i < nums . length ; i ++ ) {
14+ const prevIndex = i - 1 ;
15+
16+ const dp1Max = Math . max ( dp1 [ prevIndex ] + nums [ i ] , dp1 [ i ] ) ;
17+ dp1 . push ( dp1Max ) ;
18+
19+ const dp2Max = Math . max ( dp2 [ prevIndex ] + nums [ i ] , dp2 [ i ] ) ;
20+ dp2 . push ( dp2Max ) ;
21+ }
22+
23+ return Math . max ( dp1 [ dp1 . length - 2 ] , dp2 [ dp2 . length - 1 ] )
24+ } ;
25+
26+ // 시간복잡도 O(n) -> nums의 길이만큼 for문에서 최대값을 dp계산
27+ // 공간복잡도 O(n) -> nums의 길이만큼 dp배열에 원소가 추가됨
You can’t perform that action at this time.
0 commit comments