Skip to content

Commit

Permalink
Merge pull request #841 from eunhwa99/main
Browse files Browse the repository at this point in the history
[eunhwa99] Week 5
  • Loading branch information
eunhwa99 authored Jan 8, 2025
2 parents d52bb73 + 77bcf76 commit 7ae7503
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
55 changes: 55 additions & 0 deletions best-time-to-buy-and-sell-stock/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ํ’€์ด ๋ฐฉ๋ฒ•
// 1. ์ธ๋ฑ์Šค x๊นŒ์ง€์˜ ์ตœ์†Œ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด 1๊ฐœ์™€, ์ธ๋ฑ์Šค x๋ถ€ํ„ฐ์˜ ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด 1๊ฐœ๋ฅผ ๋งŒ๋“ ๋‹ค.
// 2. 1๋ฒˆ์—์„œ ๋งŒ๋“  ๋‘ ๋ฐฐ์—ด์— ๊ฐ’์„ ์ฑ„์šด๋‹ค.
// 3. ๋‘ ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ์ธ๋ฑ์Šค ๋ณ„๋กœ ์ฐจ๋ฅผ ๊ตฌํ•˜๊ณ , ๊ทธ ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค.

// ์‹œ๊ฐ„ ๋ณต์žก๋„
// O(n) : ๋ฐฐ์—ด์„ 2๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ O(n)์ด๋‹ค.
// ๊ณต๊ฐ„ ๋ณต์žก๋„
// O(n) : ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ€๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ์œผ๋ฏ€๋กœ O(n)์ด๋‹ค.

class Solution {
public int maxProfit(int[] prices) {
int len = prices.length;
int[] minArr = new int[len];
int[] maxArr = new int[len];

for(int i=0;i<len;i++){
if(i==0){
minArr[i] = prices[i];
maxArr[len-i-1] = prices[len-i-1];
}else{
minArr[i] = Math.min(minArr[i-1], prices[i]);
maxArr[len-i-1] = Math.max(maxArr[len-i], prices[len-i-1]);
}
}

int result = 0;
for(int i=0;i<len;i++){
result = Math.max(result, maxArr[i]-minArr[i]);
}
return result;
}
}


// 2nd solution
// ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)

class Solution{
public int maxProfit(int[] prices){
int len = prices.length;
int buy = prices[0];
int result = 0;

for(int i=1;i<len;i++){
if(prices[i]<buy){ // ๋” ์ €๋ ดํ•œ ์ฃผ์‹์ด ์žˆ์œผ๋ฏ€๋กœ
buy = prices[i]; // ์ด ์ฃผ์‹์„ ์‚ฐ๋‹ค.
}else{
result = Math.max(result, prices[i]-buy); // ํ˜„์žฌ ์ฃผ์‹์„ ํŒ”์•˜์„ ๋•Œ ์ด๋“์ด ๋” ํฌ๋‹ค๋ฉด ํŒ๋‹ค.
}
}
return result;
}
}
48 changes: 48 additions & 0 deletions encode-and-decode-strings/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;
// n: ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ ๊ธธ์ด, m: ๊ฐ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด.

// ์‹œ๊ฐ„ ๋ณต์žก๋„
// Encoding: O(n * m)
// Decoding: O(n * m)
// ๊ณต๊ฐ„ ๋ณต์žก๋„
// Encoding: O(n * m),
// Decoding: O(n * m)
class Solution {

// ๋ฐ›์•„์˜จ string ๋“ค์„ ํ•œ ๋ฌธ์ž์—ด๋กœ ํ•ฉ์นจ
// (๋ฌธ์ž์—ด๊ธธ์ด)#(๋ฌธ์ž์—ด) ํ˜•์‹
public String encode(List<String> strs) {
StringBuilder encodedStr = new StringBuilder();
for (String str : strs) {
encodedStr.append(str.length()).append('#').append(str);
}
return encodedStr.toString();
}

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

while (i < s.length()) {

int j = i;
while (s.charAt(j) != '#') { // # ๋ฌธ์ž ์ฐพ๊ธฐ
j++;
}

// ๋ฌธ์ž์—ด ๊ธธ์ด ์ถ”์ถœ
int length = Integer.parseInt(s.substring(i, j));

// ์œ„์—์„œ ๊ตฌํ•œ ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด ์ถ”์ถœ
result.add(s.substring(j + 1, j + 1 + length));

// i ์œ„์น˜ ๋ณ€๊ฒฝ
i = j + 1 + length;
}

return result;
}

}
31 changes: 31 additions & 0 deletions group-anagrams/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// ํ•ด๊ฒฐ๋ฒ•
// 1. ๋ฐฐ์—ด ์•ˆ์˜ ์›”์†Œ๋“ค์„ ์ˆœํšŒํ•œ๋‹ค.
// 2. ๊ฐ ์›์†Œ์— ๋Œ€ํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•œ ํ›„, hashmap์— ๋„ฃ๋Š”๋‹ค. ์ด ๋•Œ hashMap ์ž๋ฃŒ๊ตฌ์กฐ๋Š” <String, List<String>> ํ˜•ํƒœ๋กœ ๋งŒ๋“ ๋‹ค.

