Skip to content

[YoungSeok-Choi] Week 5 Solutions #1380

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions best-time-to-buy-and-sell-stock/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// NOTE: tc --> O(n)
class Solution {
public int maxProfit(int[] prices) {

int curMax = 0;
int gMax = 0;

if(prices.length == 0) return 0;

int sell = prices[0];
for(int i = 1; i < prices.length; i++) {
curMax = Math.max(0, prices[i] - sell);

// NOTE: 새롭게 시작하는게 더 좋은경우
if(curMax == 0) {
sell = prices[i];
}

gMax = Math.max(curMax, gMax);
}

return gMax;
}
}
45 changes: 45 additions & 0 deletions encode-and-decode-strings/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;
import java.util.List;

public class Solution {
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
List<String> temp = new ArrayList<>();

if(strs.size() == 0) return null;

for(String s : strs) {
if(":".equals(s)) {
temp.add("::");
} else {
temp.add(s);
}
}

return String.join(":;", temp);
}

/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
public List<String> decode(String str) {
List<String> temp = new ArrayList<>();

if(str == null) return new ArrayList<>();

// if(str.length() == 0) return new ArrayList<>();

for(String s : str.split(":;")) {
if("::".equals(s)) {
temp.add(":");
} else {
temp.add(s);
}
}
return temp;
}
}
34 changes: 34 additions & 0 deletions group-anagrams/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// NOTE: tc -> O(n)
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {

List<List<String>> result = new ArrayList<>();
Map<String, List<String>> sMap = new HashMap<>();

for(int i = 0; i < strs.length; i++) {
char[] cArr = strs[i].toCharArray();
Arrays.sort(cArr);
String sorted = new String(cArr);

if(sMap.containsKey(sorted)) {
sMap.get(sorted).add(strs[i]);
} else {
List<String> temp = new ArrayList<>();
temp.add(strs[i]);
sMap.put(sorted, temp);
}
}

for(List<String> arr : sMap.values()) {
result.add(arr);
}

return result;
}
}
37 changes: 37 additions & 0 deletions implement-trie-prefix-tree/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.HashMap;
import java.util.Map;

// Map으로 풀려버려서 당황..
// 이진트리? 어떤식으로 풀어야 할지 자료구조 정하고 다시 풀어보기..
class Trie {

Map<String, Boolean> tMap;

public Trie() {
this.tMap = new HashMap<>();
}

public void insert(String word) {
this.tMap.put(word, true);
}

public boolean search(String word) {
return this.tMap.containsKey(word);
}

public boolean startsWith(String prefix) {
for(String key : this.tMap.keySet()) {
if(key.startsWith(prefix)) return true;
}

return false;
}
}

/**
* Your Trie object will be instantiated and called as such:
* Trie obj = new Trie();
* obj.insert(word);
* boolean param_2 = obj.search(word);
* boolean param_3 = obj.startsWith(prefix);
*/
80 changes: 80 additions & 0 deletions word-break/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

// DFS 완전탐색할 때 불필요한 탐색을 줄이는 방법을 항상 고민할 것.
class Solution {
public int size = 0;
public Map<String, Boolean> failedMap = new HashMap<>();
public boolean wordBreak(String s, List<String> wordDict) {
size = wordDict.size();

Set<Character> sSet = new HashSet<>();
for (char c : s.toCharArray()) {
sSet.add(c);
}

Set<Character> wSet = new HashSet<>();
for (char c : String.join("", wordDict).toCharArray()) {
wSet.add(c);
}

if(sSet.size() > wSet.size()) {
return false;
}

return dfs(s, wordDict);
}

public boolean dfs(String s, List<String> wordDict) {
if(s.length() == 0) return true;
if(failedMap.containsKey(s)) return false;

for(int i = 0; i < size; i++) {
String word = wordDict.get(i);
if(s.startsWith(word)) {

s = s.substring(word.length());
boolean result = dfs(s, wordDict);

if(result) {
return true;
} else {
failedMap.put(s, true);
}

s = word + s;
}
}

return false;
}
}

// 특정 문자로 시작되는 것만 판단하여 반복해 풀려고 했던 접근법.
// 전체 조합을 보아야 하는 "cars", ["cars", "ca", "rs"] 경우에 반례가 됨.
class WrongSolution {
public boolean wordBreak(String s, List<String> wordDict) {
int size = wordDict.size();

while(true) {
boolean isMatched = false;
for(int i = 0; i < size; i++) {
String word = wordDict.get(i);
if(s.startsWith(word)) {
isMatched = true;
s = s.substring(word.length());
break;
}
}

if(!isMatched) {
break;
}
}

return s.length() == 0;
}
}