-
-
Notifications
You must be signed in to change notification settings - Fork 338
[Hyeri1ee] WEEK 08 solutions #2557
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
75fe368
345c2d0
bd4afe0
da77602
dd005fb
fbbbae1
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,48 @@ | ||
| import java.util.*; | ||
| /* | ||
| 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 { | ||
|
|
||
|
|
||
| //기존 node, 복사한 node | ||
| Map<Node, Node> visited = new HashMap<>(); | ||
| public Node cloneGraph(Node node) { | ||
| if (node==null) return null; | ||
|
|
||
| if (visited.containsKey(node)){ | ||
| return visited.get(node); | ||
| } | ||
|
|
||
| //없는 경우 | ||
| Node newClone= new Node(node.val); | ||
| visited.put(node, newClone); | ||
|
|
||
| //이웃 복사 | ||
| for(Node target : node.neighbors){ | ||
| newClone.neighbors.add(cloneGraph(target)); | ||
| } | ||
|
|
||
| return newClone; | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
|
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,33 @@ | ||
| import java.util.*; | ||
| class Solution { | ||
| public int longestCommonSubsequence(String text1, String text2) { | ||
|
|
||
|
|
||
| //int longerlen= (text1.length() >= text2.length()) ? text1.length() : text2.length(); | ||
| //int lesslen= (text1.length() <= text2.length()) ? text1.length() : text2.length(); | ||
| int[][] dp = new int[text1.length()+1][text2.length()+1]; | ||
|
|
||
| //String longertext= (text1.length() >= text2.length()) ? text1 : text2; | ||
| //String lesstext= (text1.length() <= text2.length()) ? text1 : text2; | ||
|
|
||
|
|
||
|
|
||
| for (int i = 1; i <=text1.length(); i++) { | ||
| for (int j = 1; j <=text2.length(); j++) { | ||
| //문자가 같으면 | ||
| if (text1.charAt(i-1) == text2.charAt(j-1)){ | ||
| dp[i][j] = dp[i-1][j-1]+1; | ||
| //System.out.println("dp["+i+"]["+j+"]: "+ dp[i][j]); | ||
| }else{ | ||
| //문자가 다르면 | ||
| dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); | ||
| //System.out.println("dp["+i+"]["+j+"]: "+ dp[i][j]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| return dp[text1.length()][text2.length()]; | ||
| }//end of method | ||
| } | ||
|
|
|
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,61 @@ | ||
| class Solution { | ||
| //left,right | ||
|
|
||
| //right를 left에서부터 이동하면서 max (대체해야되는 alphabet 수) 갱신 | ||
| //(right - left+ 1) - 다수등장alphabet 개수 = 대체해야되는 alphabet 수 | ||
| //대체해야되는 수 <= k 인 경우 완료 | ||
| //"" > k 인 경우 left++ | ||
|
|
||
| //빈도수 저장용 | ||
| int[] alpha = new int[26]; | ||
| int answer =0 ; | ||
|
|
||
| public int characterReplacement(String s, int k) { | ||
|
|
||
| //인덱스 정의 (초기값) | ||
| int left= 0; | ||
| int right= 0; | ||
|
|
||
|
|
||
|
|
||
| while(right < s.length()){ | ||
| //현재 갱신된 right까지 빈도수 저장 | ||
| //System.out.println("[right] : " + right); | ||
| //System.out.println("[left] : " + left); | ||
|
|
||
| alpha[s.charAt(right) - 'A']++; | ||
|
|
||
| int max = mostSeenFrequency(left, right, alpha); | ||
|
|
||
|
|
||
|
|
||
| while ((right-left + 1) - max > k) { | ||
| alpha[s.charAt(left) - 'A']--; | ||
| left++; | ||
| max = mostSeenFrequency(left, right, alpha); | ||
| } | ||
|
|
||
| if (answer < right-left + 1){ | ||
| answer=right-left+1; | ||
| } | ||
| right++; | ||
|
|
||
| //System.out.println("[answer] : " + answer); | ||
|
|
||
|
|
||
| }//end of while | ||
|
|
||
| return answer; | ||
|
|
||
| } | ||
|
|
||
| private static int mostSeenFrequency(int left, int right, int[] alpha){ | ||
| int max =0; | ||
| for(int i=0;i<26;i++){ | ||
| if (max < alpha[i]) max=alpha[i]; | ||
| } | ||
| return max; | ||
| } | ||
| } | ||
|
|
||
|
|
|
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,49 @@ | ||
| class Solution { | ||
| //길이 1~s.length()까지 palindrome 함수 시도 | ||
| int cnt=0; // | ||
|
|
||
|
|
||
| public int countSubstrings(String s) { | ||
|
|
||
| for(int len=1; len <= s.length();len++){//1~3 | ||
| for(int idx =0; idx <= s.length()-len ; idx++){ | ||
| //idx인덱스부터 len길이만큼의 문자열이 palindrome인지 판별 | ||
| boolean result = isPalindrome(s, idx, idx+len-1); //start: 시작 인덱스 , end: 끝 인덱스 | ||
|
|
||
| if (result){ | ||
| cnt++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return cnt; | ||
|
|
||
| } | ||
|
|
||
| private static boolean isPalindrome(String w, int start, int end){ | ||
| if (start == end) return true; | ||
| //start!= end | ||
| int s = start; | ||
| int e = end; | ||
| while(s<= e){ | ||
| if(e < start && s > end) return true; | ||
|
|
||
|
|
||
| //서로 값이 다른 경우 | ||
| if (w.charAt(s) != w.charAt(e)){ | ||
| return false; | ||
| } | ||
|
|
||
| //서로 값이 같은 경우 continue; | ||
| s++; | ||
| e--; | ||
| } | ||
|
|
||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
|
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,13 @@ | ||
| 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.
🏷️ 알고리즘 패턴 분석