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 | 31 |
Tags
- 구현
- Number Theory
- 자바
- Stack
- hash table
- Method
- SQL
- Math
- 코딩테스트
- dynamic programming
- 코테
- Class
- bit manipulation
- database
- java
- Matrix
- greedy
- geometry
- two pointers
- Binary Tree
- sorting
- Tree
- string
- array
- Counting
- Data Structure
- implement
- Binary Search
- 파이썬
- simulation
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1184. Distance Between Bus Stops 본문
1. Input
1) int[] distance
- n == distance.length
2) int start
3) int destination
2. Output
1) start에서 destination까지 가는 최단 경로를 반환
- 버스정류장은 0번부터 n-1번까지 있다.
- 시계방향으로 가거나 반시계방향으로 갈 수 있다.
- distance[i] == i번 버스정류장과 (i+1)%n번 버스정류장 사이의 거리
3. Constraint
1) 1 <= n <= 10^4
2) 0 <= start, destination < n
3) 0 <= distance[i] <= 10^4
4. Example
Input: distance = [1,2,3,4], start = 0, destination = 2 -> Output: 3
설명:
- 시계방향: 1 + 2 = 3
- 반시계방향: 4 + 3 = 7
- 따라서 3을 반환한다.
5. Code
1) 첫 코드(2023/04/05)
int total = 0, sum = 0, a = Math.min(start,destination), b = Math.max(start,destination);
for(int i=0 ; i<distance.length ; i++){
total += distance[i];
if(a<=i && i<b)
sum += distance[i];
}
return Math.min(total-sum, sum);
- 오랜만에 괜찮은 성능이 나왔다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1128. Number of Equivalent Domino Pairs (0) | 2023.04.06 |
---|---|
[LeetCode/Easy] 1160. Find Words That Can Be Formed by Characters (0) | 2023.04.06 |
[LeetCode/Easy] 1047. Remove All Adjacent Duplicates In String (0) | 2023.04.04 |
[LeetCode/Easy] 1037. Valid Boomerang (0) | 2023.04.04 |
[백준 온라인 저지] 9506. 약수들의 합 (0) | 2023.04.04 |