Skip to content

Commit fb49cce

Browse files
[YoungSeok-Choi] Week 9 Solutions (#1528)
* add: linked-list-cycle * add: [week-8] clong-graph * add: Pacific Atlantic Water Flow * add: Sum of Two Integers * add: maximon product subarry * fix: lint * add: Palindrimic substrings * add: lcs, max product subArray * add: min-window-substr --------- Co-authored-by: Yongseok.choi <[email protected]>
1 parent ee81a6d commit fb49cce

File tree

8 files changed

+633
-0
lines changed

8 files changed

+633
-0
lines changed

clone-graph/YoungSeok-Choi.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/*
5+
// Definition for a Node.
6+
class Node {
7+
public int val;
8+
public List<Node> neighbors;
9+
public Node() {
10+
val = 0;
11+
neighbors = new ArrayList<Node>();
12+
}
13+
public Node(int _val) {
14+
val = _val;
15+
neighbors = new ArrayList<Node>();
16+
}
17+
public Node(int _val, ArrayList<Node> _neighbors) {
18+
val = _val;
19+
neighbors = _neighbors;
20+
}
21+
}
22+
*/
23+
class Solution {
24+
public Map<Node, Node> nMap = new HashMap<>();
25+
26+
public Node cloneGraph(Node node) {
27+
if (node == null)
28+
return null;
29+
30+
if (nMap.containsKey(node)) {
31+
return nMap.get(node);
32+
}
33+
34+
Node clone = new Node(node.val);
35+
nMap.put(node, clone);
36+
37+
for (Node nei : node.neighbors) {
38+
clone.neighbors.add(cloneGraph(nei));
39+
}
40+
41+
return clone;
42+
}
43+
}
44+
45+
class WrongSolution {
46+
public Node graph;
47+
public Map<Node, Node> nMap = new HashMap<>();
48+
49+
public Node cloneGraph(Node node) {
50+
if (node == null)
51+
return null;
52+
53+
graph = new Node(node.val);
54+
nMap.put(node, graph);
55+
56+
// print(graph);
57+
58+
return clone(node, graph);
59+
}
60+
61+
public Node clone(Node node, Node cur) {
62+
for (int i = 0; i < node.neighbors.size(); i++) {
63+
Node adj = node.neighbors.get(i);
64+
65+
if (nMap.containsKey(adj)) {
66+
return nMap.get(adj);
67+
}
68+
69+
nMap.put(adj, new Node(adj.val));
70+
cur.neighbors.add(nMap.get(adj));
71+
clone(adj, nMap.get(adj));
72+
}
73+
74+
return cur;
75+
}
76+
77+
public void print(Node node) {
78+
System.out.println("visit " + node.val);
79+
80+
for (int i = 0; i < node.neighbors.size(); i++) {
81+
Node adj = node.neighbors.get(i);
82+
System.out.println("nei " + adj.val);
83+
print(adj);
84+
}
85+
}
86+
}

linked-list-cycle/YoungSeok-Choi.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.HashSet;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class Solution {
9+
public boolean hasCycle(ListNode head) {
10+
Set<ListNode> nSet = new HashSet<>();
11+
12+
while (head != null) {
13+
if (nSet.contains(head)) {
14+
return true;
15+
}
16+
17+
nSet.add(head);
18+
head = head.next;
19+
}
20+
21+
return false;
22+
}
23+
}
24+
25+
/**
26+
* Definition for singly-linked list.
27+
* class ListNode {
28+
* int val;
29+
* ListNode next;
30+
* ListNode(int x) {
31+
* val = x;
32+
* next = null;
33+
* }
34+
* }
35+
*/
36+
// NOTE: 같은 val의 다른 Node의 경우를 고려하지 못했다
37+
class WrongSolution {
38+
public boolean hasCycle(ListNode head) {
39+
Map<Integer, List<Integer>> vMap = new HashMap<>(); // NOTE: val, pos
40+
41+
if (head == null) {
42+
return false;
43+
}
44+
45+
ListNode cur = head;
46+
boolean isCyclick = false;
47+
int pos = -1;
48+
while (cur != null) {
49+
50+
if (vMap.containsKey(cur.val)) {
51+
isCyclick = true;
52+
break;
53+
}
54+
55+
List<Integer> posList = new ArrayList<>();
56+
posList.add(pos++);
57+
vMap.put(cur.val, posList);
58+
cur = head.next;
59+
}
60+
61+
if (isCyclick) {
62+
return true;
63+
} else {
64+
return false;
65+
}
66+
}
67+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
class Solution {
8+
private Map<String, Integer> memo = new HashMap<>();
9+
10+
public int longestCommonSubsequence(String t1, String t2) {
11+
return lcs(0, 0, t1, t2);
12+
}
13+
14+
private int lcs(int i, int j, String t1, String t2) {
15+
if (i == t1.length() || j == t2.length()) {
16+
return 0;
17+
}
18+
19+
String key = i + "," + j;
20+
if (memo.containsKey(key)) {
21+
return memo.get(key);
22+
}
23+
24+
int res;
25+
if (t1.charAt(i) == t2.charAt(j)) {
26+
res = 1 + lcs(i + 1, j + 1, t1, t2);
27+
} else {
28+
res = Math.max(lcs(i + 1, j, t1, t2), lcs(i, j + 1, t1, t2));
29+
}
30+
31+
memo.put(key, res);
32+
return res;
33+
}
34+
}
35+
36+
class WrongSolution {
37+
public int longestCommonSubsequence(String t1, String t2) {
38+
Map<Integer, List<Integer>> sMap = new HashMap<>();
39+
int cnt = 0;
40+
41+
// t1의 연속으로 등장하는 문자열을 하나로만 바꾸기 (e.g. abbbvvvvssssmmmddd -> abvsmd)
42+
StringBuilder sb = new StringBuilder();
43+
char start = t1.charAt(0);
44+
sb.append(start);
45+
for (int i = 1; i < t1.length(); i++) {
46+
if (start != t1.charAt(i)) {
47+
sb.append(t1.charAt(i));
48+
start = t1.charAt(i);
49+
}
50+
}
51+
52+
t1 = sb.toString();
53+
54+
for (int i = 0; i < t1.length(); i++) {
55+
sMap.computeIfAbsent(t1.charAt(i) - 97, k -> new ArrayList<>()).add(i);
56+
}
57+
58+
StringBuilder filtered = new StringBuilder();
59+
for (int i = 0; i < t2.length(); i++) {
60+
int alpIdx = t2.charAt(i) - 97;
61+
if (sMap.containsKey(alpIdx)) {
62+
filtered.append(t2.charAt(i));
63+
}
64+
}
65+
t2 = filtered.toString();
66+
67+
int prevT1Idx = -1;
68+
for (int i = 0; i < t2.length(); i++) {
69+
int alpIdx = t2.charAt(i) - 97;
70+
71+
List<Integer> idxList = sMap.get(alpIdx);
72+
if (idxList == null)
73+
continue;
74+
75+
for (int idx : idxList) {
76+
if (idx > prevT1Idx) {
77+
cnt++;
78+
prevT1Idx = idx;
79+
break;
80+
}
81+
}
82+
}
83+
84+
return cnt;
85+
}
86+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
// 답지보고 풀이..
5+
class Solution {
6+
public int maxProduct(int[] nums) {
7+
int max = nums[0];
8+
int currMax = nums[0];
9+
int currMin = nums[0];
10+
11+
for (int i = 1; i < nums.length; i++) {
12+
int temp = currMax;
13+
currMax = Math.max(nums[i], Math.max(currMax * nums[i], currMin * nums[i]));
14+
currMin = Math.min(nums[i], Math.min(temp * nums[i], currMin * nums[i]));
15+
max = Math.max(max, currMax);
16+
}
17+
18+
return max;
19+
}
20+
}
21+
22+
// NOTE:핵심 컨셉은 0을 만나기 전까지 한칸씩 늘려가며 모든 원소의 곱을 만든다. 0을 만나면 첫 번째 원소를 뺀 나머지 원소들의 곱의
23+
// 값으로 최대값을 계속 갱신해나간다.
24+
25+
// 위의 아이디어로 문제를 풀이했지만, 모든 경우의 예외를 핸들링 할 수가 없어 풀이를 더이상 진행하지 않았다..
26+
class WrongSolution {
27+
28+
public int mx = -98764321;
29+
public List<Boolean> bList = new ArrayList<>();
30+
public boolean isStartInit = true;
31+
public boolean isOverZero = false;
32+
33+
public int maxProduct(int[] nums) {
34+
int start = 1;
35+
int zeroIdx = 0;
36+
if (nums.length == 1) {
37+
return nums[0];
38+
}
39+
40+
for (int i = 0; i < nums.length; i++) {
41+
42+
if (nums[i] == 0) {
43+
if (mx <= 0) {
44+
bList.add(false);
45+
} else {
46+
bList.add(true);
47+
}
48+
49+
while (zeroIdx < i) {
50+
start /= nums[zeroIdx];
51+
mx = Math.max(mx, start);
52+
53+
if (mx >= 1) {
54+
bList.add(true);
55+
}
56+
57+
zeroIdx++;
58+
}
59+
60+
start = 1;
61+
isStartInit = true;
62+
zeroIdx++; // 0값인 인덱스를 건너뛰고 다음 번 인덱스를 대입
63+
} else {
64+
start *= nums[i];
65+
mx = Math.max(mx, start);
66+
67+
if (start >= 1) {
68+
isStartInit = false;
69+
isOverZero = true;
70+
}
71+
}
72+
}
73+
74+
while (zeroIdx < nums.length) {
75+
start /= nums[zeroIdx];
76+
mx = Math.max(mx, start);
77+
zeroIdx++;
78+
}
79+
80+
mx = Math.max(mx, nums[nums.length - 1]);
81+
82+
if (nums[nums.length - 1] >= 1) {
83+
isOverZero = true;
84+
}
85+
86+
if (mx > 0 && isOverZero) {
87+
bList.add(true);
88+
}
89+
90+
for (boolean b : bList) {
91+
if (b) {
92+
return mx;
93+
}
94+
}
95+
96+
if (bList.size() > 0) {
97+
return isStartInit ? 0 : mx;
98+
}
99+
100+
return mx;
101+
}
102+
}

0 commit comments

Comments
 (0)