Skip to content

Commit

Permalink
Merge pull request #466 from jaejeong1/main
Browse files Browse the repository at this point in the history
[jaejeong1] Week 6
  • Loading branch information
jaejeong1 authored Sep 21, 2024
2 parents 879ecd7 + 601ed62 commit 5e24497
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
31 changes: 31 additions & 0 deletions container-with-most-water/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class SolutionMaxArea {
public int maxArea(int[] height) {
// A์™€ B ๊ฐ„ ๋ฉด์  = (A์™€ B์˜ X์ถ• ์ฐจ์ด) * (A์™€ B Y์ถ• ์ค‘ ๋” ์ž‘์€ Y์ถ• ๊ธธ์ด)
// 1๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•: ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์— ๋Œ€ํ•ด ์œ„ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค, ์‹œ๊ฐ„๋ณต์žก๋„: O(N^2) / ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
// (์ตœ์ข…) 2๋ฒˆ์งธ ํ’€์ด ๋ฐฉ๋ฒ•: ์–‘์ชฝ ๋์—์„œ ํˆฌํฌ์ธํ„ฐ๋กœ ์ขํ˜€์˜ค๋ฉด์„œ ๋ฉด์ ์„ ๊ตฌํ•œ๋‹ค, ์‹œ๊ฐ„๋ณต์žก๋„: O(N) / ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
// ์ „์ฒด ๋„ˆ๋น„๋ฅผ ๊ณ ๋ คํ•˜๋ฉด์„œ ์›€์ง์—ฌ์•ผํ•œ๋‹ค.
// ๋งค๋ฒˆ ๋ฉด์ ์„ ๊ณ„์‚ฐํ•˜๊ณ , ๋” ํฌ๋ฉด ์ €์žฅํ•œ๋‹ค.
// lt์™€ rt ์ค‘ ๋” ๋‚ฎ์€์ชฝ์„ ์ขํžŒ๋‹ค

var lt = 0;
var rt = height.length-1;
var maxArea = 0;

while(lt < rt) {
var x = rt - lt;
var y = Math.min(height[lt], height[rt]);
var currentArea = x * y;
if (maxArea < currentArea) {
maxArea = currentArea;
}

if (height[lt] < height[rt]) {
lt++;
} else {
rt--;
}
}

return maxArea;
}
}
63 changes: 63 additions & 0 deletions design-add-and-search-words-data-structure/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import java.util.HashMap;
import java.util.Map;

class WordDictionary {
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), N(word์˜ ๊ธธ์ด)
// ๊ณต๊ฐ„๋ณต์žก๋„: O(N)
Map<Character, WordDictionary> child;
boolean isEnd;

public WordDictionary() {
child = new HashMap<>();
isEnd = false;
}

public void addWord(String word) {
var node = this;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);

node.child.putIfAbsent(c, new WordDictionary());
node = node.child.get(c);

if (i == word.length() - 1) {
node.isEnd = true;
return;
}
}
}

public boolean search(String word) {
return searchHelper(word, 0, this);
}

private boolean searchHelper(String word, int index, WordDictionary node) {
if (index == word.length()) {
return node.isEnd;
}

char c = word.charAt(index);
// . ์ด ๋‚˜์˜ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด ๋ชจ๋‘ ์žฌ๊ท€๋ฅผ ๋Œ๋ฆฐ๋‹ค
if (c == '.') {
for (WordDictionary childNode : node.child.values()) {
if (searchHelper(word, index + 1, childNode)) {
return true;
}
}
return false;
} else {
var childNode = node.child.get(c);
if (childNode == null) {
return false;
}
return searchHelper(word, index + 1, childNode);
}
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
45 changes: 45 additions & 0 deletions valid-parentheses/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.HashMap;
import java.util.Stack;
import java.util.Map;

class SolutionValidParentheses {

public boolean isValid(String s) {
// last in first out ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋ฏ€๋กœ ์Šคํƒ ์‚ฌ์šฉ
// ์—ฌ๋Š” ๋ฌธ์ž๋ฉด put
// ๋‹ซ๋Š” ๋ฌธ์ž๋ฉด pop
// ์ด ๋•Œ pop ๋Œ€์ƒ์ด ์˜ฌ๋ฐ”๋ฅธ ์ง์ธ์ง€ ํ™•์ธํ•œ๋‹ค. ์•„๋‹ˆ๋ฉด false ๋ฐ˜ํ™˜
// ์ง ํ™•์ธ ์‹œ O(1)๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด Map์„ ์‚ฌ์šฉ, ์—ฌ๋Š” ๋ฌธ์ž์™€ ๋‹ซ๋Š” ๋ฌธ์ž๋ฅผ key:value๋กœ ๋งคํ•‘
// ๋๊นŒ์ง€ ๋Œ์•˜๊ณ , ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด return true, ์•„๋‹ˆ๋ฉด false ๋ฐ˜ํ™˜
// ์‹œ๊ฐ„๋ณต์žก๋„: O(N), ๊ณต๊ฐ„๋ณต์žก๋„: O(N)

Stack<Character> stack = new Stack<>();

Map<Character, Character> matchedMap = new HashMap<>();
matchedMap.put('(', ')');
matchedMap.put('{', '}');
matchedMap.put('[', ']');

for (int i=0; i<s.length(); i++) {
var currentChar = s.charAt(i);

// ์—ฌ๋Š” ๋ฌธ์ž
if (matchedMap.containsKey(currentChar)) {
stack.push(currentChar);
} else { // ๋‹ซ๋Š” ๋ฌธ์ž
if (stack.isEmpty()) { // ์—ฌ๋Š” ๋ฌธ์ž๊ฐ€ ์—†์„ ๊ฒฝ์šฐ false
return false;
}

var prevChar = stack.peek();
if (matchedMap.get(prevChar).equals(currentChar)) {
stack.pop();
} else { // ๋‹ซ๋Š” ๋ฌธ์ž์™€ ์—ฌ๋Š” ๋ฌธ์ž ์ง์ด ์•ˆ๋งž์„ ๊ฒฝ์šฐ false
return false;
}
}
}

return stack.isEmpty(); // ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด ๋ชจ๋“  ์ง ๋งค์นญ์ด ์™„๋ฃŒ๋œ ๊ฒƒ์œผ๋กœ true ๋ฐ˜ํ™˜
}
}

0 comments on commit 5e24497

Please sign in to comment.