File tree Expand file tree Collapse file tree 5 files changed +224
-0
lines changed
design-add-and-search-words-data-structure
pacific-atlantic-water-flow Expand file tree Collapse file tree 5 files changed +224
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * // Definition for a _Node.
3+ * function _Node(val, neighbors) {
4+ * this.val = val === undefined ? 0 : val;
5+ * this.neighbors = neighbors === undefined ? [] : neighbors;
6+ * };
7+ */
8+
9+ /**
10+ * @param {_Node } node
11+ * @return {_Node }
12+ */
13+ var cloneGraph = function ( node ) {
14+ let visited = { } ;
15+
16+ const dfs = ( node ) => {
17+ if ( ! node ) return node ;
18+ if ( visited [ node . val ] ) return visited [ node . val ] ;
19+
20+ let root = new Node ( node . val ) ;
21+ visited [ node . val ] = root ;
22+
23+ for ( let neighbor of node . neighbors ) {
24+ root . neighbors . push ( dfs ( neighbor ) ) ;
25+ }
26+ return root ;
27+ } ;
28+
29+ return dfs ( node ) ;
30+ } ;
31+
32+ // TC: O(n+e) -> n: number of nodes | e: number of edges
33+ // SC: O(v) -> v: length of visited object
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } numCourses
3+ * @param {number[][] } prerequisites
4+ * @return {boolean }
5+ */
6+ var canFinish = function ( numCourses , prerequisites ) {
7+ // Initialize in-degree array and graph map
8+ const inDegree = Array ( numCourses ) . fill ( 0 ) ;
9+ const graph = new Map ( ) ;
10+
11+ // Build the graph and compute in-degrees
12+ prerequisites . forEach ( ( [ course , pre ] ) => {
13+ inDegree [ course ] ++ ;
14+ if ( ! graph . has ( pre ) ) {
15+ graph . set ( pre , [ ] ) ;
16+ }
17+ graph . get ( pre ) . push ( course ) ;
18+ } ) ;
19+
20+ // Queue for courses with no prerequisites
21+ const queue = [ ] ;
22+ for ( let i = 0 ; i < numCourses ; i ++ ) {
23+ if ( inDegree [ i ] === 0 ) {
24+ queue . push ( i ) ;
25+ }
26+ }
27+
28+ // Process the courses
29+ let count = 0 ;
30+ while ( queue . length > 0 ) {
31+ const course = queue . shift ( ) ;
32+ count ++ ;
33+ if ( graph . has ( course ) ) {
34+ graph . get ( course ) . forEach ( ( nextCourse ) => {
35+ inDegree [ nextCourse ] -- ;
36+ if ( inDegree [ nextCourse ] === 0 ) {
37+ queue . push ( nextCourse ) ;
38+ }
39+ } ) ;
40+ }
41+ }
42+
43+ // Return true if all courses can be finished
44+ return count === numCourses ;
45+ } ;
46+
47+ // TC: O(V + E)
48+ // V is the number of courses, E is the number of prerequisites.
49+ // SC: O(V + E)
Original file line number Diff line number Diff line change 1+ var WordDictionary = function ( ) {
2+ this . dictionary = new Set ( ) ;
3+ } ;
4+
5+ /**
6+ * @param {string } word
7+ * @return {void }
8+ */
9+ WordDictionary . prototype . addWord = function ( word ) {
10+ this . dictionary . add ( word ) ;
11+ } ;
12+
13+ /**
14+ * @param {string } word
15+ * @return {boolean }
16+ */
17+ WordDictionary . prototype . search = function ( word ) {
18+ if ( word . indexOf ( "." ) != - 1 ) {
19+ // Case of word has a '.'
20+ for ( let str of this . dictionary ) {
21+ if ( str . length != word . length ) continue ;
22+ let i ;
23+ for ( i = 0 ; i < word . length ; i ++ ) {
24+ if ( word [ i ] === "." ) continue ;
25+ if ( word [ i ] != str [ i ] ) break ;
26+ }
27+ if ( i === str . length ) return true ;
28+ }
29+ return false ;
30+ } else {
31+ return this . dictionary . has ( word ) ;
32+ }
33+ } ;
34+
35+ // n: number of words | m: length of the word
36+ // TC: O(n*m)
37+ // SC: O(n*m)
Original file line number Diff line number Diff line change 1+ var numIslands = function ( grid ) {
2+ // Declare row and column length
3+ const m = grid . length ,
4+ n = grid [ 0 ] . length ;
5+ let numIslands = 0 ;
6+
7+ // Available directions for depth-first search
8+ const dir = [
9+ [ 0 , 1 ] ,
10+ [ 1 , 0 ] ,
11+ [ 0 , - 1 ] ,
12+ [ - 1 , 0 ] ,
13+ ] ;
14+
15+ // Function to depth-first search inside of grid
16+ const dfs = ( i , j ) => {
17+ grid [ i ] [ j ] = "2" ;
18+
19+ let x , y ;
20+ for ( d of dir ) {
21+ x = i + d [ 0 ] ;
22+ y = j + d [ 1 ] ;
23+ if ( x >= 0 && x < m && y >= 0 && y < n && grid [ x ] [ y ] === "1" ) {
24+ dfs ( x , y ) ;
25+ }
26+ }
27+ return ;
28+ } ;
29+
30+ for ( let i = 0 ; i < m ; i ++ ) {
31+ for ( let j = 0 ; j < n ; j ++ ) {
32+ if ( grid [ i ] [ j ] === "1" ) {
33+ dfs ( i , j ) ;
34+ numIslands ++ ;
35+ }
36+ }
37+ }
38+ return numIslands ;
39+ } ;
40+
41+ // TC: O(m*n)
42+ // SC: O(m*n)
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[][] } heights
3+ * @return {number[][] }
4+ */
5+ var pacificAtlantic = function ( heights ) {
6+ const m = heights . length ,
7+ n = heights [ 0 ] . length ;
8+ let result = [ ] ;
9+
10+ const pacific = new Array ( m ) . fill ( null ) . map ( ( ) => new Array ( n ) . fill ( false ) ) ;
11+ const atlantic = new Array ( m ) . fill ( null ) . map ( ( ) => new Array ( n ) . fill ( false ) ) ;
12+
13+ const dir = [
14+ [ 0 , 1 ] ,
15+ [ 1 , 0 ] ,
16+ [ 0 , - 1 ] ,
17+ [ - 1 , 0 ] ,
18+ ] ;
19+
20+ const dfs = ( i , j , ocean ) => {
21+ // Check visited cell
22+ ocean [ i ] [ j ] = true ;
23+
24+ for ( d of dir ) {
25+ let x = i + d [ 0 ] ,
26+ y = j + d [ 1 ] ;
27+ if (
28+ x >= 0 &&
29+ x < m &&
30+ y >= 0 &&
31+ y < n &&
32+ ! ocean [ x ] [ y ] &&
33+ heights [ x ] [ y ] >= heights [ i ] [ j ]
34+ ) {
35+ dfs ( x , y , ocean ) ;
36+ }
37+ }
38+ } ;
39+
40+ // Check the cells can flow left and right edge
41+ for ( let i = 0 ; i < m ; i ++ ) {
42+ dfs ( i , 0 , pacific ) ;
43+ dfs ( i , n - 1 , atlantic ) ;
44+ }
45+
46+ // Check the cells can flow top and bottom edge
47+ for ( let j = 0 ; j < n ; j ++ ) {
48+ dfs ( 0 , j , pacific ) ;
49+ dfs ( m - 1 , j , atlantic ) ;
50+ }
51+
52+ for ( let i = 0 ; i < m ; i ++ ) {
53+ for ( let j = 0 ; j < n ; j ++ ) {
54+ if ( pacific [ i ] [ j ] && atlantic [ i ] [ j ] ) {
55+ result . push ( [ i , j ] ) ;
56+ }
57+ }
58+ }
59+ return result ;
60+ } ;
61+
62+ // TC: O(m*n)
63+ // SC: O(m*n)
You can’t perform that action at this time.
0 commit comments