Skip to content

[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
merged 9 commits into from
May 31, 2025
Merged
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
86 changes: 86 additions & 0 deletions clone-graph/YoungSeok-Choi.java
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);
}
}
}
67 changes: 67 additions & 0 deletions linked-list-cycle/YoungSeok-Choi.java
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;
}
}
}
86 changes: 86 additions & 0 deletions longest-common-subsequence/YoungSeok-Choi.java
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<>();

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;
}
}
102 changes: 102 additions & 0 deletions maximum-product-subarray/YoungSeok-Choi.java
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;
}
}
Loading