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
- Matrix
- implement
- database
- Number Theory
- Tree
- 코딩테스트
- 자바
- Counting
- 파이썬
- Method
- Data Structure
- Class
- dynamic programming
- Binary Tree
- 코테
- simulation
- java
- Binary Search
- sorting
- string
- Math
- Stack
- SQL
- geometry
- 구현
- bit manipulation
- array
- hash table
- two pointers
- greedy
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1299. Replace Elements with Greatest Element on Right Side 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1299. Replace Elements with Greatest Element on Right Side
무지맘 2022. 12. 24. 01:351. Input
1) 정수 배열 arr
2. Output
1) 자기보다 오른쪽에 있는 요소들 중 가장 큰 수로 치환한 결과 배열
2) 가장 마지막 요소는 자기보다 오른쪽에 있는 요소가 없으므로 –1로 치환한다.
3. Constraint
1) 1 <= arr.length <= 10^4
2) 1 <= arr[i] <= 10^5
4. Example
Input: arr = [17,18,5,4,6,1] -> Output: [18,6,6,6,1,-1]
설명:
- index 0 -> 가장 큰 수는 index 1 (18)
- index 1 -> 가장 큰 수는 index 4 (6)
- index 2 -> 가장 큰 수는 index 4 (6)
- index 3 -> 가장 큰 수는 index 4 (6).
- index 4 -> 가장 큰 수는 index 5 (1)
- index 5 -> 가장 마지막 요소이므로 -1
5. Code
1) 첫 코드(2022/06/16)
if(arr.length == 1)
return new int[] {-1};
for(int i=0 ; i<arr.length-1 ; i++){
int max = 0;
int max_index = 0;
for(int j=i+1 ; j<arr.length ; j++)
if(arr[j] > max){
max = arr[j];
max_index = j;
}
for(int k=i ; k<max_index ; k++)
arr[k] = max;
i = max_index -1;
}
arr[arr.length-1] = -1;
return arr;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1323. Maximum 69 Number (0) | 2022.12.24 |
---|---|
[LeetCode/Easy] 1313. Decompress Run-Length Encoded List (0) | 2022.12.24 |
[LeetCode/Easy] 1295. Find Numbers with Even Number of Digits (0) | 2022.12.24 |
[LeetCode/Easy] 1290. Convert Binary Number in a Linked List to Integer (0) | 2022.12.24 |
[LeetCode/Easy] 1281. Subtract the Product and Sum of Digits of an Integer (0) | 2022.12.24 |