File tree Expand file tree Collapse file tree 3 files changed +125
-0
lines changed
non-overlapping-intervals
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 3 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } intervals
3+ * @return {number }
4+ */
5+ var eraseOverlapIntervals = function ( intervals ) {
6+
7+ const sort = intervals . sort ( ( a , b ) => {
8+ if ( a [ 1 ] === b [ 1 ] ) {
9+ return a [ 0 ] - b [ 0 ] ;
10+ }
11+
12+ return a [ 1 ] - b [ 1 ] ;
13+ } ) ;
14+
15+ let count = 0 ;
16+ let max = sort [ 0 ] [ 1 ] ;
17+
18+ for ( let i = 0 ; i < sort . length - 1 ; i ++ ) {
19+ const right = sort [ i + 1 ] [ 0 ] ;
20+
21+ if ( max > right ) {
22+ count ++ ;
23+ } else {
24+ max = sort [ i + 1 ] [ 1 ] ;
25+ }
26+ }
27+
28+ return count ;
29+ } ;
30+
31+ // ์๊ฐ๋ณต์ก๋ - O(nlogn) ์ธํฐ๋ฒ ๋ท ์ซ์ ์ ๋ ฌ ์ ์ ๋ ฌ๋ก ์ธํ ์๊ฐ๋ณต์ก๋ ๋ฐ์
32+ // ๊ณต๊ฐ๋ณต์ก๋ - O(1) ํน๋ณํ ์๋ฃ๊ตฌ์กฐ๊ฐ ์ฌ์ฉ๋์ง ์์ ์์ ๊ณต๊ฐ๋ณต์ก๋ ๋ฐ์
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * function ListNode(val, next) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.next = (next===undefined ? null : next)
6+ * }
7+ */
8+ /**
9+ * @param {ListNode } head
10+ * @param {number } n
11+ * @return {ListNode }
12+ */
13+ var removeNthFromEnd = function ( head , n ) {
14+ if ( ! head ) {
15+ return null ;
16+ }
17+
18+ let node = head ;
19+
20+ const list = [ ] ;
21+
22+ while ( node ) {
23+ list . push ( node ) ;
24+ node = node . next ;
25+ }
26+
27+ const targetIndex = list . length - n - 1 ;
28+
29+ if ( targetIndex === - 1 && list . length === 1 ) {
30+ return null ;
31+ }
32+
33+ if ( targetIndex === - 1 ) {
34+ return head . next ;
35+ }
36+
37+ if ( list [ targetIndex ] ) {
38+ list [ targetIndex ] . next = list [ targetIndex ] ?. next ?. next || null ;
39+ }
40+
41+ return head ;
42+ } ;
43+
44+ // ์๊ฐ๋ณต์ก๋ O(n) - ๋
ธ๋๋ฅผ ํ๋ฒ ์ํํ์ฌ ๋ฆฌ์คํธ์ ์ ์ฅ
45+ // ๊ณต๊ฐ๋ณต์ก๋ O(n) - ์ํ ์ดํ์ ์ ๊ฑฐํ ๋
ธ๋์ ๋ฐ๋ก ์ ๋
ธ๋๋ ์ ๊ทผํ๊ธฐ ์ํ์ฌ ๋ชจ๋ ๋
ธ๋๋ฅผ ๋ฐฐ์ด์ ์ ์ฅ
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 } p
11+ * @param {TreeNode } q
12+ * @return {boolean }
13+ */
14+ var isSameTree = function ( p , q ) {
15+ const dfs = ( a , b ) => {
16+ const isValSame = a ?. val === b ?. val ;
17+ const isLeftValSame = a ?. left ?. val === b ?. left ?. val ;
18+ const isRightValSame = a ?. right ?. val === b ?. right ?. val ;
19+
20+ if ( ! isValSame || ! isLeftValSame || ! isRightValSame ) {
21+ return true ;
22+ }
23+
24+ if ( a ?. left && b ?. left ) {
25+ const isLeftDiff = dfs ( a . left , b . left ) ;
26+
27+ if ( isLeftDiff ) {
28+ return true ;
29+ }
30+ }
31+ W
32+
33+ if ( a ?. right && b ?. right ) {
34+ const isRightDiff = dfs ( a . right , b . right ) ;
35+
36+ if ( isRightDiff ) {
37+ return true ;
38+ }
39+ }
40+
41+ }
42+
43+
44+ return ! dfs ( p , q ) ;
45+ } ;
46+
47+ // ์๊ฐ๋ณต์ก๋ - O(n) p์ q๊ฐ ๊ฐ๋ค๋ฉด ๋ชจ๋ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฏ๋ก
48+ // ๊ณต๊ฐ๋ณต์ก๋ - O(h) ๊น์ด์ฐ์ ํ์์ ์ฌ์ฉํ์ฌ ํธ๋ฆฌ์ ์ต๋ ๋์ด๋งํผ ์คํํ๊ฒฝ์ด ํจ์ํธ์ถ์คํ์ ์ ์ฅ๋๋ฏ๋ก
You canโt perform that action at this time.
0 commit comments