Skip to content
Merged
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
14 changes: 14 additions & 0 deletions best-time-to-buy-and-sell-stock/hyunolike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int maxProfit(int[] prices) {
// 주식 최대 이익
int minPrice = Integer.MAX_VALUE;
int maxProfit = 0;

for (int price : prices) {
minPrice = Math.min(minPrice, price);
maxProfit = Math.max(maxProfit, price - minPrice);
}

return maxProfit;
}
}
39 changes: 39 additions & 0 deletions encode-and-decode-strings/hyunolike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Codec {

// Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();

for (String s : strs) {
sb.append(s.length()).append('#').append(s);
}

return sb.toString();
}

// Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> res = new ArrayList<>();
int i = 0;

while (i < s.length()) {
int j = i;

// find '#'
while (s.charAt(j) != '#') {
j++;
}

int length = Integer.parseInt(s.substring(i, j));
j++; // move past '#'

String word = s.substring(j, j + length);
res.add(word);

i = j + length; // move to next encoded segment
}

return res;
}
}

22 changes: 22 additions & 0 deletions group-anagrams/hyunolike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
// 같은 문자열 끼리 묶기
// 그룹이 가장 적은 순으로 정렬

Map<String, List<String>> map = new HashMap<>();

// 각 문자열 char로 바꿔서 정렬하고 다시 문자열
for(String str : strs) {
char[] arr = str.toCharArray();
Arrays.sort(arr);

String key = new String(arr); // 정렬된 문자열 키로 하기

map.computeIfAbsent(key, k -> new ArrayList<>()).add(str);
}

return new ArrayList<>(map.values());
// map 자료구조, 정렬까지 생각
// 이후 프로세스 생각 어려움
}
}
55 changes: 55 additions & 0 deletions implement-trie-prefix-tree/hyunolike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class TrieNode {
TrieNode[] children = new TrieNode[26];
boolean isEnd;
}

class Trie {

private TrieNode root;

public Trie() {
root = new TrieNode();
}

public void insert(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int idx = c - 'a';
if(node.children[idx] == null) {
node.children[idx] = new TrieNode();
}
node = node.children[idx];
}
node.isEnd = true;
}

public boolean search(String word) {
TrieNode node = root;
for(char c : word.toCharArray()) {
int idx = c - 'a';
if(node.children[idx] == null) return false;
node = node.children[idx];
}

return node.isEnd;
}

public boolean startsWith(String prefix) {
TrieNode node = root;
for(char c : prefix.toCharArray()) {
int idx = c - 'a';
if(node.children[idx] == null) return false;
node = node.children[idx];
}
return true;
}
}

/**
* 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);
*/

20 changes: 20 additions & 0 deletions word-break/hyunolike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
boolean[] dp = new boolean[n+1];
dp[0] = true;

Set<String> wordSet = new HashSet<>(wordDict);

for(int i = 1; i <= n; i++) {
for(int j = 0; j < i; j++) {
if(dp[j] && wordSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
}
}

17 changes: 17 additions & 0 deletions word-break/hyunolike.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function wordBreak(s: string, wordDict: string[]): boolean {
const n = s.length;
const dp:boolean[] = Array(n+1).fill(false);
dp[0] = true;

const wordSet = new Set(wordDict);

for(let i = 1; i <= n; i++) {
for(let j = 0; j < i; j++) {
if(dp[j] && wordSet.has(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[n];
};