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
- geometry
- bit manipulation
- Data Structure
- string
- 파이썬
- SQL
- implement
- Counting
- 자바
- hash table
- Tree
- 코딩테스트
- Binary Tree
- Class
- Math
- Stack
- greedy
- 코테
- sorting
- two pointers
- array
- Number Theory
- dynamic programming
- 구현
- Method
- database
- simulation
- java
- Matrix
- Binary Search
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 1608. Special Array With X Elements Greater Than or Equal X 본문
코딩테스트 풀이/JAVA
[LeetCode/Easy] 1608. Special Array With X Elements Greater Than or Equal X
무지맘 2022. 12. 29. 12:131. Input
1) 음이 아닌 정수 배열 nums
2. Output
1) nums를 특별하게 만들어주는 숫자 x
2) nums가 특별하지 않다면 –1
// 배열이 특별하기 위한 조건
- 해당 배열에 x 이상인 수가 정확히 x개 있을 때
- x는 배열에 없는 숫자여도 상관 없다.
- x는 개수에 해당하는 숫자이므로 음수가 될 수 없다.
3. Constraint
1) 1 <= nums.length <= 100
2) 0 <= nums[i] <= 1000
4. Example
Input: nums = [3,5] -> Output: 2
Input: nums = [0,0] -> Output: -1
설명:
// 첫번째
- 3, 5는 2 이상인 수고, 2 이상인 수는 nums에 2개 있다.
// 두번째
- x=0이라면 0 이상인 수가 0개여야 하는데 맞지 않다.
- x=1이라면 1 이상인 수가 1개여야 하는데 맞지 않다.
- 어떤 x도 nums를 특별하게 할 수 없으므로 –1을 반환한다.
5. Code
1) 첫 코드(2022/08/01)
import java.util.*;
int x = nums.length;
Arrays.sort(nums);
int count = 0;
while(x>0){
for(int i=nums.length-1 ; i>=0 ; i--){
if(nums[i]>=x) count++;
else break;
}
if(x==count) break;
count = 0;
x--;
}
return x==0 ? -1 : x;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1624. Largest Substring Between Two Equal Characters (0) | 2022.12.29 |
---|---|
[LeetCode/Easy] 1619. Mean of Array After Removing Some Elements (0) | 2022.12.29 |
[LeetCode/Easy] 1614. Maximum Nesting Depth of the Parentheses (0) | 2022.12.29 |
[프로그래머스/Lv.1] 과일 장수 (0) | 2022.12.28 |
[프로그래머스/Lv.0] k의 개수 (0) | 2022.12.27 |