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
- Method
- greedy
- java
- Matrix
- 코테
- database
- 파이썬
- simulation
- Counting
- string
- two pointers
- Class
- geometry
- implement
- Tree
- Binary Tree
- array
- bit manipulation
- sorting
- 코딩테스트
- Binary Search
- hash table
- dynamic programming
- SQL
- Stack
- Data Structure
- 자바
- Number Theory
- 구현
- Math
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2341. Maximum Number of Pairs in Array 본문
1. Input
1) int[] nums
2. Output
1) nums에 아래 연산을 최대한 많이 수행한 후, 없앤 쌍의 개수와 nums에 남은 숫자의 개수를 차례대로 담은 배열 answer를 반환
// 연산 방법
- nums에서 같은 숫자 2개를 고른다.
- 두 숫자 모두 num에서 없애고, 없앤 숫자는 쌍을 이룬다.
3. Constraint
1) 1 <= nums.length <= 100
2) 0 <= nums[i] <= 100
4. Example
Input: nums = [1,3,2,1,3,2,2] -> Output: [3,1]
설명:
- nums[0] == nums[3] == 1: nums=[3,2,3,2,2], answer=[1,_]
- nums[0] == nums[2] == 3: nums=[2,2,2], answer=[2,_]
- nums[0] == nums[1] == 2: nums=[2], answer=[3,_]
- 더이상 없앨 숫자가 없으므로 nums에 남은 숫자는 1개이므로 [3,1]을 반환한다.
5. Code
1) 첫 코드(2022/08/04)
int pair = 0;
for(int i=0 ; i<nums.length-1 ; i++){
for(int j=i+1 ; j<nums.length ; j++){
if(nums[i]!=-1 && nums[i]==nums[j]){
nums[i] = -1;
nums[j] = -1;
pair++;
break;
}
}
}
return new int[] {pair, nums.length - 2*pair};
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2351. First Letter to Appear Twice (0) | 2023.01.16 |
---|---|
[LeetCode/Easy] 2347. Best Poker Hand (0) | 2023.01.16 |
[LeetCode/Easy] 2319. Check if Matrix Is X-Matrix (0) | 2023.01.16 |
[LeetCode/Easy] 2315. Count Asterisks (0) | 2023.01.16 |
[LeetCode/Easy] 2299. Strong Password Checker II (0) | 2023.01.16 |