File tree Expand file tree Collapse file tree 5 files changed +103
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +103
-0
lines changed Original file line number Diff line number Diff line change 1+ // ๋ฌธ์ ์๋๋ฅผ ์ ๋ชจ๋ฅด๊ฒ ์. ๊น์ ๋ณต์ฌ๊ฐ ๋ Node๋ฅผ ๋ง๋ค๋ฉด ๋๋ค.
2+ // DFS๋ก ํ๋ฉด ๋๊ณ , BFS๋ ๊ฐ๋ฅํ๊ฒ ์ง๋ง BFS๋ ๊น๋ฑ ์๋ชปํ๋ฉด ํ์์์์ด ๋์ ๊ทธ๋ฅ DFS๋ก ํด๊ฒฐ
3+ class Solution {
4+ private Map <Node , Node > visited = new HashMap <>();
5+
6+ public Node cloneGraph (Node node ) {
7+ if (node == null ) return null ;
8+
9+ if (visited .containsKey (node )) {
10+ return visited .get (node );
11+ }
12+
13+ Node cloneNode = new Node (node .val );
14+ visited .put (node , cloneNode );
15+
16+ for (Node neighbor : node .neighbors ) {
17+ cloneNode .neighbors .add (cloneGraph (neighbor ));
18+ }
19+
20+ return cloneNode ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ // ๊ณตํต ๋ถ๋ถ ์์ด ๋ฌธ์ ๋ ๋๊ฒ ์ ๋ช
ํ ๋ฌธ์ ์
2+ // ์ผ๋ฐ์ ์ธ DP๋ O(N)์ธ๋ฐ, ์ด๊ฑด O(N^2)์ด๋ผ ์ด๋ ค์
3+ // GPT์ ๋์์ ๋ฐ์์. ๋ฐ๋ก ์ ๋ฆฌ๊ฐ ํ์ํ ๋ฏ
4+ class Solution {
5+ public int longestCommonSubsequence (String text1 , String text2 ) {
6+ int m = text1 .length (), n = text2 .length ();
7+ int [][] dp = new int [m + 1 ][n + 1 ];
8+
9+ for (int i = 1 ; i <= m ; i ++) {
10+ for (int j = 1 ; j <= n ; j ++) {
11+ if (text1 .charAt (i - 1 ) == text2 .charAt (j - 1 )) {
12+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
13+ } else {
14+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
15+ }
16+ }
17+ }
18+ return dp [m ][n ];
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ // ์ต๊ทผ ์์ฃผ ๋ณด์๋ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ๋ฅผ ์ด์ฉํ ๋ฐฉ๋ฒ
2+ // ์๊ฐ ๋ณต์ก๋ : O(N)
3+ class Solution {
4+ public int characterReplacement (String s , int k ) {
5+ int left = 0 , maxLen = 0 ;
6+ int [] count = new int [26 ];
7+ int maxCount = 0 ;
8+
9+ for (int right = 0 ; right < s .length (); right ++) {
10+ count [s .charAt (right ) - 'A' ]++; // ์ค๋ฅธ์ชฝ ๋ฌธ์ ์ถ๊ฐ
11+ maxCount = Math .max (maxCount , count [s .charAt (right ) - 'A' ]); // ์ต๋น ๋ฌธ์ ์
๋ฐ์ดํธ
12+
13+ // ํ์ฌ ์๋์ฐ์ ํฌ๊ธฐ - ์ต๋น ๋ฌธ์ ๊ฐ์ > k๋ผ๋ฉด ์๋์ฐ๋ฅผ ์ถ์
14+ while ((right - left + 1 ) - maxCount > k ) {
15+ count [s .charAt (left ) - 'A' ]--; // ์ผ์ชฝ ๋ฌธ์ ์ ๊ฑฐ
16+ left ++;
17+ }
18+
19+ maxLen = Math .max (maxLen , right - left + 1 ); // ์ต๋ ๊ธธ์ด ์
๋ฐ์ดํธ
20+ }
21+
22+ return maxLen ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ // ๊ฐ์ฅ ๋จผ์ ๋ ์ค๋ฅธ ๋ฐฉ๋ฒ์ด์ง๋ง ๊ถ์ฅ๋์ง ์๋ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์
2+ // ๋ณํ๋ ์ด์ง ๋ฌธ์์ด์ ์นํํ๋ ๋ฐฉ์์ O(K + logN)์ด๋ค.
3+ class Solution {
4+ public int hammingWeight (int n ) {
5+ return Integer .toBinaryString (n ).replace ("0" , "" ).length ();
6+ }
7+ }
8+
9+ // ๊ทธ๋ฅ ๋นํธ ์ฐ์ฐ์๋ก ํธ๋๊ฒ ์ต๊ณ ์ธ๊ฒ ๊ฐ๋ค.
10+ class Solution {
11+ public int hammingWeight (int n ) {
12+ int count = 0 ;
13+ while (n != 0 ) {
14+ count += (n & 1 ); // ๋ง์ง๋ง ์ซ์๊ฐ 1์ธ์ง ํ์ธ
15+ n >>>= 1 ; // n์ 1์นธ ์ค๋ฅธ์ชฝ ์ด๋
16+ }
17+ return count ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ // ๋นํธ ์ฐ์ฐ์๋ฅผ ์ด์ฉํ ๋ฌธ์ ๊ฐ ์ ํ๊ธฐ ์ด๋ ต๊ธดํ๋ฐ ์์ฃผ ๋์ค๋ ๊ฒ ๊ฐ๋ค
2+ class Solution {
3+ public int getSum (int a , int b ) {
4+ while (b != 0 ) {
5+ int carry = (a & b ) << 1 ; // AND ์ฐ์ฐ ํ ์ผ์ชฝ ์ฌํํธํ์ฌ ์๋ฆฌ ์ฌ๋ฆผ ๊ณ์ฐ
6+ a = a ^ b ; // XOR ์ฐ์ฐ์ผ๋ก ์๋ฆฌ ์ฌ๋ฆผ ์๋ ๋ง์
์ํ
7+ b = carry ; // ์๋ฆฌ ์ฌ๋ฆผ์ ๋ค์ ์ฐ์ฐ์ ์ฌ์ฉ
8+ }
9+ return a ;
10+ }
11+ }
12+
13+ // ์ด๋ฐ์์ผ๋ก๋ ํ๋ฆฌ์ง๋ง ์ด๋ฐ ๊ฒฐ๊ณผ๋ ์ํ์ง ์์์ง ๋..
14+ class Solution {
15+ public int getSum (int a , int b ) {
16+ return Math .addExact (a , b );
17+ }
18+ }
You canโt perform that action at this time.
0 commit comments