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
- greedy
- hash table
- Math
- Method
- SQL
- 코테
- geometry
- sorting
- two pointers
- Binary Tree
- 코딩테스트
- Binary Search
- Counting
- Number Theory
- 파이썬
- Matrix
- Stack
- java
- database
- Tree
- 구현
- Data Structure
- implement
- 자바
- string
- dynamic programming
- simulation
- array
- Class
- bit manipulation
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Medium] 16. 3Sum Closest 본문
1. Input
1) 정수 배열 nums
2) 정수 target
2. Output
1) nums의 세 수의 합이 target에 가장 가까울 때, 그 합을 반환
3. Constraint
1) 3 <= nums.length <= 500
2) -1000 <= nums[i] <= 1000
3) - 10^4 <= target <= 10^4
4. Example
Input: nums = [-1,2,1,-4], target = 1 -> Output: 2
설명: nums의 요소 중 세 수의 합으로 나오는 것은 [2, -4, -1]이고, target인 1과의 차는 각각 [1, 5, 2]이므로 차가 가장 작은 2를 반환한다.
5. Code
1) 첫 코드(2023/01/03)
int answer = nums[0]+nums[1]+nums[2];
for(int i=0 ; i<nums.length-2 ; i++){
for(int j=i+1 ; j<nums.length-1 ; j++){
for(int k=j+1 ; k<nums.length ; k++){
if(Math.abs(nums[i]+nums[j]+nums[k]-target)<Math.abs(answer-target))
answer = nums[i]+nums[j]+nums[k];
} // k
} // j
} // i
return answer;
- 실행시간이 너무 길다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 1773. Count Items Matching a Rule (0) | 2023.01.04 |
---|---|
[LeetCode/Easy] 1768. Merge Strings Alternately (0) | 2023.01.04 |
[LeetCode/Easy] 1742. Maximum Number of Balls in a Box (0) | 2023.01.03 |
[LeetCode/Easy] 1732. Find the Highest Altitude (0) | 2023.01.02 |
[LeetCode/Easy] 1725. Number Of Rectangles That Can Form The Largest Square (0) | 2023.01.02 |