๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/LeetCode

[LeetCode] 016. 3Sum Closest JAVA

by Bhinney 2023. 1. 5.
 

3Sum Closest - LeetCode

3Sum Closest - Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.   Examp

leetcode.com


๐Ÿ“ ๋ฌธ์ œ

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
๊ธธ์ด๊ฐ€ n์ธ ์ •์ˆ˜ ๋ฐฐ์—ด nums์™€ ๋ชฉํ‘œ๊ฐ€ ์ •์ˆ˜์ธ ๊ฒฝ์šฐ ํ•ฉ๊ณ„๊ฐ€ ๋ชฉํ‘œ์— ๊ฐ€์žฅ ๊ทผ์ ‘ํ•˜๋„๋ก nums์—์„œ ์„ธ ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ฐพ์œผ์‹œ์˜ค.
์„ธ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜์‹œ์˜ค.

๋ถ€์—ฐ ์„ค๋ช… : ๊ธธ์ด๊ฐ€ n์ธ ์ •์ˆ˜์˜ ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ๊ทธ ์•ˆ์—์„œ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

ํ˜„์žฌ ์ธ๋ฑ์Šค์˜ ๊ฐ’๊ณผ ์ธ์ ‘ํ•œ ์–‘ ์˜†์˜ ๊ฐ’์„ ๋” ํ•˜๊ณ , ๋ชฉํ‘œ์— ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.


๐Ÿ“ ์˜ˆ์‹œ

Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Input: nums = [0,0,0], target = 1
Output: 0
Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0).

๐Ÿ“ ํ’€์ด ์„ค๋ช…

  • ๊ณ„์‚ฐ์— ์šฉ์ดํ•˜๋„๋ก ๋ฐฐ์—ด์„ ์ •๋ ฌํ•ด์ฃผ์—ˆ๋‹ค.
  • ๋น„๊ต๋ฅผ ์œ„ํ•ด 0,1,2 ๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
  • ์„ธ ๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋ณต๋ฌธ์— ์กฐ๊ฑด์‹์€ i๊ฐ€ ๋ฐฐ์—ด์˜ ๊ธธ์ด - 2 ๋ณด๋‹ค ์ž‘์„ ๋•Œ๋กœ ํ•ด์ฃผ์—ˆ๋‹ค.

 

public static int threeSumClosest(int[] nums, int target) {

   Arrays.sort(nums);
   int result = nums[0] + nums[1] + nums[2];

   for (int i = 0; i < nums.length - 2; i++) {

   }

   return result;
}

  • ์„ธ ์ˆ˜์˜ ํ•ฉ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‚˜๋จธ์ง€ ๋‘ ์ˆ˜์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•ด์ฃผ๊ณ  ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.
  • ๋‹ค์‹œ ๋ฐ˜๋ณต๋ฌธ์„ ์—ด๊ณ , ์„ธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ๋”ํ•œ ๊ฐ’์ด ๋งŒ์•ฝ target๊ณผ ๊ฐ™์œผ๋ฉด ํ•ด๋‹น ๊ฐ’์„ ๋ฆฌํ„ด์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
  • ๋งŒ์•ฝ ๋”ํ•œ ๊ฐ’์ด target๋ณด๋‹ค ํฌ๋ฉด ๋’ค์—์„œ๋ถ€ํ„ฐ ์„ธ๊ณ  ์žˆ๋Š” right์˜ ๊ฐ’์„ ๋งˆ์ด๋„ˆ์Šค ์‹œ์ผœ์ฃผ๊ณ 
  • ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด left์˜ ๊ฐ’์„ ํ”Œ๋Ÿฌ์Šค ์‹œ์ผœ์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  target์ด ์•ˆ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ๊ทผ์ ‘๊ฐ’์„ ๊ตฌํ•ด์•ผํ–ˆ๋‹ค.
  • target์—์„œ sum์„ ๋บ€ ์ ˆ๋Œ“๊ฐ’์ด result๋ฅผ ๋บ€ ์ ˆ๋Œ“๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด, ๋” ๊ทผ์ ‘ํ•œ ์ˆซ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— result๋ฅผ sum์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚˜๋ฉด result ๊ฐ’์„ ๋ฆฌํ„ด์‹œ์ผœ ์ฃผ์—ˆ๋‹ค.
public static int threeSumClosest(int[] nums, int target) {

   ...

   for (int i = 0; i < nums.length - 2; i++) {

      int left = i + 1;
      int right = nums.length - 1;

      while (left < right) {
         int sum = nums[left] + nums[right] + nums[i];

         if (sum == target) {
            return sum;
         }

         if (sum > target) {
            right--;
         } else {
            left++;
         }

         if (Math.abs(target - sum) < Math.abs(target - result)) {
            result = sum;
         }
      }
   }

   return result;
}

์ง์ „์— ํ’€์—ˆ๋˜ 3Sum ๋ฌธ์ œ์˜ ๋ณ€ํ˜•์ด์—ˆ๋‹ค.

๋•๋ถ„์— ์กฐ๊ธˆ ์ˆ˜์›”ํ•˜๊ฒŒ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.


๐Ÿ“ ์ตœ์ข… ํ’€์ด

 

public static int threeSumClosest(int[] nums, int target) {

   Arrays.sort(nums);
   int result = nums[0] + nums[1] + nums[2];

   for (int i = 0; i < nums.length - 2; i++) {

      int left = i + 1;
      int right = nums.length - 1;

      while (left < right) {
         int sum = nums[left] + nums[right] + nums[i];

         if (sum == target) {
            return sum;
         }

         if (sum > target) {
            right--;
         } else {
            left++;
         }

         if (Math.abs(target - sum) < Math.abs(target - result)) {
            result = sum;
         }
      }
   }

   return result;
}โ€‹

'์•Œ๊ณ ๋ฆฌ์ฆ˜ > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 015. 3Sum JAVA  (0) 2023.01.05
[LeetCode] 014.Longest Common Prefix JAVA  (0) 2023.01.04
[LeetCode] 013. Roman to Integer JAVA  (0) 2022.12.30
[LeetCode] 011.Container With Most Water JAVA  (0) 2022.12.26
[LeetCode] 012. Integer to Roman JAVA  (0) 2022.12.23

๋Œ“๊ธ€