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
47 changes: 47 additions & 0 deletions container-with-most-water/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
브루트포스를 통해 모든 경우의 수를 연산하여 찾는 방식
배열 height의 길이 -> N
시간 복잡도 : O(N^2) -> 시간 초과
공간 복잡도 : O(1)
*/
class FailSolution {
public int maxArea(int[] height) {
int max=0;
int area=0;
for(int i=0;i<height.length;i++){
for(int j=i+1;j<height.length;j++){
area= Math.max(area,Math.min(height[j],height[i])*(j-i));
if(area>max){
max=area;
}
}
}
return max;
}
}

/**
투포인터를 활용하여, 낮은 높이의 포인터를 갱신하면서 최대 넓이를 구하는 방식
배열 height의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(1)
*/
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int max = 0;
while(left < right) {
int h = Math.min(height[right], height[left]);
int w = right - left;
max = Math.max(max, w * h);

if(h == height[right]) {
right--;
} else {
left++;
}
}
return max;
}
}
116 changes: 116 additions & 0 deletions design-add-and-search-words-data-structure/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
Tries + BFS를 활용한 방식
addWord()
문자열 word의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(N)
search()
Tries 내부의 노드 개수 -> M
시간 복잡도 : O(M)
공간 복잡도 : O(M)
*/
class WordDictionary {
public Map<Character, WordNode> wordMap;

public WordDictionary() {
wordMap = new HashMap<>();
}

public void addWord(String word) {
WordNode wordNode = null;
char ch = word.charAt(0);
wordNode = wordMap.get(ch);

if(wordNode == null) {
boolean isFirstWord = word.length() == 1;
wordNode = new WordNode(ch, isFirstWord);
wordMap.put(ch, wordNode);
}

for(int idx = 1; idx < word.length(); idx++) {
char target = word.charAt(idx);
boolean isLeaf = word.length() - 1 == idx;
wordNode = wordNode.next.computeIfAbsent(target, key -> new WordNode(key, isLeaf));
}
wordNode.isLeaf = true;


}

public boolean search(String word) {
Queue<Node> que = new ArrayDeque<>();
boolean result = false;
char ch = word.charAt(0);
WordNode wordNode = wordMap.get(ch);
int len = word.length();

if(ch == '.' && wordMap.size() != 0) {
for(Map.Entry<Character, WordNode> entry : wordMap.entrySet()) {
que.add(new Node(entry.getValue(), 1));
}
}

if (wordNode != null) {
que.add(new Node(wordNode, 1));
}



while(!que.isEmpty()) {
Node node = que.poll();
if(node.idx == len && node.wordNode.isLeaf) {
result = true;
break;
}

if(node.idx == len) {
continue;
}

char target = word.charAt(node.idx);

if(target == '.' && node.wordNode.next.size() != 0) {
for(Map.Entry<Character, WordNode> entry : node.wordNode.next.entrySet()) {
que.add(new Node(entry.getValue(), node.idx + 1));
}
continue;
}


if (!node.wordNode.next.containsKey(target)) {
continue;
}

que.add(new Node(node.wordNode.next.get(target), node.idx + 1));
}

return result;

}
}

class Node {
WordNode wordNode;
int idx;
public Node(WordNode wordNode, int idx) {
this.wordNode = wordNode;
this.idx = idx;
}
}

class WordNode {
char ch;
Map<Character, WordNode> next;
boolean isLeaf;

public WordNode(char ch) {
this(ch, false);
}

public WordNode(char ch, boolean isLeaf) {
next = new HashMap<>();
this.ch = ch;
this.isLeaf = isLeaf;
}

}
59 changes: 59 additions & 0 deletions longest-increasing-subsequence/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
dp를 활용한 방식
nums의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(N)
*/
class Solution {
public int lengthOfLIS(int[] nums) {
int[] dp =new int[nums.length];
for(int i = 0; i < dp.length; i++) {
for(int j = 0; j < i; j++) {
if(nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
return Arrays.stream(dp)
.max()
.getAsInt() + 1;
}
}

/**
이분탐색 활용한 방식
nums의 길이 -> N
시간 복잡도 : O(NlogN)
공간 복잡도 : O(N)
*/
class Solution {
public int[] list;
public int lengthOfLIS(int[] nums) {
int result = 0;
list =new int[nums.length];
Arrays.fill(list, Integer.MAX_VALUE);
for(int i = 0; i < list.length; i++) {
int idx = binarySearch(list, nums[i]);
list[idx] = nums[i];
result = Math.max(result, idx + 1);

}
return result;
}

public int binarySearch(int[] list, int target) {
int start = 0;
int end = list.length - 1;

while(start <= end) {
int mid = (start + end) / 2;
if(list[mid] >= target) {
end = mid - 1;
} else {
start = mid + 1;
}
}

return start;
}
}
55 changes: 55 additions & 0 deletions spiral-matrix/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
구현을 통해 배열을 읽어들이는 방식
시간 복잡도 : O(N*M)
공간 복잡도 : O(N*M)
*/
class Solution {
int[] moveY = {1, 0, -1, 0};
int[] moveX = {0, -1, 0, 1};
int N;
int M;
public List<Integer> spiralOrder(int[][] matrix) {
N = matrix.length;
M = matrix[0].length;
boolean[][] visited = new boolean[N][M];
List<Integer> result = new ArrayList<>();
int curX = 0;
int curY = 0;
int direction = 0;
int count = 0;
visited[curX][curY] = true;
result.add(matrix[curX][curY]);

while(true) {
if(count == 4) {
break;
}
int tempX = curX + moveX[direction];
int tempY = curY + moveY[direction];
if(outOfIndex(tempX, tempY)) {
direction = (direction + 1) % 4;
count++;
continue;
}

if(visited[tempX][tempY]) {
direction = (direction + 1) % 4;
count++;
continue;
}

curX = tempX;
curY = tempY;
result.add(matrix[curX][curY]);
visited[curX][curY] = true;

count = 0;
}

return result;
}

public boolean outOfIndex(int x, int y) {
return x < 0 || x >= N || y < 0 || y >= M;
}
}
37 changes: 37 additions & 0 deletions valid-parentheses/se6816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
Stack을 활용한 방식
문자열 s의 길이 -> N
시간 복잡도 : O(N)
공간 복잡도 : O(N)
*/
class Solution {
public boolean isValid(String s) {
boolean result= true;
Stack<Character> sta=new Stack<>();
Map<Character,Character> map=new HashMap<>();
map.put('}','{');
map.put(']','[');
map.put(')','(');
for(int i=0; i<s.length();i++){
char ch=s.charAt(i);
if(ch == '(' || ch == '[' || ch == '{'){
sta.push(ch);
continue;
}
char target=map.get(ch);
if(sta.isEmpty()){
result=false;
break;
}
if(!(sta.peek() == target)){
result=false;
break;
}
sta.pop();
}
if(!sta.isEmpty()){
result=false;
}
return result;
}
}