Skip to content

Commit f518058

Browse files
authored
Merge pull request #166 from bky373/main
[bky373] 10th week solutions
2 parents 4bcdf88 + 5e03df3 commit f518058

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

graph-valid-tree/bky373.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
time: O(n + m), where n is the number of nodes and m is the number of edges in the graph.
3+
space: O(n + m)
4+
*/
5+
class Solution {
6+
7+
public boolean validTree(int n, int[][] edges) {
8+
9+
if (edges.length != n - 1) {
10+
return false;
11+
}
12+
13+
List<List<Integer>> adjList = new ArrayList<>();
14+
for (int i = 0; i < n; i++) {
15+
adjList.add(new ArrayList<>());
16+
}
17+
for (int[] edge : edges) {
18+
adjList.get(edge[0])
19+
.add(edge[1]);
20+
adjList.get(edge[1])
21+
.add(edge[0]);
22+
}
23+
24+
Stack<Integer> stack = new Stack<>();
25+
Set<Integer> visited = new HashSet<>();
26+
stack.push(0);
27+
visited.add(0);
28+
29+
while (!stack.isEmpty()) {
30+
int curr = stack.pop();
31+
for (int adj : adjList.get(curr)) {
32+
if (visited.contains(adj)) {
33+
continue;
34+
}
35+
visited.add(adj);
36+
stack.push(adj);
37+
}
38+
}
39+
40+
return visited.size() == n;
41+
}
42+
}

house-robber-ii/bky373.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
time: O(N)
3+
time: O(1)
4+
*/
5+
class Solution {
6+
7+
public int rob(int[] nums) {
8+
if (nums.length == 0) {
9+
return 0;
10+
}
11+
if (nums.length == 1) {
12+
return nums[0];
13+
}
14+
15+
return Math.max(
16+
dp(nums, 0, nums.length - 2),
17+
dp(nums, 1, nums.length - 1)
18+
);
19+
}
20+
21+
private static int dp(int[] nums, int start, int end) {
22+
int maxOfOneStepAhead = nums[end];
23+
int maxOfTwoStepsAhead = 0;
24+
25+
for (int i = end - 1; i >= start; --i) {
26+
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]);
27+
28+
maxOfTwoStepsAhead = maxOfOneStepAhead;
29+
maxOfOneStepAhead = curr;
30+
}
31+
return maxOfOneStepAhead;
32+
}
33+
}

house-robber/bky373.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
time: O(N)
3+
space: O(1)
4+
*/
5+
class Solution {
6+
7+
public int rob(int[] nums) {
8+
if (nums.length == 0) {
9+
return 0;
10+
}
11+
12+
int maxOfTwoStepsAhead = 0;
13+
int maxOfOneStepAhead = nums[nums.length - 1];
14+
15+
for (int i = nums.length - 2; i >= 0; --i) {
16+
int curr = Math.max(maxOfOneStepAhead, maxOfTwoStepsAhead + nums[i]);
17+
18+
maxOfTwoStepsAhead = maxOfOneStepAhead;
19+
maxOfOneStepAhead = curr;
20+
}
21+
return maxOfOneStepAhead;
22+
}
23+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
class Solution {
2+
3+
/*
4+
* Approach 1.
5+
* time: O(n^3)
6+
* space: O(1)
7+
*/
8+
public String longestPalindrome1(String s) {
9+
String longest = "";
10+
for (int i = 0; i < s.length(); i++) {
11+
for (int j = i; j < s.length(); j++) {
12+
String ss = s.substring(i, j + 1);
13+
if (isPalindrome(ss)) {
14+
if (ss.length() > longest.length()) {
15+
longest = ss;
16+
}
17+
}
18+
}
19+
}
20+
return longest;
21+
}
22+
23+
// Approach 1.
24+
private boolean isPalindrome(String s) {
25+
int left = 0;
26+
int right = s.length() - 1;
27+
while (left < right) {
28+
if (s.charAt(left) != s.charAt(right)) {
29+
return false;
30+
}
31+
left++;
32+
right--;
33+
}
34+
return true;
35+
}
36+
37+
/*
38+
* Approach 2-1.
39+
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다.
40+
* space: O(1)
41+
*/
42+
class Solution {
43+
44+
public String longestPalindrome(String s) {
45+
int maxStart = 0;
46+
int maxEnd = 0;
47+
48+
for (int i = 0; i < s.length(); i++) {
49+
int start = i;
50+
int end = i;
51+
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) {
52+
if (maxEnd - maxStart < end - start) {
53+
maxStart = start;
54+
maxEnd = end;
55+
}
56+
start--;
57+
end++;
58+
}
59+
60+
start = i;
61+
end = i + 1;
62+
while (0 <= start && end < s.length() && s.charAt(start) == s.charAt(end)) {
63+
if (maxEnd - maxStart < end - start) {
64+
maxStart = start;
65+
maxEnd = end;
66+
}
67+
start--;
68+
end++;
69+
}
70+
}
71+
return s.substring(maxStart, maxEnd + 1);
72+
}
73+
}
74+
75+
/*
76+
* Approach 2-2.
77+
* time: 시간 복잡도는 평균적으로 O(n)이고, palindrome 의 길이가 n 에 가까워지면 시간 복잡도 역시 O(n^2) 에 가까워 진다.
78+
* space: O(1)
79+
*/
80+
class Solution {
81+
int maxStart = 0;
82+
int maxEnd = 0;
83+
84+
public String longestPalindrome(String s) {
85+
for (int i = 0; i < s.length(); i++) {
86+
calculateMaxLength(i, i, s);
87+
calculateMaxLength(i, i+1, s);
88+
}
89+
return s.substring(maxStart, maxEnd + 1);
90+
}
91+
92+
public void calculateMaxLength(int start, int end, String s){
93+
while (start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
94+
if (this.maxEnd - this.maxStart < end - start) {
95+
this.maxStart = start;
96+
this.maxEnd = end;
97+
}
98+
start--;
99+
end++;
100+
}
101+
}
102+
103+
}
104+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
time: O(n+m), where n is the number of nodes and m is the number of edges in the graph.
3+
space: O(n)
4+
*/
5+
class Solution {
6+
7+
public int countComponents(int n, int[][] edges) {
8+
boolean[] visited = new boolean[n];
9+
10+
Map<Integer, List<Integer>> map = new HashMap<>();
11+
for (int[] edge : edges) {
12+
map.computeIfAbsent(edge[0], k -> new ArrayList<>())
13+
.add(edge[1]);
14+
map.computeIfAbsent(edge[1], k -> new ArrayList<>())
15+
.add(edge[0]);
16+
}
17+
18+
int ans = 0;
19+
for (int i = 0; i < n; i++) {
20+
if (visited[i]) {
21+
continue;
22+
}
23+
dfs(i, map, visited);
24+
ans++;
25+
}
26+
return ans;
27+
}
28+
29+
public void dfs(int k, Map<Integer, List<Integer>> map, boolean[] visited) {
30+
if (visited[k]) {
31+
return;
32+
}
33+
visited[k] = true;
34+
if (!map.containsKey(k)) {
35+
return;
36+
}
37+
List<Integer> values = map.get(k);
38+
for (int v : values) {
39+
dfs(v, map, visited);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)