File tree Expand file tree Collapse file tree
longest-consecutive-sequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const containsDuplicate = function ( nums ) {
2+ const data = new Set ( nums ) ;
3+ return data . size !== nums . length ;
4+ } ;
Original file line number Diff line number Diff line change 1+ const rob = function ( nums ) {
2+ // ์ต๋ ๋์ ๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด
3+ const data = [ ] ;
4+ data [ 0 ] = nums [ 0 ] ;
5+
6+ for ( let i = 1 ; i < nums . length ; i ++ ) {
7+ data [ i ] = Math . max ( data [ i - 1 ] , ( data [ i - 2 ] ?? 0 ) + nums [ i ] ) ;
8+ }
9+
10+ return data [ data . length - 1 ] ;
11+ } ;
Original file line number Diff line number Diff line change 1+ var longestConsecutive = function ( nums ) {
2+ // ์ซ์ ์กด์ฌ ์ ๋ฌด๋ฅผ ํ์ธํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ ์ธํ
3+ const dataSet = new Set ( nums ) ;
4+ let answer = 0 ;
5+
6+ // ์ ์ฒด ์ซ์๋ฅผ ์ํํ๋ฉด์ ์์์ ์ ํ์ธ
7+ for ( let num of dataSet ) {
8+ // num - 1์ด dataSet์ ์๋์ง ํ์ธ. ์๋ค๋ฉด ์์์ ์ด ์๋๋ฏ๋ก ํจ์ค
9+ if ( ! dataSet . has ( num - 1 ) ) {
10+ // ์๋ค๋ฉด ์์์ . ์ฌ๊ธฐ์๋ถํฐ ์ฐ์๋ ์ซ์๊ฐ ์ผ๋ง๋ ์๋์ง ์นด์ดํ
.
11+ let count = 1 ;
12+ let target = num ;
13+ while ( dataSet . has ( target + 1 ) ) {
14+ count ++ ;
15+ target ++ ;
16+ }
17+ // longest๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ฏ๋ก max๋ก ๋ ๊ธด ๋ต์ ํ๋จ
18+ answer = Math . max ( count , answer ) ;
19+ }
20+ }
21+ return answer ;
22+ } ;
Original file line number Diff line number Diff line change 1+ const topKFrequent = function ( nums , k ) {
2+ const tempArr = Array . from ( { length : nums . length } , ( ) => [ ] ) ;
3+ const obj = { } ;
4+
5+ for ( let num of nums ) {
6+ obj [ num ] = ( obj [ num ] || 0 ) + 1 ;
7+ }
8+
9+ for ( let key in obj ) {
10+ const val = obj [ key ] - 1 ;
11+ tempArr [ val ] . push ( + key ) ;
12+ }
13+
14+ // answer.flat(), return answer.slice(-k) ๋์ ์ข ๋ ์ต์ ํ๋ ์ฝ๋๋ก ๋ณ๊ฒฝํฉ๋๋ค.
15+ const answer = [ ] ;
16+ for ( let i = tempArr . length - 1 ; i >= 0 ; i -- ) {
17+ for ( let num of tempArr [ i ] ) {
18+ answer . push ( num ) ;
19+ if ( answer . length === k ) return answer ;
20+ }
21+ }
22+ return answer ;
23+ } ;
Original file line number Diff line number Diff line change 1+ const twoSum = function ( nums , target ) {
2+ let memo = { } ;
3+
4+ for ( let i = 0 ; i < nums . length ; i ++ ) {
5+ let current = nums [ i ] ;
6+ let needed = target - current ;
7+
8+ if ( needed in memo ) {
9+ return [ memo [ needed ] , i ] ;
10+ }
11+
12+ memo [ current ] = i ;
13+ }
14+ } ;
You canโt perform that action at this time.
0 commit comments