File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * time complexity: O(n)
3+ * space complexity: O(n)
4+ */
5+ function containsDuplicate ( nums : number [ ] ) : boolean {
6+ const seen = new Set ( ) ;
7+ for ( let num of nums ) {
8+ if ( seen . has ( num ) ) {
9+ return true ;
10+ }
11+ seen . add ( num ) ;
12+ }
13+ return false ;
14+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * time complexity: O(n) - iterate over a loop
3+ * space complexity: O(n) - dp array
4+ *
5+ * comment: initial naive implementation: simple odd/even alternation, which may return result that is "accidentally correct."
6+ */
7+ function rob ( nums : number [ ] ) : number {
8+ // early return
9+ if ( nums . length === 0 ) return 0 ;
10+ if ( nums . length === 1 ) return nums [ 0 ] ;
11+
12+ const dp : number [ ] = new Array ( nums . length ) ;
13+ dp [ 0 ] = nums [ 0 ] ;
14+ dp [ 1 ] = Math . max ( nums [ 0 ] , nums [ 1 ] ) ;
15+
16+ for ( let i = 2 ; i < nums . length ; i ++ ) {
17+ // select either 1) current + best from 2 houses ago or 2) skip current, best from previous
18+ dp [ i ] = Math . max ( nums [ i ] + dp [ i - 2 ] , dp [ i - 1 ] ) ;
19+ }
20+
21+ return dp [ nums . length - 1 ] ; // max money from all houses
22+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * requirement: return result in linear time (O(n))
3+ */
4+ function longestConsecutive ( nums : number [ ] ) : number {
5+ if ( nums . length === 0 ) return 0 ;
6+
7+ const numSet = new Set ( nums ) ; // O(1) lookups
8+ let maxLength = 0 ;
9+
10+ for ( const num of numSet ) {
11+ // key: determine the "start" of a consecutive sequence.
12+ // approach: if num - 1 doesn't exist in the set → num is the start of a sequence.
13+ if ( ! numSet . has ( num - 1 ) ) {
14+ let currentNum = num ;
15+ let currentLength = 1 ;
16+
17+ // count consecutive numbers
18+ while ( numSet . has ( currentNum + 1 ) ) {
19+ currentNum ++ ;
20+ currentLength ++ ;
21+ }
22+
23+ maxLength = Math . max ( maxLength , currentLength ) ;
24+ }
25+ }
26+ return maxLength ;
27+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * time complexity: O(n + m log m),
3+ * n = length of input array
4+ * m = number of unique elements (m ≤ n)
5+ * space complexity: O(m)
6+ */
7+
8+ function topKFrequent ( nums : number [ ] , k : number ) : number [ ] {
9+ const freqMap = new Map < number , number > ( ) ;
10+
11+ // Count frequencies using a map: O(n)
12+ for ( const num of nums ) {
13+ freqMap . set ( num , ( freqMap . get ( num ) || 0 ) + 1 ) ;
14+ }
15+
16+ // Extract the top k elements
17+ return Array . from ( freqMap . entries ( ) ) // O(m)
18+ . sort ( ( [ , a ] , [ , b ] ) => b - a ) // O(m log m)
19+ . slice ( 0 , k )
20+ . map ( ( [ num ] ) => num ) ; // extract the keys only
21+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Time Complexity: O(n) (using a single pass with a hash map)
3+ * (If a nested loop was used, it would be O(n^2))
4+ */
5+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
6+ const numAndIndex = new Map < number , number > ( ) ;
7+
8+ for ( let i = 0 ; i < nums . length ; i ++ ) {
9+ const diff = target - nums [ i ] ;
10+
11+ if ( numAndIndex . has ( diff ) ) {
12+ return [ numAndIndex . get ( diff ) ! , i ] ;
13+ }
14+
15+ numAndIndex . set ( nums [ i ] , i ) ;
16+ }
17+
18+ return [ ] ;
19+ }
You can’t perform that action at this time.
0 commit comments