File tree Expand file tree Collapse file tree 4 files changed +137
-0
lines changed
kth-smallest-element-in-a-bst
lowest-common-ancestor-of-a-binary-search-tree Expand file tree Collapse file tree 4 files changed +137
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/insert-interval/submissions/1678158074/
3+ * @param {number[][] } intervals
4+ * @param {number[] } newInterval
5+ * @return {number[][] }
6+ */
7+ var insert = function ( intervals , newInterval ) {
8+ const result = [ ] ;
9+ let i = 0 ;
10+ const n = intervals . length ;
11+
12+ // 1. newIntervalλ³΄λ€ λλλ μμ μ΄ λ¨Όμ μΈ intervalμ κ·Έλ₯ μΆκ°
13+ while ( i < n && intervals [ i ] [ 1 ] < newInterval [ 0 ] ) {
14+ result . push ( intervals [ i ] ) ;
15+ i ++ ;
16+ }
17+
18+ // 2. newIntervalκ³Ό κ²ΉμΉλ intervalμ λ³ν©
19+ while ( i < n && intervals [ i ] [ 0 ] <= newInterval [ 1 ] ) {
20+ newInterval [ 0 ] = Math . min ( newInterval [ 0 ] , intervals [ i ] [ 0 ] ) ;
21+ newInterval [ 1 ] = Math . max ( newInterval [ 1 ] , intervals [ i ] [ 1 ] ) ;
22+ i ++ ;
23+ }
24+ result . push ( newInterval ) ;
25+
26+ // 3. λλ¨Έμ§ intervalμ κ·Έλλ‘ μΆκ°
27+ while ( i < n ) {
28+ result . push ( intervals [ i ] ) ;
29+ i ++ ;
30+ }
31+
32+ return result ;
33+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/kth-smallest-element-in-a-bst/
3+ * Definition for a binary tree node.
4+ * function TreeNode(val, left, right) {
5+ * this.val = (val===undefined ? 0 : val)
6+ * this.left = (left===undefined ? null : left)
7+ * this.right = (right===undefined ? null : right)
8+ * }
9+ */
10+ /**
11+ * @param {TreeNode } root
12+ * @param {number } k
13+ * @return {number }
14+ */
15+ var kthSmallest = function ( root , k ) {
16+ // μ€μ μν κ²°κ³Όλ₯Ό μ μ₯ν λ°°μ΄
17+ const inorder = [ ] ;
18+
19+ // μ€μ μν ν¨μ μ μ
20+ function traverse ( node ) {
21+ if ( ! node ) return ;
22+ traverse ( node . left ) ; // μΌμͺ½ μλΈνΈλ¦¬ λ°©λ¬Έ
23+ inorder . push ( node . val ) ; // νμ¬ λ
Έλ κ° μΆκ°
24+ traverse ( node . right ) ; // μ€λ₯Έμͺ½ μλΈνΈλ¦¬ λ°©λ¬Έ
25+ }
26+
27+ // νΈλ¦¬ μν μμ
28+ traverse ( root ) ;
29+
30+ // kλ²μ§Έλ‘ μμ κ° λ°ν (1-indexed μ΄λ―λ‘ k-1)
31+ return inorder [ k - 1 ] ;
32+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
3+ * Definition for a binary tree node.
4+ * function TreeNode(val) {
5+ * this.val = val;
6+ * this.left = this.right = null;
7+ * }
8+ */
9+
10+ /**
11+ * @param {TreeNode } root
12+ * @param {TreeNode } p
13+ * @param {TreeNode } q
14+ * @return {TreeNode }
15+ */
16+ var lowestCommonAncestor = function ( root , p , q ) {
17+ let current = root ;
18+
19+ while ( current ) {
20+ // λ λ
Έλμ κ°μ΄ νμ¬ λ
Έλλ³΄λ€ λͺ¨λ μμΌλ©΄ μΌμͺ½ μλΈνΈλ¦¬λ‘ μ΄λ
21+ if ( p . val < current . val && q . val < current . val ) {
22+ current = current . left ;
23+ }
24+ // λ λ
Έλμ κ°μ΄ νμ¬ λ
Έλλ³΄λ€ λͺ¨λ ν¬λ©΄ μ€λ₯Έμͺ½ μλΈνΈλ¦¬λ‘ μ΄λ
25+ else if ( p . val > current . val && q . val > current . val ) {
26+ current = current . right ;
27+ }
28+ // νμ¬ λ
Έλκ° λΆκΈ°μ μ΄λ©΄ κ·Έκ² LCA!
29+ else {
30+ return current ;
31+ }
32+ }
33+
34+ return null ; // λ§μ½ νΈλ¦¬μ μλ€λ©΄ null λ°ν (μΌλ°μ μΌλ‘ BSTμλ νμ μ‘΄μ¬νλ€κ³ κ°μ )
35+ } ;
Original file line number Diff line number Diff line change 1+ import { Interval } from '/opt/node/lib/lintcode/index.js' ;
2+
3+ /**
4+ * https://www.lintcode.com/problem/920/
5+ * Definition of Interval:
6+ * class Interval {
7+ * constructor(start, end) {
8+ * this.start = start;
9+ * this.end = end;
10+ * }
11+ * }
12+ */
13+
14+ export class Solution {
15+ /**
16+ * @param intervals: an array of meeting time intervals
17+ * @return : if a person could attend all meetings
18+ */
19+ canAttendMeetings ( intervals ) {
20+ // λ¨Όμ νμλ₯Ό μμ μκ° κΈ°μ€μΌλ‘ μ λ ¬ν©λλ€.
21+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
22+
23+ // μ λ ¬λ νμλ€μ μμ°¨μ μΌλ‘ λΉκ΅νλ©° κ²ΉμΉλμ§ νμΈν©λλ€.
24+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
25+ const prevEnd = intervals [ i - 1 ] [ 1 ] ; // μ΄μ νμμ λ μκ°
26+ const currStart = intervals [ i ] [ 0 ] ; // νμ¬ νμμ μμ μκ°
27+
28+ // μ΄μ νμμ λ μκ°λ³΄λ€ νμ¬ νμμ μμ μκ°μ΄ λΉ λ₯΄λ©΄ κ²ΉμΉ©λλ€.
29+ if ( currStart < prevEnd ) {
30+ return false ;
31+ }
32+ }
33+
34+ // κ²ΉμΉλ νμκ° μμΌλ©΄ trueλ₯Ό λ°νν©λλ€.
35+ return true ;
36+ }
37+ }
You canβt perform that action at this time.
0 commit comments