- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 195
[YoungSeok-Choi] Week 9 Solutions #1528
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
+633
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e2f9d9f
add: linked-list-cycle
Yongseok-Choi-dev aed3ca8
add: [week-8] clong-graph
Yongseok-Choi-dev 1b3b88e
add: Pacific Atlantic Water Flow
Yongseok-Choi-dev d0237c7
add: Sum of Two Integers
Yongseok-Choi-dev b071d3e
add: maximon product subarry
Yongseok-Choi-dev 110d2e3
fix: lint
Yongseok-Choi-dev 7d72d80
add: Palindrimic substrings
Yongseok-Choi-dev 678bb7d
add: lcs, max product subArray
Yongseok-Choi-dev e6dd7cd
add: min-window-substr
Yongseok-Choi-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/* | ||
// 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; | ||
} | ||
} | ||
*/ | ||
class Solution { | ||
public Map<Node, Node> nMap = new HashMap<>(); | ||
|
||
public Node cloneGraph(Node node) { | ||
if (node == null) | ||
return null; | ||
|
||
if (nMap.containsKey(node)) { | ||
return nMap.get(node); | ||
} | ||
|
||
Node clone = new Node(node.val); | ||
nMap.put(node, clone); | ||
|
||
for (Node nei : node.neighbors) { | ||
clone.neighbors.add(cloneGraph(nei)); | ||
} | ||
|
||
return clone; | ||
} | ||
} | ||
|
||
class WrongSolution { | ||
public Node graph; | ||
public Map<Node, Node> nMap = new HashMap<>(); | ||
|
||
public Node cloneGraph(Node node) { | ||
if (node == null) | ||
return null; | ||
|
||
graph = new Node(node.val); | ||
nMap.put(node, graph); | ||
|
||
// print(graph); | ||
|
||
return clone(node, graph); | ||
} | ||
|
||
public Node clone(Node node, Node cur) { | ||
for (int i = 0; i < node.neighbors.size(); i++) { | ||
Node adj = node.neighbors.get(i); | ||
|
||
if (nMap.containsKey(adj)) { | ||
return nMap.get(adj); | ||
} | ||
|
||
nMap.put(adj, new Node(adj.val)); | ||
cur.neighbors.add(nMap.get(adj)); | ||
clone(adj, nMap.get(adj)); | ||
} | ||
|
||
return cur; | ||
} | ||
|
||
public void print(Node node) { | ||
System.out.println("visit " + node.val); | ||
|
||
for (int i = 0; i < node.neighbors.size(); i++) { | ||
Node adj = node.neighbors.get(i); | ||
System.out.println("nei " + adj.val); | ||
print(adj); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class Solution { | ||
public boolean hasCycle(ListNode head) { | ||
Set<ListNode> nSet = new HashSet<>(); | ||
|
||
while (head != null) { | ||
if (nSet.contains(head)) { | ||
return true; | ||
} | ||
|
||
nSet.add(head); | ||
head = head.next; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* int val; | ||
* ListNode next; | ||
* ListNode(int x) { | ||
* val = x; | ||
* next = null; | ||
* } | ||
* } | ||
*/ | ||
// NOTE: 같은 val의 다른 Node의 경우를 고려하지 못했다 | ||
class WrongSolution { | ||
public boolean hasCycle(ListNode head) { | ||
Map<Integer, List<Integer>> vMap = new HashMap<>(); // NOTE: val, pos | ||
|
||
if (head == null) { | ||
return false; | ||
} | ||
|
||
ListNode cur = head; | ||
boolean isCyclick = false; | ||
int pos = -1; | ||
while (cur != null) { | ||
|
||
if (vMap.containsKey(cur.val)) { | ||
isCyclick = true; | ||
break; | ||
} | ||
|
||
List<Integer> posList = new ArrayList<>(); | ||
posList.add(pos++); | ||
vMap.put(cur.val, posList); | ||
cur = head.next; | ||
} | ||
|
||
if (isCyclick) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class Solution { | ||
private Map<String, Integer> memo = new HashMap<>(); | ||
YoungSeok-Choi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public int longestCommonSubsequence(String t1, String t2) { | ||
return lcs(0, 0, t1, t2); | ||
} | ||
|
||
private int lcs(int i, int j, String t1, String t2) { | ||
if (i == t1.length() || j == t2.length()) { | ||
return 0; | ||
} | ||
|
||
String key = i + "," + j; | ||
if (memo.containsKey(key)) { | ||
return memo.get(key); | ||
} | ||
|
||
int res; | ||
if (t1.charAt(i) == t2.charAt(j)) { | ||
res = 1 + lcs(i + 1, j + 1, t1, t2); | ||
} else { | ||
res = Math.max(lcs(i + 1, j, t1, t2), lcs(i, j + 1, t1, t2)); | ||
} | ||
|
||
memo.put(key, res); | ||
return res; | ||
} | ||
} | ||
|
||
class WrongSolution { | ||
public int longestCommonSubsequence(String t1, String t2) { | ||
Map<Integer, List<Integer>> sMap = new HashMap<>(); | ||
int cnt = 0; | ||
|
||
// t1의 연속으로 등장하는 문자열을 하나로만 바꾸기 (e.g. abbbvvvvssssmmmddd -> abvsmd) | ||
StringBuilder sb = new StringBuilder(); | ||
char start = t1.charAt(0); | ||
sb.append(start); | ||
for (int i = 1; i < t1.length(); i++) { | ||
if (start != t1.charAt(i)) { | ||
sb.append(t1.charAt(i)); | ||
start = t1.charAt(i); | ||
} | ||
} | ||
|
||
t1 = sb.toString(); | ||
|
||
for (int i = 0; i < t1.length(); i++) { | ||
sMap.computeIfAbsent(t1.charAt(i) - 97, k -> new ArrayList<>()).add(i); | ||
} | ||
|
||
StringBuilder filtered = new StringBuilder(); | ||
for (int i = 0; i < t2.length(); i++) { | ||
int alpIdx = t2.charAt(i) - 97; | ||
if (sMap.containsKey(alpIdx)) { | ||
filtered.append(t2.charAt(i)); | ||
} | ||
} | ||
t2 = filtered.toString(); | ||
|
||
int prevT1Idx = -1; | ||
for (int i = 0; i < t2.length(); i++) { | ||
int alpIdx = t2.charAt(i) - 97; | ||
|
||
List<Integer> idxList = sMap.get(alpIdx); | ||
if (idxList == null) | ||
continue; | ||
|
||
for (int idx : idxList) { | ||
if (idx > prevT1Idx) { | ||
cnt++; | ||
prevT1Idx = idx; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return cnt; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// 답지보고 풀이.. | ||
class Solution { | ||
public int maxProduct(int[] nums) { | ||
int max = nums[0]; | ||
int currMax = nums[0]; | ||
int currMin = nums[0]; | ||
|
||
for (int i = 1; i < nums.length; i++) { | ||
int temp = currMax; | ||
currMax = Math.max(nums[i], Math.max(currMax * nums[i], currMin * nums[i])); | ||
currMin = Math.min(nums[i], Math.min(temp * nums[i], currMin * nums[i])); | ||
max = Math.max(max, currMax); | ||
} | ||
|
||
return max; | ||
} | ||
} | ||
|
||
// NOTE:핵심 컨셉은 0을 만나기 전까지 한칸씩 늘려가며 모든 원소의 곱을 만든다. 0을 만나면 첫 번째 원소를 뺀 나머지 원소들의 곱의 | ||
// 값으로 최대값을 계속 갱신해나간다. | ||
|
||
// 위의 아이디어로 문제를 풀이했지만, 모든 경우의 예외를 핸들링 할 수가 없어 풀이를 더이상 진행하지 않았다.. | ||
class WrongSolution { | ||
|
||
public int mx = -98764321; | ||
public List<Boolean> bList = new ArrayList<>(); | ||
public boolean isStartInit = true; | ||
public boolean isOverZero = false; | ||
|
||
public int maxProduct(int[] nums) { | ||
int start = 1; | ||
int zeroIdx = 0; | ||
if (nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
|
||
if (nums[i] == 0) { | ||
if (mx <= 0) { | ||
bList.add(false); | ||
} else { | ||
bList.add(true); | ||
} | ||
|
||
while (zeroIdx < i) { | ||
start /= nums[zeroIdx]; | ||
mx = Math.max(mx, start); | ||
|
||
if (mx >= 1) { | ||
bList.add(true); | ||
} | ||
|
||
zeroIdx++; | ||
} | ||
|
||
start = 1; | ||
isStartInit = true; | ||
zeroIdx++; // 0값인 인덱스를 건너뛰고 다음 번 인덱스를 대입 | ||
} else { | ||
start *= nums[i]; | ||
mx = Math.max(mx, start); | ||
|
||
if (start >= 1) { | ||
isStartInit = false; | ||
isOverZero = true; | ||
} | ||
} | ||
} | ||
|
||
while (zeroIdx < nums.length) { | ||
start /= nums[zeroIdx]; | ||
mx = Math.max(mx, start); | ||
zeroIdx++; | ||
} | ||
|
||
mx = Math.max(mx, nums[nums.length - 1]); | ||
|
||
if (nums[nums.length - 1] >= 1) { | ||
isOverZero = true; | ||
} | ||
|
||
if (mx > 0 && isOverZero) { | ||
bList.add(true); | ||
} | ||
|
||
for (boolean b : bList) { | ||
if (b) { | ||
return mx; | ||
} | ||
} | ||
|
||
if (bList.size() > 0) { | ||
return isStartInit ? 0 : mx; | ||
} | ||
|
||
return mx; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.