Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- Data Structure
- bit manipulation
- implement
- Stack
- Class
- sorting
- Binary Search
- Binary Tree
- simulation
- hash table
- geometry
- greedy
- SQL
- two pointers
- 구현
- database
- 코테
- 자바
- Tree
- 파이썬
- Method
- string
- array
- Math
- Matrix
- Counting
- Number Theory
- 코딩테스트
- dynamic programming
- java
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2582. Pass the Pillow 본문
1. Input
1) int n
2) int time
2. Output
1) 다음을 time번 시행한 후 베개를 갖고 있는 사람의 번호를 반환
- 1번부터 n번까지 일렬로 서있고, 처음에는 1번이 베개를 갖고 있다.
- 1초에 1칸 옆 사람에게 베개를 전달해준다. (1번이 2번에게, 2번이 3번에, ...)
- n번이 베개를 넘길 차례가 되면 n-1번에게 전달한다. 즉, 전달 방향이 반대로 바뀐다.
3. Constraint
1) 2 <= n <= 1000
2) 1 <= time <= 1000
4. Example
Input: n = 4, time = 5 -> Output: 2
설명: (초기)1 -> 2 -> 3 -> 4 -> 3 -> 2
5. Code
1) 첫 코드(2023/05/06)
class Solution {
public int passThePillow(int n, int time) {
int answer = 1;
boolean right = true;
for(int i=0 ; i<time ; i++){
if(right){
if(answer==n){
right = false; answer--;
} else
answer++;
} else{
if(answer==1){
right = true; answer++;
} else
answer--;
}
}
return answer;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2614. Prime In Diagonal (0) | 2023.05.08 |
---|---|
[LeetCode/Easy] 2605. Form Smallest Number From Two Digit Arrays (0) | 2023.05.06 |
[LeetCode/Easy] 2570. Merge Two 2D Arrays by Summing Values (0) | 2023.05.06 |
[LeetCode/Easy] 2549. Count Distinct Numbers on Board (0) | 2023.05.06 |
[LeetCode/Easy] 2540. Minimum Common Value (0) | 2023.05.06 |