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
- string
- 구현
- Data Structure
- sorting
- Math
- 코딩테스트
- Stack
- Number Theory
- simulation
- Counting
- bit manipulation
- Binary Search
- implement
- two pointers
- database
- dynamic programming
- Method
- 파이썬
- greedy
- 코테
- geometry
- hash table
- array
- SQL
- Binary Tree
- Matrix
- Class
- 자바
- Tree
- java
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 747. Largest Number At Least Twice of Others 본문
1. Input
1) int 배열 nums
2. Output
1) nums의 수 중 가장 큰 수의 인덱스 반환
2) 이때 가장 큰 수는 다른 수들보다 최소 2배 이상은 되어야 한다.
3) 그런 수가 없다면 –1 반환
3. Constraint
1) 2 <= nums.length <= 50
2) 0 <= nums[i] <= 100
3) 가장 큰 수는 1개뿐이다.
4. Example
Input: nums={3,6,1,0} -> Output: 1
Input: nums={1,2,3,4} -> Output: -1
설명:
- 6이 가장 큰 수이고, 남은 3, 1, 0보다 2배 이상이기 때문에 6의 위치인 1을 반환한다.
- 4가 가장 큰 수이지만, 3의 2배 이상이 안되기 때문에 –1을 반환한다.
5. Code
1) 첫 코드(2022/07/19)
int max = 0;
int index = 0;
for(int i=0 ; i<nums.length ; i++)
if(max<nums[i]){
max = nums[i];
index = i;
}
for(int i=0 ; i<nums.length ; i++)
if(max<nums[i]*2 && i!=index)
return -1;
return index;
2) 다시 풀어본 코드(2022/12/07)
import java.util.*;
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0 ; i<nums.length ; i++)
list.add(nums[i]);
Arrays.sort(nums);
int answer = list.indexOf(nums[nums.length-1]);
if(nums[nums.length-1]<nums[nums.length-2]*2)
answer = -1;
return answer;
- 1번이 모든 면에서 더 낫다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 771. Jewels and Stones (0) | 2022.12.07 |
---|---|
[LeetCode/Easy] 762. Prime Number of Set Bits in Binary Representation (0) | 2022.12.07 |
[LeetCode/Easy] 744. Find Smallest Letter Greater Than Target (0) | 2022.12.06 |
[LeetCode/Easy] 728. Self Dividing Numbers (0) | 2022.12.06 |
[LeetCode/Easy] 724. Find Pivot Index (0) | 2022.12.06 |