File tree Expand file tree Collapse file tree 10 files changed +205
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 10 files changed +205
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def containsDuplicate (self , nums : List [int ]) -> bool :
4
+ num_dict = {}
5
+ for num in nums :
6
+ if num in num_dict :
7
+ return True
8
+ else :
9
+ num_dict [num ] = 1
10
+ return False
11
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/contains-duplicate/
3
+ * ํ์ด๋ฐฉ๋ฒ: Set์ ์ด์ฉํ์ฌ ์ค๋ณต๋ ๊ฐ์ด ์๋์ง ํ์ธ
4
+ * ์๊ฐ๋ณต์ก๋: O(n)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ * 1. ๋จ์ํ๊ฒ sorted๋ฅผ ์ด์ฉํ์ฌ ์ด์ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ์ค๋ณต๋ ๊ฐ์ด ์๋์ง ํ์ธ
9
+ * 2. ์ ๋ ฌํ์ง ์๊ณ nums์ ๊ธธ์ด๋งํผ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ์ค๋ณต๋ ๊ฐ์ด ์๋์ง ์ ์ฅํ๋ฉด์ ํ์ธ
10
+ */
11
+ function containsDuplicate ( nums : number [ ] ) : boolean {
12
+ // ์ค๋ณต๋ ๊ฐ์ด ์๋ ์๋ฃ๊ตฌ์กฐ Set ํ์ฉ
13
+ const set = new Set < number > ( nums ) ;
14
+ // Set์ size์ nums์ length๋ฅผ ๋น๊ตํ์ฌ ์ค๋ณต๋ ๊ฐ์ด ์๋์ง ํ์ธ
15
+ return set . size !== nums . length ;
16
+ }
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def rob (self , nums : List [int ]) -> int :
4
+ a = [0 ] * len (nums )
5
+
6
+ if len (nums ) == 1 :
7
+ return nums [0 ]
8
+ elif len (nums ) == 2 :
9
+ return max (nums [0 ], nums [1 ])
10
+
11
+ a [0 ] = nums [0 ]
12
+ a [1 ] = nums [1 ]
13
+ a [2 ] = max (a [0 ] + nums [2 ], a [1 ])
14
+
15
+ for i in range (3 , len (nums )):
16
+ a [i ] = max (a [i - 3 ], a [i - 2 ]) + nums [i ]
17
+
18
+ return max (a )
19
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/house-robber/
3
+ * ํ์ด๋ฐฉ๋ฒ: DP๋ฅผ ์ด์ฉํ์ฌ ์ง์ ํธ ๋ ์ต๋๊ฐ์ ๊ตฌํจ
4
+ * ์๊ฐ๋ณต์ก๋: O(n)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ */
9
+ function rob ( nums : number [ ] ) : number {
10
+ if ( nums . length === 0 ) return 0 ;
11
+ if ( nums . length === 1 ) return nums [ 0 ] ;
12
+ if ( nums . length === 2 ) return Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
13
+
14
+ let prev = nums [ 0 ] ;
15
+ let maxResult = Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
16
+ let current = 0 ;
17
+
18
+ // ๋จ์ ์ง์ ์ํํ๋ฉด์ ์ต๋๊ฐ์ ๊ตฌํจ
19
+ for ( let i = 2 ; i < nums . length ; i ++ ) {
20
+ current = Math . max ( maxResult , prev + nums [ i ] ) ;
21
+ prev = maxResult ;
22
+ maxResult = current ;
23
+ }
24
+ return maxResult ;
25
+ }
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(nlog(n))
2
+ class Solution :
3
+ def longestConsecutive (self , nums : List [int ]) -> int :
4
+ nums = sorted (list (set (nums )))
5
+ if len (nums ) == 0 :
6
+ return 0
7
+ elif len (nums ) == 1 :
8
+ return 1
9
+ cur_long = 1
10
+ longest = 1
11
+ for i , num in enumerate (nums ):
12
+ if i == 0 :
13
+ continue
14
+ else :
15
+ if nums [i - 1 ] + 1 == nums [i ]:
16
+ cur_long += 1
17
+ if longest < cur_long :
18
+ longest = cur_long
19
+ else :
20
+ cur_long = 1
21
+ return longest
22
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/longest-consecutive-sequence/
3
+ * ํ์ด๋ฐฉ๋ฒ: ์ ๋ ฌ ํ ์ํ๋ฅผ ํตํด ์ฐ์๋ ๊ฐ์ด ์๋์ง ํ์ธ
4
+ * ์๊ฐ๋ณต์ก๋: O(nlogn)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(1)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ */
9
+
10
+ function longestConsecutive ( nums : number [ ] ) : number {
11
+ if ( nums . length === 0 ) return 0 ;
12
+ const sorted = nums . sort ( ( a , b ) => a - b ) ;
13
+ let prev = sorted [ 0 ] ;
14
+ let result = 1 ;
15
+ let candiResult = 1 ;
16
+
17
+ for ( let current of sorted ) {
18
+ if ( prev === current ) continue ;
19
+ if ( current === prev + 1 ) {
20
+ candiResult += 1 ;
21
+ } else {
22
+ if ( candiResult > result ) {
23
+ result = candiResult ;
24
+ }
25
+ candiResult = 1 ;
26
+ }
27
+ prev = current ;
28
+ }
29
+
30
+ if ( candiResult > result ) result = candiResult ;
31
+ return result ;
32
+ }
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(nlog(n))
2
+ import heapq
3
+ class Solution :
4
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
5
+
6
+ num_dict = {}
7
+ for num in nums :
8
+ num_dict [num ] = num_dict .get (num , 0 ) + 1
9
+ heap = []
10
+ for k_ , v in num_dict .items ():
11
+ heapq .heappush (heap , [- v , k_ ])
12
+ ans = []
13
+ for i in range (k ):
14
+ ans .append (heapq .heappop (heap )[1 ])
15
+ return ans
16
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/top-k-frequent-elements/
3
+ * ํ์ด๋ฐฉ๋ฒ: ์ํ๋ฅผ ํตํด ๋น๋์๋ฅผ ์ ์ฅ, Object.entries๋ฅผ ํตํด ์ ๋ ฌํ์ฌ k๊ฐ๊น์ง ๋ฐํ
4
+ * ์๊ฐ๋ณต์ก๋: O(nlogn)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ */
9
+ function topKFrequent ( nums : number [ ] , k : number ) : number [ ] {
10
+ const KFrequentObject = new Object ( ) ;
11
+ const result = new Array ( ) ;
12
+ for ( let num of nums ) {
13
+ if ( ! Object . hasOwn ( KFrequentObject , num ) ) KFrequentObject [ num ] = 0 ;
14
+ KFrequentObject [ num ] ++ ;
15
+ }
16
+ // Object.entries๋ฅผ ํตํด key, value๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ (ํฌ์ธํธ)
17
+ let sorted = Object . entries ( KFrequentObject ) . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] ) ;
18
+ for ( let node of sorted ) {
19
+ result . push ( parseInt ( node [ 0 ] ) ) ;
20
+ }
21
+ return result . slice ( 0 , k ) ;
22
+ }
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def isPalindrome (self , s : str ) -> bool :
4
+ s = "" .join (s .lower ().split (" " ))
5
+ new_s = ""
6
+ for item in s :
7
+ if (ord ("a" ) <= ord (item ) <= ord ("z" )) or (ord ("0" ) <= ord (item ) <= ord ("9" )):
8
+ new_s += item
9
+ output = True
10
+ new_s_2 = new_s [::- 1 ]
11
+ return new_s_2 == new_s
12
+ return output
13
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Source: https://leetcode.com/problems/valid-palindrome/
3
+ * ํ์ด๋ฐฉ๋ฒ: ๋ฌธ์์ด์ ์กฐ๊ฑด์ ๋ง๊ฒ ์ ์ ํ ํ reverseํ์ฌ ๋น๊ต
4
+ * ์๊ฐ๋ณต์ก๋: O(n)
5
+ * ๊ณต๊ฐ๋ณต์ก๋: O(n)
6
+ *
7
+ * ์๊ฐ๋๋ ํ์ด๋ฐฉ๋ฒ
8
+ * 1. ์ ๊ท์์ ์ด์ฉํ์ฌ ๋ฌธ์์ด์ ์ ์ ํ ํ reverseํ์ฌ ๋น๊ต => ์ ๊ท์ ์์ฑ์ ๋ชปํด์ ๋ฐฐ์
9
+ * 2. ๋ฌธ์์ด์ ์ํํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ๋ฌธ์๋ง ๋ฐฐ์ด์ ์ ์ฅํ ํ reverseํ์ฌ ๋น๊ต
10
+ */
11
+ function isPalindrome ( s : string ) : boolean {
12
+ // ๋ฏธ๋ฆฌ ๊ธธ์ด๊ฐ 2๋ณด๋ค ์์ ๊ฒฝ์ฐ๋ true๋ก ๋ฐํ(Palindrome ์กฐ๊ฑด์ ๋ง์)
13
+ if ( s . length < 2 ) return true ;
14
+ const formatted : string [ ] = [ ] ;
15
+
16
+ // ๋ฌธ์์ด์ ์ํํ๋ฉด์ ์กฐ๊ฑด์ ๋ง๋ ๋ฌธ์๋ง ์๋ฌธ์๋ก ๋ณํํด์ ๋ฐฐ์ด์ ์ ์ฅ
17
+ for ( let c of s ) {
18
+ if (
19
+ ( c >= "a" && c <= "z" ) ||
20
+ ( c >= "A" && c <= "Z" ) ||
21
+ ( c >= "0" && c <= "9" )
22
+ )
23
+ formatted . push ( c . toLowerCase ( ) ) ;
24
+ }
25
+ // formatted ๋ฐฐ์ด์ reverseํ์ฌ JSON.stringify๋ก ๋น๊ต (์ค์)
26
+ // ? toReverse()๊ฐ chrome console์์๋ ์๋ํ๋๋ฐ ์ฌ๊ธฐ์๋ ์ ์๋๋์ง ๋ชจ๋ฅด๊ฒ ์
27
+ const reversed = [ ...formatted ] . reverse ( ) ;
28
+ return JSON . stringify ( reversed ) === JSON . stringify ( formatted ) ;
29
+ }
You canโt perform that action at this time.
0 commit comments