// ์‹œ๊ฐ„ ๋ณต์žก๋„:
// 1. ํฌ๊ธฐ๊ฐ€ m์ธ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•  ๋•Œ O(mlogm)์ด ์†Œ์š”
// 2. ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ n ์ด๋ฏ€๋กœ ์ˆœํšŒํ•˜๋Š”๋ฐ O(n)
// 3. hashmap ์‚ฝ์ž…์€ O(1)
// ์œ„ ์‚ฌ์‹ค๋“ค์„ ์กฐํ•ฉํ•˜๋ฉด ์ด O(nmlogm)์ด ์†Œ์š”๋œ๋‹ค.
// ์ฐธ๊ณ ๋กœ, ์ „์ฒด ๋ฐฐ์—ด ํฌ๊ธฐ๋Š” ์ตœ๋Œ€ 10^4 ์ด๊ณ , ๊ฐ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100์ด๋ฏ€๋กœ, ์œ„์™€ ๊ฐ™์ด ์ ‘๊ทผํ•  ๊ฒฝ์šฐ, ์ตœ๋Œ€ 10^6์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

// ๊ณต๊ฐ„ ๋ณต์žก๋„: hashmap ํฌ๊ธฐ๋งŒํผ ์‚ฌ์šฉ -> O(nm)

class Solution{
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<>();
for(String str: strs){
char[] charArr = str.toCharArray();
Arrays.sort(charArr);

map.computeIfAbsent(String.valueOf(charArr), key -> new ArrayList<>()).add(str);
}
return map.values().stream().toList();
}
}
35 changes: 35 additions & 0 deletions implement-trie-prefix-tree/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.HashMap;
import java.util.Map;

// insert ์‹œ, ๋ฌธ์ž์—ด์˜ prefix๋ฅผ ๋‹ค Map์— ์ €์žฅํ•˜๊ณ , ํ•ด๋‹น ๋ฌธ์ž์—ด์€ prefix ์ด๋ฏ€๋กœ boolean false ๋กœ ์„ค์ •
// prefix๊ฐ€ ์•„๋‹Œ ์˜จ์ „ํ•œ ๋ฌธ์ž์—ด ์‚ฝ์ž…์€ true ๋กœ ์ €์žฅ

// search ์‹œ, Map์— ํ•ด๋‹น ๋‹จ์–ด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , boolean ๊ฐ’์ด true ์ธ์ง€ ํ™•์ธ
// startsWith๋Š” ๊ทธ๋ƒฅ Map ์— ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

// ๊ณต๊ฐ„ ๋ณต์žก๋„: Map ํฌ๊ธฐ -> O(N)
// ์‹œ๊ฐ„ ๋ณต์žก๋„: ์ „์ฒด ํ˜ธ์ถœ ์ˆ˜ * String ๊ธธ์ด -> O(N*M)
// ์ฐธ๊ณ ) ์ตœ๋Œ€ ์‹œ๊ฐ„ ๋ณต์žก๋„ : 2000 * 3*10^4 = 6*10^7
class Trie {

Map<String,Boolean> stringSet;
public Trie() {
stringSet = new HashMap<>();
}

public void insert(String word) {
for(int i=0;i<word.length();i++){
stringSet.putIfAbsent(word.substring(0, i), false);
}
stringSet.put(word, true);
}

public boolean search(String word) {
return stringSet.containsKey(word) && stringSet.get(word)==true;
}

public boolean startsWith(String prefix) {

return stringSet.containsKey(prefix);
}
}
36 changes: 36 additions & 0 deletions word-break/eunhwa99.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.List;


// ํ’€์ด
// dfs + memo
// memo[i] = true/false : i ์ธ๋ฑ์Šค์—์„œ ์‹œ์ž‘ํ•ด์„œ wordDict์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„๋ฌธ์ž์—ด ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋Š”์ง€ ์œ ๋ฌด
//์‹œ๊ฐ„ ๋ณต์žก๋„: O(n*n) = start~end(์žฌ๊ท€) + start~end(๋ฐ˜๋ณต๋ฌธ)
// ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n) - ์žฌ๊ท€ ๊นŠ์ด

class Solution {

public boolean wordBreak(String s, List<String> wordDict) {
return dfs(0, s.length(), s, wordDict, new Boolean[s.length()]);

}
public boolean dfs(int start, int len, String s, List<String> wordDict, Boolean[] memo){
if(start==len){
return true;
}

if(memo[start]!=null) return memo[start];

for(int end=start+1; end<=len;end++){
if(wordDict.contains(s.substring(start, end))){
if(dfs(end, len, s, wordDict, memo)){
memo[start] = true;
return true;
}
}
}

memo[start]=false;
return false;

}
}

0 comments on commit 7ae7503

Please sign in to comment.