Skip to content

[YoungSeok-Choi] week 6 solutions #1409

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 3 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
63 changes: 63 additions & 0 deletions container-with-most-water/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// tc: O(n)
// 투 포인터라는 문제풀이 방법으로 간단히 (답지보고) 해결...
class Solution {
public int maxArea(int[] h) {
int start = 0;
int end = h.length - 1;
int mx = -987654321;

while(start < end) {
mx = Math.max(mx, (end - start) * Math.min(h[start], h[end]));

if(h[start] < h[end]) {
start++;
} else {
end--;
}
}

return mx;
}
}


// 예외처리가 덕지덕지 붙기 시작할 때. (잘못됨을 깨닫고)다른 방법으로 풀어햐 하는건 아닌지 생각하는 습관 필요함..ㅠ
class WrongSolution {
public int maxArea(int[] h) {
int mx = Math.min(h[0], h[1]);
int idx = 0;

for(int i = 1; i < h.length; i++) {
int offset = i - idx;

int prevCalc = Math.min(h[i - 1], h[i]);
int calc = Math.min(h[idx], h[i]);
int newMx = calc * offset;

// 새롭게 인덱스를 바꿔버리는게 더 나을때.
if(prevCalc > newMx) {
idx = i - 1;
mx = Math.max(mx, prevCalc);
continue;
}

// 물을 더 많이 담을 수 있을 때.
if(h[idx] < h[i] && newMx > mx) {
if(i == 2) {
int exc = Math.min(h[1], h[i]) * offset;
if(exc > newMx) {
idx = 1;
mx = Math.max(exc, mx);
continue;
}
}

idx = i;
}

mx = Math.max(newMx, mx);
}

return mx;
}
}
69 changes: 69 additions & 0 deletions longest-increasing-subsequence/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// tc: O(n)
// idea: max largest subseq length for "N" th array index
class Solution {

public int lengthOfLIS(int[] nums) {

int[] arr = new int[nums.length];
Arrays.fill(arr, 1);

for(int i = 1; i < nums.length; i++) {
for(int j = 0; j < i; j++) {
if(nums[j] < nums[i]) {
arr[i] = Math.max(arr[i], arr[j] + 1);
}
}
}

int mx = -987654321;
for(int anInt : arr) {
System.out.print(anInt + " ");
mx = Math.max(mx, anInt);
}

return mx;
}
}

// time limit exceed

class WrongSolution {

public boolean[] visit;
public int mxVal = -987654321;
public int mx = 1;
public Map<Integer, Boolean> m = new HashMap<>();

public int lengthOfLIS(int[] nums) {
for(int i = 0; i < nums.length; i++) {
m = new HashMap<>();
mxVal = Math.max(mxVal, nums[i]);
m.put(nums[i], true);
dfs(i, nums);
mxVal = -987654321;
}

return mx;
}

public void dfs(int idx, int[] nums) {
mx = Math.max(mx, m.size());

for(int i = idx + 1; i < nums.length; i++) {
if(mxVal < nums[i]) {
int prev = mxVal;
mxVal = nums[i];
m.put(nums[i], true);

dfs(i, nums);

mxVal = prev;
m.remove(nums[i]);
}
}
}
}
33 changes: 33 additions & 0 deletions valid-parentheses/YoungSeok-Choi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Stack;

// TC: O(n)
class Solution {
Stack<Character> st = new Stack<>();
public boolean isValid(String s) {
for(char c : s.toCharArray()) {
if(st.empty() || !isClosing(c)) {
st.push(c);
continue;
}
Comment on lines +8 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stack에 쌓아야 하는 조건들을 먼저 검사해서 for-loop 횟수를 최소화 하는 접근이 인상적이네요~~!


char temp = st.peek();
if(temp == '(' && c == ')') {
st.pop();
} else if(temp == '[' && c == ']') {
st.pop();
} else if(temp == '{' && c == '}') {
st.pop();
} else {
st.push(c);
}
}

return st.empty();
}

public boolean isClosing(char c) {
if(c == ')' || c == ']' || c == '}') return true;

return false;
}
}