-
-
Notifications
You must be signed in to change notification settings - Fork 335
[hyeri0903] WEEK 08 Solutions #2564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2517fa1
f40ebfb
8c9defe
2a3f7c8
5cf072b
ccd1c10
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| // Definition for a Node. | ||
| class Node { | ||
| public int val; | ||
| public List<Node> neighbors; | ||
| public Node() { | ||
| val = 0; | ||
| neighbors = new ArrayList<Node>(); | ||
| } | ||
| public Node(int _val) { | ||
| val = _val; | ||
| neighbors = new ArrayList<Node>(); | ||
| } | ||
| public Node(int _val, ArrayList<Node> _neighbors) { | ||
| val = _val; | ||
| neighbors = _neighbors; | ||
| } | ||
| } | ||
| */ | ||
|
|
||
| class Solution { | ||
| //key: original node, value: clone node | ||
| private Map<Node, Node> map = new HashMap<>(); | ||
|
|
||
| public Node cloneGraph(Node node) { | ||
| if(node == null) { | ||
| return null; | ||
| } | ||
| //이미 존재하면 return | ||
| if(map.containsKey(node)) { | ||
| return map.get(node); | ||
| } | ||
|
|
||
| //현재 노드 복제 | ||
| Node clone = new Node(node.val); | ||
| map.put(node, clone); | ||
|
|
||
| //이웃 노드들 복제해서 연결 | ||
| for(Node neighbor: node.neighbors) { | ||
| clone.neighbors.add(cloneGraph(neighbor)); | ||
| } | ||
| return clone; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| class Solution { | ||
| public int longestCommonSubsequence(String text1, String text2) { | ||
| /** | ||
| 1.문제: 가장 긴 common subsequence length return, 없으면 0 return | ||
| 2.constraints | ||
| - length min = 1, max = 1000 | ||
| - text1, text2 모두 lowercase로 구성 | ||
| 3.풀이 | ||
| - dp[i][j] = text1의 앞 i개, text2의 앞 j개를 비교했을 때 LCS 길이 | ||
| - 문자가 같으면: dp[i][j] = dp[i-1][j-1] + 1 | ||
| - 문자가 다르면: dp[i][j] = max(dp[i-1][j], dp[i][j-1]) | ||
| */ | ||
| int t1Size = text1.length(); //row | ||
| int t2Size = text2.length(); //col | ||
| int[][] dp = new int[t1Size + 1][t2Size + 1]; | ||
|
|
||
| for(int i = 1; i <= t1Size; i++) { | ||
| for(int j = 1; j <= t2Size; j++) { | ||
| if(text1.charAt(i-1) == text2.charAt(j-1)) { | ||
| dp[i][j] = dp[i-1][j-1] + 1; | ||
| } else { | ||
| dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); | ||
| } | ||
| } | ||
| } | ||
| return dp[t1Size][t2Size]; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| class Solution { | ||
| public int characterReplacement(String s, int k) { | ||
| /** | ||
| 1.problem: k번 특정 문자를 골라서 바꿀 수 있다. 이때 same letter 로 이루어진 가장 긴 substring 의 길이을 구하라. | ||
| 2.constraints | ||
| - s.length min = 1, max = 10^5 | ||
| - k.length min = 0, max = s.length | ||
| 3.solution | ||
| - sliding window: time O(n), space O(1) | ||
| */ | ||
|
|
||
| int[] table = new int[26]; | ||
| int n = s.length(); | ||
| int maxFreq = 0; | ||
| int maxLen = 0; | ||
| int left = 0; | ||
|
|
||
| for(int right = 0; right < n; right++) { | ||
| //1.right 확장 -> count 증가 | ||
| int currentChar = s.charAt(right) - 'A'; | ||
| table[currentChar]++; | ||
|
|
||
| //2.maxFreq update | ||
| maxFreq = Math.max(maxFreq, table[currentChar]); | ||
|
|
||
| //3.조건 깨지면 left 이동하면서 count-- | ||
| //바꿔야하는 문자 개수가 k 보다 큰 경우 | ||
| if((right - left + 1 - maxFreq) > k) { | ||
| int idx = s.charAt(left) - 'A'; | ||
| table[idx]--; | ||
| left++; | ||
| } | ||
|
|
||
| //4.maxLen update | ||
| maxLen = Math.max(maxLen, right - left + 1); | ||
| } | ||
| return maxLen; | ||
| } | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| class Solution { | ||
| public int countSubstrings(String s) { | ||
| /** | ||
| 1.문제: s 에서 palindromoic substring 개수 return | ||
| 2.constraints: | ||
| - 연속적인 문자 | ||
| - s.length min = 1, max = 1000 | ||
| 3.풀이 | ||
| - bruteforce: time:O(n^3), space:O(n) | ||
| - expand around : time(On^2), space:O(1) | ||
| */ | ||
|
|
||
| int n = s.length(); | ||
| if(n == 1) return 1; | ||
| int count = 0; | ||
|
|
||
| for(int i = 0; i < n; i++) { | ||
| //odd | ||
| count += checkPalindrome(s, i, i); | ||
|
|
||
| //even | ||
| count += checkPalindrome(s, i, i+1); | ||
| } | ||
| return count; | ||
|
|
||
| } | ||
|
|
||
| private int checkPalindrome(String s, int left, int right) { | ||
| int count = 0; | ||
| while(left >= 0 && right < s.length() && s.charAt(right) == s.charAt(left)) { | ||
| count += 1; | ||
| left -= 1; | ||
| right += 1; | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Solution { | ||
| public int reverseBits(int n) { | ||
| int result = 0; | ||
| for(int i = 0; i < 32; i++) { | ||
| result <<= 1; //왼쪽 이동 | ||
| result |= (n & 1); //마지막 비트 붙이기 | ||
| n >>= 1; // 오른쪽 이동 | ||
| } | ||
| return result; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🏷️ 알고리즘 패턴 분석