File tree Expand file tree Collapse file tree 6 files changed +174
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 6 files changed +174
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/clone-graph/
3
+ * // Definition for a _Node.
4
+ * function _Node(val, neighbors) {
5
+ * this.val = val === undefined ? 0 : val;
6
+ * this.neighbors = neighbors === undefined ? [] : neighbors;
7
+ * };
8
+ * ์๊ฐ ๋ณต์ก๋: O(N) โ ๋
ธ๋ ์๋งํผ ์ํ
9
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(N) โ visited ๋งต๊ณผ ์ฌ๊ท ํธ์ถ ์คํ
10
+ */
11
+
12
+ /**
13
+ * @param {_Node } node
14
+ * @return {_Node }
15
+ */
16
+ var cloneGraph = function ( node ) {
17
+ if ( ! node ) return null ;
18
+
19
+ const visited = new Map ( ) ;
20
+
21
+ const dfs = ( n ) => {
22
+ if ( visited . has ( n ) ) {
23
+ return visited . get ( n ) ;
24
+ }
25
+
26
+ const clone = new Node ( n . val ) ;
27
+ visited . set ( n , clone ) ;
28
+
29
+ for ( let neighbor of n . neighbors ) {
30
+ clone . neighbors . push ( dfs ( neighbor ) ) ;
31
+ }
32
+
33
+ return clone ;
34
+ } ;
35
+
36
+ return dfs ( node ) ;
37
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+
9
+ /**
10
+ * @param {ListNode } head
11
+ * @return {boolean }
12
+ */
13
+ var hasCycle = function ( head ) {
14
+ // ๋ฆฌ์คํธ๊ฐ ๋น์ด ์๊ฑฐ๋ ๋
ธ๋๊ฐ ํ๋๋ฟ์ด๋ฉด ์ฌ์ดํด์ด ์์ ์ ์์
15
+ if ( ! head || ! head . next ) return false ;
16
+
17
+ // ๋ ํฌ์ธํฐ ์ด๊ธฐํ: slow๋ ํ ์นธ์ฉ, fast๋ ๋ ์นธ์ฉ ์ด๋
18
+ let slow = head ;
19
+ let fast = head . next ;
20
+
21
+ // fast์ slow๊ฐ ๋ง๋ ๋๊น์ง ๋ฐ๋ณต
22
+ while ( fast !== slow ) {
23
+ // fast๊ฐ ๋์ ๋๋ฌํ๋ฉด ์ฌ์ดํด์ด ์์
24
+ if ( ! fast || ! fast . next ) return false ;
25
+
26
+ // slow๋ ํ ์นธ ์ด๋
27
+ slow = slow . next ;
28
+ // fast๋ ๋ ์นธ ์ด๋
29
+ fast = fast . next . next ;
30
+ }
31
+
32
+ // fast์ slow๊ฐ ๋ง๋ฌ๋ค๋ฉด ์ฌ์ดํด์ด ์์
33
+ return true ;
34
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/longest-common-subsequence/submissions/1644426037/
3
+ * @param {string } text1
4
+ * @param {string } text2
5
+ * @return {number }
6
+ */
7
+ var longestCommonSubsequence = function ( text1 , text2 ) {
8
+ const m = text1 . length ;
9
+ const n = text2 . length ;
10
+
11
+ // Create 2D array initialized with 0
12
+ const dp = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
13
+
14
+ // Fill the dp table
15
+ for ( let i = 1 ; i <= m ; i ++ ) {
16
+ for ( let j = 1 ; j <= n ; j ++ ) {
17
+ if ( text1 [ i - 1 ] === text2 [ j - 1 ] ) {
18
+ // Characters match
19
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ;
20
+ } else {
21
+ // No match, take the max from left or top cell
22
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
23
+ }
24
+ }
25
+ }
26
+
27
+ // The length of the longest common subsequence
28
+ return dp [ m ] [ n ] ;
29
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var maxProduct = function ( nums ) {
6
+ // ์ต๋ ๊ณฑ์ ์ ์ฅํ ๋ณ์
7
+ let maxProduct = nums [ 0 ] ;
8
+ // ํ์ฌ ์์น๊น์ง์ ์ต๋ ๊ณฑ๊ณผ ์ต์ ๊ณฑ์ ์ ์ฅํ ๋ณ์
9
+ let currentMax = nums [ 0 ] ;
10
+ let currentMin = nums [ 0 ] ;
11
+
12
+ // ๋ฐฐ์ด์ ๋ ๋ฒ์งธ ์์๋ถํฐ ์ํ
13
+ for ( let i = 1 ; i < nums . length ; i ++ ) {
14
+ const num = nums [ i ] ;
15
+
16
+ // ์์๋ฅผ ๊ณฑํ ๊ฒฝ์ฐ ์ต๋์ ์ต์๊ฐ ๋ฐ๋ ์ ์์ผ๋ฏ๋ก ๋ฏธ๋ฆฌ ์ ์ฅ
17
+ const tempMax = currentMax ;
18
+
19
+ // ํ์ฌ ์ซ์์ ๊ณฑํ์ ๋์ ์ต๋/์ต์ ๊ฐ์ ๊ณ์ฐ
20
+ currentMax = Math . max ( num , num * currentMax , num * currentMin ) ;
21
+ currentMin = Math . min ( num , num * tempMax , num * currentMin ) ;
22
+
23
+ // ์ ์ฒด ์ต๋ ๊ณฑ์ ์
๋ฐ์ดํธ
24
+ maxProduct = Math . max ( maxProduct , currentMax ) ;
25
+ }
26
+
27
+ return maxProduct ;
28
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/palindromic-substrings/submissions/1644425061/
3
+ * @param {string } s
4
+ * @return {number }
5
+ */
6
+ var countSubstrings = function ( s ) {
7
+ let count = 0 ;
8
+
9
+ // Helper function to expand around the center
10
+ function expandAroundCenter ( left , right ) {
11
+ while ( left >= 0 && right < s . length && s [ left ] === s [ right ] ) {
12
+ count ++ ; // Found a palindrome
13
+ left -- ;
14
+ right ++ ;
15
+ }
16
+ }
17
+
18
+ for ( let i = 0 ; i < s . length ; i ++ ) {
19
+ expandAroundCenter ( i , i ) ; // Odd-length palindromes
20
+ expandAroundCenter ( i , i + 1 ) ; // Even-length palindromes
21
+ }
22
+
23
+ return count ;
24
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/sum-of-two-integers/submissions/1649575939/
3
+ * @param {number } a
4
+ * @param {number } b
5
+ * @return {number }
6
+ */
7
+ var getSum = function ( a , b ) {
8
+ while ( b !== 0 ) {
9
+ // a์ b์ ํฉ์์ ์๋ฆฌ์ฌ๋ฆผ(carry)์ ์ ์ธํ ๊ฐ ๊ณ์ฐ (XOR ์ฐ์ฐ)
10
+ let sum = a ^ b ;
11
+
12
+ // ์๋ฆฌ์ฌ๋ฆผ(carry)์ ๊ณ์ฐ (AND ์ฐ์ฐ ํ ์ผ์ชฝ์ผ๋ก ํ ๋นํธ ์ด๋)
13
+ let carry = ( a & b ) << 1 ;
14
+
15
+ // ์๋ก์ด a๋ sum, ์๋ก์ด b๋ carry๊ฐ ๋จ
16
+ a = sum ;
17
+ b = carry ;
18
+ }
19
+
20
+ // carry๊ฐ 0์ด ๋๋ฉด ๋ํ ๊ฒ ์์ผ๋ฏ๋ก ์ต์ข
๊ฒฐ๊ณผ a ๋ฐํ
21
+ return a ;
22
+ } ;
You canโt perform that action at this time.
0 commit comments