-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
YoungSeok-Choi
wants to merge
3
commits into
DaleStudy:main
Choose a base branch
from
YoungSeok-Choi:feature/week-6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stack에 쌓아야 하는 조건들을 먼저 검사해서 for-loop 횟수를 최소화 하는 접근이 인상적이네요~~!