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
- simulation
- Data Structure
- two pointers
- implement
- Binary Search
- geometry
- Matrix
- 코딩테스트
- array
- Binary Tree
- 코테
- hash table
- SQL
- dynamic programming
- 구현
- Tree
- 자바
- Stack
- Number Theory
- Class
- Math
- Method
- java
- database
- greedy
- 파이썬
- sorting
- Counting
- bit manipulation
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Medium] 532. K-diff Pairs in an Array 본문
1. Input
1) int[] nums
2) int k
2. Output
1) 다음 조건을 만족하는 쌍의 개수
- nums[i]와 nums[j]의 차가 k
- i != j
- 중복되는 쌍은 세지 앉는다. 예를 들어 (1, 2)와 (2, 1)은 같은 것으로 취급한다.
3. Constraint
1) 1 <= nums.length <= 10^4
2) - 10^7 <= nums[i] <= 10^7
3) 0 <= k <= 10^7
4. Example
Input: nums = [3,1,4,1,5], k = 2 -> Output: 2
설명:
- 차가 2인 쌍은 (1, 3)과 (3, 5) 2가지뿐이다.
- 1이 2개이기 때문에 차가 2인 쌍은 (1, 3), (1, 3), (3, 5)의 3개지만, 중복되는 쌍은 세지 않는다.
5. Code
1) 첫 코드(2023/03/02)
HashSet<String> s = new HashSet<String>();
for(int i=0 ; i<nums.length-1 ; i++){
for(int j=i+1 ; j<nums.length ; j++)
if(Math.abs(nums[i]-nums[j])==k)
s.add(Math.min(nums[i],nums[j]) + " " + Math.max(nums[i],nums[j]));
return s.size();
- 그래 난 아직 성능을 따질 때가 아니지.....
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2558. Take Gifts From the Richest Pile (0) | 2023.03.02 |
---|---|
[LeetCode/Easy] 2562. Find the Array Concatenation Value (0) | 2023.03.02 |
[LeetCode/Easy] 2566. Maximum Difference by Remapping a Digit (0) | 2023.03.01 |
[LeetCode/Medium] 503. Next Greater Element II (0) | 2023.02.27 |
[LeetCode/Easy] 2574. Left and Right Sum Differences (0) | 2023.02.27 |