Skip to content

[bky373] 10th week solutions #166

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

Merged
merged 5 commits into from
Jul 9, 2024
Merged
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
42 changes: 42 additions & 0 deletions graph-valid-tree/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
time: O(n + m), where n is the number of nodes and m is the number of edges in the graph.
space: O(n + m)
*/
class Solution {

public boolean validTree(int n, int[][] edges) {

if (edges.length != n - 1) {
return false;
}

List<List<Integer>> adjList = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjList.add(new ArrayList<>());
}
for (int[] edge : edges) {
adjList.get(edge[0])
.add(edge[1]);
adjList.get(edge[1])
.add(edge[0]);
}

Stack<Integer> stack = new Stack<>();
Set<Integer> visited = new HashSet<>();
stack.push(0);
visited.add(0);

while (!stack.isEmpty()) {
int curr = stack.pop();
for (int adj : adjList.get(curr)) {
if (visited.contains(adj)) {
continue;
}
visited.add(adj);
stack.push(adj);
}
}

return visited.size() == n;
}
}
33 changes: 33 additions & 0 deletions house-robber-ii/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
time: O(N)
time: O(1)
*/
class Solution {

public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}

return Math.max(
dp(nums, 0, nums.length - 2),
dp(nums, 1, nums.length - 1)
);
}

private static int dp(int[] nums, int start, int end) {
int maxOfOneStepAhead = nums[end];
int maxOfTwoStepsAhead = 0;

for (int i = end - 1; i >= start; --i) {
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]);

maxOfTwoStepsAhead = maxOfOneStepAhead;
maxOfOneStepAhead = curr;
}
return maxOfOneStepAhead;
}
}
23 changes: 23 additions & 0 deletions house-robber/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
time: O(N)
space: O(1)
*/
class Solution {

public int rob(int[] nums) {
if (nums.length == 0) {
return 0;
}

int maxOfTwoStepsAhead = 0;
int maxOfOneStepAhead = nums[nums.length - 1];

for (int i = nums.length - 2; i >= 0; --i) {
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]);

maxOfTwoStepsAhead = maxOfOneStepAhead;
maxOfOneStepAhead = curr;
}
return maxOfOneStepAhead;
}
}
104 changes: 104 additions & 0 deletions longest-palindromic-substring/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class Solution {

/*
* Approach 1.
* time: O(n^3)
* space: O(1)
*/
public String longestPalindrome1(String s) {
String longest = "";
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
String ss = s.substring(i, j + 1);
if (isPalindrome(ss)) {
if (ss.length() > longest.length()) {
longest = ss;
}
}
}
}
return longest;
}

// Approach 1.
private boolean isPalindrome(String s) {
int left = 0;
int right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}

/*
* Approach 2-1.
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다.
* space: O(1)
*/
class Solution {

public String longestPalindrome(String s) {
int maxStart = 0;
int maxEnd = 0;

for (int i = 0; i < s.length(); i++) {
int start = i;
int end = i;
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) {
if (maxEnd - maxStart < end - start) {
maxStart = start;
maxEnd = end;
}
start--;
end++;
}

start = i;
end = i + 1;
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 while 블록이 위에 있는 while 블록과 완전히 동일한것 같은데
맞다면 중복을 제거해보면 어떠실까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 맞습니다 동일한 블록입니다 중복을 제거해봐도 좋겠네요 감사합니다!

if (maxEnd - maxStart < end - start) {
maxStart = start;
maxEnd = end;
}
start--;
end++;
}
}
return s.substring(maxStart, maxEnd + 1);
}
}

/*
* Approach 2-2.
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다.
* space: O(1)
*/
class Solution {
Comment on lines +75 to +80
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dev-jonghoonpark
리뷰 주신 부분 여기에 반영했습니다~ 덕분에 코드가 훨씬 깔끔해졌네요!
610f418

int maxStart = 0;
int maxEnd = 0;

public String longestPalindrome(String s) {
for (int i = 0; i < s.length(); i++) {
calculateMaxLength(i, i, s);
calculateMaxLength(i, i+1, s);
}
return s.substring(maxStart, maxEnd + 1);
}

public void calculateMaxLength(int start, int end, String s){
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
if (this.maxEnd - this.maxStart < end - start) {
this.maxStart = start;
this.maxEnd = end;
}
start--;
end++;
}
}

}
}
42 changes: 42 additions & 0 deletions number-of-connected-components-in-an-undirected-graph/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
time: O(n+m), where n is the number of nodes and m is the number of edges in the graph.
space: O(n)
*/
class Solution {

public int countComponents(int n, int[][] edges) {
boolean[] visited = new boolean[n];

Map<Integer, List<Integer>> map = new HashMap<>();
for (int[] edge : edges) {
map.computeIfAbsent(edge[0], k -> new ArrayList<>())
.add(edge[1]);
map.computeIfAbsent(edge[1], k -> new ArrayList<>())
.add(edge[0]);
}

int ans = 0;
for (int i = 0; i < n; i++) {
if (visited[i]) {
continue;
}
dfs(i, map, visited);
ans++;
}
return ans;
}

public void dfs(int k, Map<Integer, List<Integer>> map, boolean[] visited) {
if (visited[k]) {
return;
}
visited[k] = true;
if (!map.containsKey(k)) {
return;
}
List<Integer> values = map.get(k);
for (int v : values) {
dfs(v, map, visited);
}
}
}