File tree Expand file tree Collapse file tree 5 files changed +124
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +124
-0
lines changed 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+ /**
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+ /**
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+ /**
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+ /**
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