File tree Expand file tree Collapse file tree 5 files changed +136
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +136
-0
lines changed Original file line number Diff line number Diff line change 1+ // 풀이
2+ // Set으로 중복 제거 후 nums와 길이 비교
3+
4+ // TC : O(N)
5+ // SC : O(N)
6+
7+ var containsDuplicate = function ( nums ) {
8+ return new Set ( nums ) . size !== nums . length
9+ } ;
10+
Original file line number Diff line number Diff line change 1+ // 풀이
2+ // dp를 이용한 풀이
3+ // 점화식 : dp[index] = Math.max(dp[index-1], dp[index-2] + nums[index])
4+ // 사용되는 변수가 index-1, index-2 뿐이라 불필요한 배열을 제거하고자 함.
5+
6+ // TC : O(N)
7+ // SC : O(1)
8+
9+ var rob = function ( nums ) {
10+ let dp = new Array ( 2 ) ;
11+
12+ dp [ 0 ] = nums [ 0 ]
13+ // nums의 길이가 1인 경우 예외처리
14+ dp [ 1 ] = nums . length > 1 ? Math . max ( nums [ 0 ] , nums [ 1 ] ) : nums [ 0 ]
15+
16+ nums . forEach ( ( num , index ) => {
17+ if ( index <= 1 ) return ;
18+
19+ let temp = Math . max ( dp [ 1 ] , dp [ 0 ] + nums [ index ] )
20+ dp [ 0 ] = dp [ 1 ]
21+ dp [ 1 ] = temp
22+ } )
23+
24+ return dp [ 1 ]
25+ } ;
26+
Original file line number Diff line number Diff line change 1+ // 정렬을 이용한 풀이
2+ // TC : O(NlogN)
3+ // SC : O(N)
4+
5+ var longestConsecutiveUsingSort = function ( nums ) {
6+ if ( nums . length === 0 ) return 0 ;
7+
8+ const uniqueNums = [ ...new Set ( nums ) ] . sort ( ( a , b ) => a - b ) ;
9+
10+ let max_cnt = 0 ;
11+ let cnt = 1 ;
12+ for ( let i = 1 ; i < uniqueNums . length ; ++ i ) {
13+ if ( uniqueNums [ i - 1 ] + 1 == uniqueNums [ i ] ) {
14+ cnt += 1 ;
15+ } else {
16+ max_cnt = cnt > max_cnt ? cnt : max_cnt ;
17+ cnt = 1 ;
18+ }
19+ }
20+ max_cnt = cnt > max_cnt ? cnt : max_cnt ;
21+ return max_cnt ;
22+ } ;
23+
24+ // 집합을 이용한 풀이
25+ // TC : O(N)
26+ // SC : O(N)
27+
28+ var longestConsecutive = function ( nums ) {
29+ if ( nums . length === 0 ) return 0 ;
30+
31+ const numSet = new Set ( nums ) ;
32+ let maxLength = 0 ;
33+
34+ for ( const num of numSet ) {
35+ if ( ! numSet . has ( num - 1 ) ) {
36+ let currentNum = num ;
37+ let currentLength = 1 ;
38+
39+ while ( numSet . has ( currentNum + 1 ) ) {
40+ currentNum ++ ;
41+ currentLength ++ ;
42+ }
43+
44+ maxLength = Math . max ( maxLength , currentLength ) ;
45+ }
46+ }
47+
48+ return maxLength ;
49+ } ;
50+
Original file line number Diff line number Diff line change 1+ // 풀이
2+ // 1. 각 요소의 빈도를 계산하여 countObject 생성
3+ // 2. countObject를 정렬하여 상위 k개의 요소 추출
4+ // 3. 추출된 요소를 배열로 반환
5+
6+ // TC : O(NlogN)
7+ // SC : O(N)
8+
9+ var topKFrequent = function ( nums , k ) {
10+ const countObject = { }
11+ nums . forEach ( ( num ) => {
12+ if ( countObject [ num ] ) {
13+ countObject [ num ] += 1
14+ } else {
15+ countObject [ num ] = 1
16+ }
17+ } )
18+
19+ const sortedObject = Object . entries ( countObject )
20+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
21+ . slice ( 0 , k )
22+ . map ( item => Number ( item [ 0 ] ) )
23+
24+ return sortedObject
25+ } ;
26+
Original file line number Diff line number Diff line change 1+ // 풀이
2+ // 1. 영어, 숫자만 남기고 소문자로 변환한 newStr 생성
3+ // 2. 투포인터를 이용해 문자열 팰린드롬 여부 확인 (초기값 : true)
4+ // 3. 중간에 팰린드롬이 아닌 지점을 발견하면 flag를 false로 변경 후 return
5+
6+ // TC : O(N)
7+ // SC : O(N)
8+
9+ var isPalindrome = function ( s ) {
10+ const newStr = s . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) . toLowerCase ( ) ;
11+ let flag = true
12+ let left = 0 , right = newStr . length - 1
13+ while ( left < right ) {
14+ if ( newStr [ left ] === newStr [ right ] ) {
15+ left += 1 ;
16+ right -= 1 ;
17+ } else {
18+ flag = false ;
19+ break ;
20+ }
21+ }
22+ return flag
23+ } ;
24+
You can’t perform that action at this time.
0 commit comments