-
Notifications
You must be signed in to change notification settings - Fork 16
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
6월 4주차 #42
Open
SangeunLEE801
wants to merge
2
commits into
master
Choose a base branch
from
lee
base: master
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.
Open
6월 4주차 #42
Changes from all commits
Commits
Show all changes
2 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 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,58 @@ | ||
# Implement Queue using Stacks | ||
|
||
https://leetcode.com/problems/implement-queue-using-stacks/ | ||
|
||
## 문제 접근 방법 | ||
|
||
스택 두 개를 이용 | ||
|
||
- 스택1은 push만, 스택2는 pop과 peek할 때만 사용함 | ||
|
||
## 코드 구현 | ||
|
||
```java | ||
import java.util.Stack; | ||
|
||
class MyQueue { | ||
static Stack<Integer> s1; | ||
static Stack<Integer> s2; | ||
|
||
/** Initialize your data structure here. */ | ||
public MyQueue() { | ||
s1 = new Stack<>(); | ||
s2 = new Stack<>(); | ||
} | ||
|
||
/** Push element x to the back of queue. */ | ||
public void push(int x) { | ||
s1.push(x); | ||
} | ||
|
||
/** Removes the element from in front of queue and returns that element. */ | ||
public int pop() { | ||
if (s2.isEmpty()) { | ||
while (!s1.isEmpty()){ | ||
s2.push(s1.pop()); | ||
} | ||
} | ||
return s2.pop(); | ||
} | ||
|
||
/** Get the front element. */ | ||
public int peek() { | ||
if (s2.isEmpty()) { | ||
while (!s1.isEmpty()){ | ||
s2.push(s1.pop()); | ||
} | ||
} | ||
return s2.peek(); | ||
|
||
} | ||
|
||
/** Returns whether the queue is empty. */ | ||
public boolean empty() { | ||
return s1.isEmpty() && s2.isEmpty(); | ||
} | ||
} | ||
|
||
``` |
This file contains 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,52 @@ | ||
# 연속합 | ||
|
||
https://www.acmicpc.net/problem/1912 | ||
|
||
## 문제 접근 방법 | ||
|
||
- dp인건 알고있었지만 어떻게 푸는지 몰라서 해설 찾아봄 | ||
- 핵심은 i-1번째까지의 합(dp[i-1])에 현재 값(arr[i])을 더한 것과, 그냥 현재 값 중 누가 더 큰 지 비교하는것 | ||
|
||
### 코드 | ||
|
||
```java | ||
import java.util.Scanner; | ||
|
||
public class Baekjoon1912 { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int[] arr = new int[n]; | ||
for (int i=0;i<n;i++){ | ||
arr[i]=scanner.nextInt(); | ||
} | ||
|
||
System.out.println(solution(n, arr)); | ||
|
||
} | ||
|
||
private static int solution(int n, int[] arr) { | ||
int[] dp = new int[n]; | ||
dp[0]=arr[0]; | ||
for (int i=1;i<n;i++){ | ||
dp[i]=Math.max(dp[i-1]+arr[i], arr[i]); | ||
} | ||
|
||
int max=Integer.MIN_VALUE; | ||
for (int i:dp){ | ||
if (max<i) max=i; | ||
} | ||
Comment on lines
+35
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 푼제풀이 확인 했습니다:) 고생하셨어요! 프로그램의 첫번째 코드에 max 연산을 넣으면 최대값을 찾는 반복문을 수행하지 않을 수 있어요. 이미 알고 계실거라 생각하지만...ㅎㅎ 추가적으로 이 코드에서 |
||
return max; | ||
} | ||
|
||
|
||
} | ||
``` | ||
|
||
### Time complexity | ||
|
||
O(n) | ||
|
||
### Space complexity | ||
|
||
O(n) |
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.
영어문제를 풀어야 하는 일이 많으신가봐요! 저도 이 글 보고 링크 문제 확인해봤는데요.
전 스택1과 배열을 생성해서 풀려고 했네요ㅋㅋ
pop 메서드 호출 시
이 풀이는 배열해제를 시키지 않으면 가비지 데이터가 많이 생길 것 같고 매 pop마다 배열을 생성하는 번거로움이 있었어요. lee님이 푸신 스택 두개 이용하는게 가장 효율적인 것 같네요!