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 4 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;
}
}