File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ /**
3+ * Determines if the array contains any duplicate values.
4+ * Uses a Set to track seen numbers for O(n) time complexity.
5+ *
6+ * @param nums - An array of integers.
7+ * @returns `true` if there are duplicates, `false` otherwise.
8+ *
9+ * Time Complexity: O(n)
10+ * Space Complexity: O(n)
11+ */
12+ function containsDuplicate ( nums : number [ ] ) : boolean {
13+ let numSet = new Set ( nums ) ;
14+ return numSet . size != nums . length ;
15+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Finds the maximum amount of money that can be robbed without robbing two adjacent houses.
3+ * Uses dynamic programming to track the best outcome at each step.
4+ *
5+ * @param {number[] } nums
6+ * @return {number }
7+ */
8+ function rob ( nums : number [ ] ) : number {
9+ const dp = new Array ( nums . length + 1 ) ;
10+ dp [ 0 ] = 0 ;
11+ dp [ 1 ] = nums [ 0 ] ;
12+ for ( let i = 2 ; i < dp . length ; i ++ ) {
13+ dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + nums [ i - 1 ] ) ;
14+ }
15+ return dp [ dp . length - 1 ] ;
16+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Finds the length of the longest consecutive elements sequence.
3+ * Eliminates duplicates using a Set and only starts counting when the current number is the beginning of a sequence.
4+ *
5+ * @param nums - An array of integers.
6+ * @returns The length of the longest consecutive sequence.
7+ *
8+ * Time Complexity: O(n)
9+ * Space Complexity: O(n)
10+ */
11+ function longestConsecutive ( nums : number [ ] ) : number {
12+ let longest = 0 ;
13+ const numSet = new Set ( nums ) ;
14+
15+ for ( const num of numSet ) {
16+ if ( ! numSet . has ( num - 1 ) ) {
17+ let length = 1 ;
18+ while ( numSet . has ( num + length ) ) {
19+ length ++ ;
20+ }
21+ longest = Math . max ( length , longest ) ;
22+ }
23+ }
24+
25+ return longest ;
26+ }
Original file line number Diff line number Diff line change 1+
2+ /**
3+ * Finds the k most frequent elements in an array.
4+ * Uses a map to count occurrences and then sorts by frequency.
5+ *
6+ * @param nums - An array of integers.
7+ * @param k - The number of most frequent elements to return.
8+ * @returns An array of the k most frequent elements.
9+ *
10+ * Time Complexity: O(n log n)
11+ * Space Complexity: O(n)
12+ */
13+ function topKFrequent ( nums : number [ ] , k : number ) : number [ ] {
14+ let numMap = new Map ( ) ;
15+ for ( let i = 0 ; i < nums . length ; i ++ ) {
16+ numMap . set ( nums [ i ] , ( numMap . get ( nums [ i ] ) ?? 0 ) + 1 ) ;
17+ }
18+
19+ return Array . from ( numMap . entries ( ) )
20+ . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] )
21+ . slice ( 0 , k )
22+ . map ( ( [ num , _ ] ) => num ) ;
23+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * Finds two numbers in the array that add up to the target value.
3+ * Uses a hash map to store previously seen numbers for O(n) time complexity.
4+ *
5+ * @param nums - An array of integers.
6+ * @param target - The target sum.
7+ * @returns A tuple containing the indices of the two numbers.
8+ *
9+ * Time Complexity: O(n)
10+ * Space Complexity: O(n)
11+ */
12+ function twoSum ( nums : number [ ] , target : number ) : number [ ] {
13+ const map = new Map < number , number > ( ) ;
14+
15+ for ( let i = 0 ; i < nums . length ; i ++ ) {
16+ const complement = target - nums [ i ] ;
17+ if ( map . has ( complement ) ) {
18+ return [ map . get ( complement ) ! , i ] ;
19+ }
20+ map . set ( nums [ i ] , i ) ;
21+ }
22+
23+ throw new Error ( "No solution found" ) ;
24+ }
You can’t perform that action at this time.
0 commit comments