Skip to content

[Tessa1217] Week 08 Solutions #1488

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions clone-graph/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
// 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;
}
}
*/

import java.util.HashMap;
import java.util.Map;

/**
* 참조 노드는 무방향 그래프에 연결되어있다. 그래프의 deep copy(clone)을 반환하세요.
*/
class Solution {

// 방문한 노드를 기억할 Map 선언
Map<Node, Node> visited = new HashMap<>();

public Node cloneGraph(Node node) {
return clone(node);
}

public Node clone(Node node) {
if (node == null) {
return null;
}

// 이미 방문했으면 Map에서 꺼내서 반환
if (visited.containsKey(node)) {
return visited.get(node);
}

// 신규 Node 생성
Node newNode = new Node(node.val);
visited.put(node, newNode);

// 인접 노드 Clone
if (node.neighbors != null && !node.neighbors.isEmpty()) {
for (Node neighbor : node.neighbors) {
newNode.neighbors.add(clone(neighbor));
}
}
return newNode;
}
}

27 changes: 27 additions & 0 deletions longest-common-subsequence/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 두 문자열 text1과 text2가 주어질 때 가장 긴 공통 부분 수열의 길이를 리턴하고 없으면 0을 리턴하세요.
*/
class Solution {

// 시간복잡도: O(t1Length * t2Length)
public int longestCommonSubsequence(String text1, String text2) {

int t1Length = text1.length();
int t2Length = text2.length();

int[][]dp = new int[t1Length + 1][t2Length + 1];

for (int i = 1; i <= t1Length; i++) {
for (int j = 1; j <= t2Length; 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[t1Length][t2Length];

}
}

35 changes: 35 additions & 0 deletions longest-repeating-character-replacement/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 문자열 s와 정수 k가 주어진다.
* 문자열 내 아무 문자나 대문자 영문자로 변경할 수 있으며 해당 연산을 k번까지 할 수 있을 때,
* 같은 문자를 포함하는 가장 긴 부분 수열을 반환하세요.
*/
class Solution {

// 시간복잡도: O(n)
public int characterReplacement(String s, int k) {

int[] chars = new int[26];
int left = 0;
int maxLength = 0;
int maxCnt = 0;

for (int right = 0; right < s.length(); right++) {
int charIdx = s.charAt(right) - 'A';
chars[charIdx]++;
// 현재 윈도우 내에서 가장 많이 등장하는 문자의 개수
maxCnt = Math.max(chars[charIdx], maxCnt);

// window size - max cnt > k (window 사이즈 조정)
while (right - left + 1 - maxCnt > k) {
chars[s.charAt(left) - 'A']--;
left++;
}

maxLength = Math.max(maxLength, right - left + 1);
}

return maxLength;

}
}

29 changes: 29 additions & 0 deletions palindromic-substrings/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* palindrome 부분 수열의 개수를 찾으세요.
*/
class Solution {

/** 시간복잡도: O(n^2), O(1) */
public int countSubstrings(String s) {

int count = 0;

for (int i = 0; i < s.length(); i++) {
count += checkPalindrome(s, i, i); // 홀수
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(left) == s.charAt(right)) {
count++;
left--;
right++;
}
return count;
}
}

33 changes: 33 additions & 0 deletions reverse-bits/Tessa1217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {

// 비트 연산자 사용
public int reverseBits(int n) {
int answer = 0;
for (int i = 0; i < 32; i++) {
answer <<= 1;
answer |= (n & 1);
n >>>= 1;
}

return answer;
}

// you need treat n as an unsigned value
// O(1)
// public int reverseBits(int n) {

// int answer = 0;

// String binary = Integer.toBinaryString(n);

// StringBuilder padded = new StringBuilder();
// for (int i = 0; i < 32 - binary.length(); i++) {
// padded.append('0');
// }
// padded.append(binary);

// return (int) Long.parseLong(padded.reverse().toString(), 2);

// }
}