|
1 |
| -// ๋ฌธ์ ์๊ตฌ์ฌํญ -> O(n^2) ๋ณด๋ค ์์ ์๊ฐ๋ณต์ก๋๋ก ๊ตฌํ ํ์ |
2 |
| -// ๋ฌธ์ ๋ฅผ ๋ณด์๋ง์ ์๊ฐ๋ ๊ฒ -> ์ด๋ถํ์ (2 ์์ ํฉ์ด target ์ธ ๊ฒ์ ๊ตฌํด์ผ ํ๋ฏ๋ก) |
3 |
| -// ์ด๋ถ ํ์ ์๊ฐ ๋ณต์ก๋ -> ์ ๋ ฌ(O(nlogn)) + ์ด๋ถํ์ (log(n)) -> nlogn |
4 |
| -// ๊ณต๊ฐ ๋ณต์ก๋ -> ๋ฐฐ์ด ํฌ๊ธฐ ๋งํผ ๊ณต๊ฐ ํ์ (n) |
| 1 | +// two pointer |
5 | 2 |
|
6 | 3 | import java.util.Arrays;
|
7 |
| -import java.util.HashMap; |
| 4 | +import java.util.Comparator; |
8 | 5 |
|
9 |
| -class ValueIndex implements Comparable<ValueIndex> { |
10 |
| - int value; |
| 6 | +class Point { |
11 | 7 | int index;
|
| 8 | + int value; |
| 9 | + Point(int index, int value){ |
| 10 | + this.index = index; |
| 11 | + this.value = value;} |
12 | 12 |
|
13 |
| - // ValueIndex ๊ฐ์ฒด๋ฅผ ์ ๋ ฌํ ๋ value ๊ธฐ์ค์ผ๋ก ์ค๋ฆ์ฐจ์ ์ ๋ ฌ |
14 |
| - @Override |
15 |
| - public int compareTo(ValueIndex other) { |
16 |
| - return Integer.compare(this.value, other.value); |
17 |
| - } |
18 | 13 | }
|
19 | 14 |
|
20 |
| -class Solution { |
21 |
| - |
| 15 | +// ์๊ฐ ๋ณต์ก๋: O(nlogn) - ์ ๋ ฌ |
| 16 | +// ๊ณต๊ฐ ๋ณต์ก๋: O(n) - Point ๊ฐ์ฒด ๋ฐฐ์ด |
| 17 | +class Solution{ |
22 | 18 | public int[] twoSum(int[] nums, int target) {
|
23 |
| - int size = nums.length; |
24 |
| - ValueIndex[] valueIndex = new ValueIndex[size]; |
25 |
| - for (int i = 0; i < size; ++i) { |
26 |
| - valueIndex[i] = new ValueIndex(); |
27 |
| - valueIndex[i].value = nums[i]; |
28 |
| - valueIndex[i].index = i; // ์ ๋ ฌ ์ original index ์ ์ฅ |
| 19 | + Point[] points = new Point[nums.length]; |
| 20 | + for(int i = 0; i < nums.length; i++){ |
| 21 | + points[i] = new Point(i, nums[i]); |
29 | 22 | }
|
30 |
| - Arrays.sort(valueIndex); // ์ ๋ ฌ |
| 23 | + Arrays.sort(points, Comparator.comparingInt(p -> p.value)); |
| 24 | + int[] result = new int[2]; |
31 | 25 | int left = 0;
|
32 | 26 | int right = nums.length - 1;
|
33 |
| - |
34 | 27 | while (left < right) {
|
35 |
| - int leftVal = valueIndex[left].value; |
36 |
| - int rightVal = valueIndex[right].value; |
37 |
| - int sum = leftVal + rightVal; |
38 |
| - |
39 |
| - if (sum < target) { // target ๋ณด๋ค ํฉ์ด ์์ผ๋ฉด, left ๊ฐ์ด ์ปค์ ธ์ผ ํจ |
40 |
| - left += 1; |
41 |
| - } else if (sum > target) { |
42 |
| - right -= 1; // target ๋ณด๋ค ํฉ์ด ํฌ๋ฉด, right ๊ฐ์ด ์์์ ธ์ผ ํจ |
43 |
| - } else { // sum = target ์ด๋ฉด ๋! |
44 |
| - break; |
45 |
| - } |
46 |
| - } |
47 |
| - |
48 |
| - return new int[]{valueIndex[left].index, valueIndex[right].index}; |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| - |
53 |
| -/** |
54 |
| - * hashMap์ ์ด์ฉํ O(n) ๋ฐฉ๋ฒ๋ ์๋ค๊ณ ํด์ ์ถ๊ฐ ๊ตฌํํด๋ณด์์ต๋๋ค. (์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ O(n)) |
55 |
| - */ |
56 |
| - |
57 |
| -class Solution { |
58 |
| - |
59 |
| - public int[] twoSum(int[] nums, int target) { |
60 |
| - HashMap<Integer, Integer> numMap = new HashMap<>(); |
61 |
| - int left = 0, right = 0; |
62 |
| - for (int i = 0; i < nums.length; i++) { |
63 |
| - int complement = target - nums[i]; // ํ์ฌ ์ซ์์ ํฉ์ณ์ target์ ๋ง๋๋ ์ซ์ |
64 |
| - |
65 |
| - // ์ด๋ฏธ ๊ทธ ์ซ์๊ฐ ํด์๋งต์ ์๋ค๋ฉด, ๋ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ |
66 |
| - if (numMap.containsKey(complement)) { |
67 |
| - left = numMap.get(complement); |
68 |
| - right = i; |
| 28 | + int sum = points[left].value + points[right].value; |
| 29 | + if (sum == target) { |
| 30 | + result[0] = points[left].index; |
| 31 | + result[1] = points[right].index; |
69 | 32 | break;
|
| 33 | + } else if (sum < target) { |
| 34 | + left++; |
| 35 | + } else { |
| 36 | + right--; |
70 | 37 | }
|
71 |
| - |
72 |
| - // ํด์๋งต์ ํ์ฌ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ์ถ๊ฐ |
73 |
| - numMap.put(nums[i], i); |
74 | 38 | }
|
75 |
| - |
76 |
| - return new int[]{left, right}; |
77 |
| - |
| 39 | + return result; |
78 | 40 | }
|
79 | 41 | }
|
80 |
| - |
0 commit comments