|
| 1 | +package leetcode_study |
| 2 | + |
| 3 | +import io.kotest.matchers.shouldBe |
| 4 | +import org.junit.jupiter.api.Test |
| 5 | + |
| 6 | +/** |
| 7 | + * Leetcode |
| 8 | + * 15. 3Sum |
| 9 | + * Medium |
| 10 | + */ |
| 11 | +class `3Sum` { |
| 12 | + /** |
| 13 | + * 3์ค for๋ฌธ์ผ๋ก ํ์ด๋ดค๋๋ฐ ์๊ฐ ์ด๊ณผ๊ฐ ๋๋๋ผ๊ณ ์(๋น์ฐ) |
| 14 | + * ์ฌ์ค ์ ๋ชจ๋ฅด๊ฒ ์ด์ Topic๊ณผ ํํธ๋ฅผ ์ด์ง ๋ดค๋๋ฐ ํฌ ํฌ์ธํฐ๊ฐ ์๊ธธ๋ ์ด๊ฑธ ์ด์ฉํด์ ํ์ด๋ณด๊ธฐ๋ก ํ์ต๋๋ค! |
| 15 | + * |
| 16 | + * Runtime: 72 ms(Beats: 60.54 %) |
| 17 | + * Time Complexity: O(n^2) |
| 18 | + * |
| 19 | + * Memory: 56.28 MB(Beats: 49.51 %) |
| 20 | + * Space Complexity: O(n^2) |
| 21 | + */ |
| 22 | + fun threeSum(nums: IntArray): List<List<Int>> { |
| 23 | + val answer = mutableListOf<List<Int>>() |
| 24 | + // ๋ฐฐ์ด ์ ๋ ฌ - ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํ๊ณ ํจ์จ์ ์ผ๋ก ํ์ํ๊ธฐ ์ํ์ฌ |
| 25 | + nums.sort() |
| 26 | + |
| 27 | + // nums[i]์ ์ด์ ๊ฐ์ ์๋ฏธํฉ๋๋ค. |
| 28 | + var prev = Integer.MIN_VALUE |
| 29 | + for (i in nums.indices) { |
| 30 | + // ์ด์ ๊ฐ๊ณผ ๋์ผํ ๊ฐ์ด๋ผ๋ฉด ์คํตํ์ฌ ์ค๋ณต๋ ๊ฒฝ์ฐ๋ฅผ ์ ๊ฑฐํฉ๋๋ค. |
| 31 | + if (nums[i] == prev) { |
| 32 | + continue |
| 33 | + } |
| 34 | + |
| 35 | + // ํฌ ํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํ์ฌ ๋ค ๋ํ์ฌ 0์ด ๋๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์ต๋๋ค. |
| 36 | + var left = i + 1 |
| 37 | + var right = nums.size - 1 |
| 38 | + while (left < right) { |
| 39 | + |
| 40 | + if (nums[i] + nums[left] + nums[right] > 0) { // ํฉ์ด 0๋ณด๋ค ํฌ๋ค๋ฉด right๋ฅผ ์ค์
๋๋ค. |
| 41 | + // if ๋ด์ ์๋ while๋ฌธ๋ค์ ์ค๋ณต ๊ฒฝ์ฐ์ ์๋ฅผ ํผํ๊ธฐ ์ํจ์
๋๋ค. |
| 42 | + while (0 <= right - 1 && nums[right - 1] == nums[right]) { |
| 43 | + right-- |
| 44 | + } |
| 45 | + right-- |
| 46 | + } else if (nums[i] + nums[left] + nums[right] < 0) { // ํฉ์ด 0๋ณด๋ค ์ ๋ค๋ฉด left๋ฅผ ๋์
๋๋ค. |
| 47 | + while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) { |
| 48 | + left++ |
| 49 | + } |
| 50 | + left++ |
| 51 | + } else { // ํฉ์ด 0์ด๋ผ๋ฉด ๊ฒฝ์ฐ์ ์๋ฅผ ์ถ๊ฐํฉ๋๋ค. |
| 52 | + answer.add(listOf(nums[i], nums[left], nums[right])) |
| 53 | + while (left + 1 <= nums.size - 1 && nums[left] == nums[left + 1]) { |
| 54 | + left++ |
| 55 | + } |
| 56 | + left++ |
| 57 | + while (0 <= right - 1 && nums[right - 1] == nums[right]) { |
| 58 | + right-- |
| 59 | + } |
| 60 | + right-- |
| 61 | + } |
| 62 | + } |
| 63 | + prev = nums[i] |
| 64 | + } |
| 65 | + return answer |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * ์๊ฐ ๋ณต์ก๋๋ ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ๊ฐ์ ๋์ง ์์์ง๋ง ๊ฐ๋
์ฑ ์ธก๋ฉด์์ ๊ฐ์ ํด๋ณธ ๋ฒ์ ์
๋๋ค. |
| 70 | + * Runtime: 66 ms(Beats: 65.59 %) |
| 71 | + * Time Complexity: O(n^2) |
| 72 | + * |
| 73 | + * Memory: 56.64 MB(Beats: 43.12 %) |
| 74 | + * Space Complexity: O(n^2) |
| 75 | + */ |
| 76 | + fun threeSum2(nums: IntArray): List<List<Int>> { |
| 77 | + val answer = mutableListOf<List<Int>>() |
| 78 | + nums.sort() |
| 79 | + |
| 80 | + // ์ฒซ ์ธ ์์ ํฉ์ด 0๋ณด๋ค ํฌ๊ฑฐ๋, ๋ง์ง๋ง ์ธ ์์ ํฉ์ด 0๋ณด๋ค ์์ผ๋ฉด ๋ถ๊ฐ๋ฅ |
| 81 | + val lastIndex = nums.size - 1 |
| 82 | + if (nums[0] + nums[1] + nums[2] > 0 || |
| 83 | + nums[lastIndex] + nums[lastIndex - 1] + nums[lastIndex - 2] < 0 |
| 84 | + ) { |
| 85 | + return emptyList() |
| 86 | + } |
| 87 | + |
| 88 | + var prev = nums[0] - 1 |
| 89 | + for (i in nums.indices) { |
| 90 | + // ์กฐ๊ธฐ ์ข
๋ฃ ์กฐ๊ฑด ์ถ๊ฐ |
| 91 | + if (nums[i] > 0) { |
| 92 | + break |
| 93 | + } |
| 94 | + if (nums[i] == prev) { |
| 95 | + continue |
| 96 | + } |
| 97 | + var left = i + 1 |
| 98 | + var right = nums.size - 1 |
| 99 | + while (left < right) { |
| 100 | + // ์ค๋ณต ๋ก์ง ์ ๊ฑฐ ๋ฐ sum ๋ณ์ํ |
| 101 | + val sum = nums[i] + nums[left] + nums[right] |
| 102 | + when { |
| 103 | + sum > 0 -> right = skipDuplicates(nums, right, false) |
| 104 | + sum < 0 -> left = skipDuplicates(nums, left, true) |
| 105 | + else -> { |
| 106 | + answer.add(listOf(nums[i], nums[left], nums[right])) |
| 107 | + left = skipDuplicates(nums, left, true) |
| 108 | + right = skipDuplicates(nums, right, false) |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + prev = nums[i] |
| 113 | + } |
| 114 | + return answer |
| 115 | + } |
| 116 | + |
| 117 | + private fun skipDuplicates(nums: IntArray, index: Int, isLeft: Boolean): Int { |
| 118 | + var current = index |
| 119 | + return if (isLeft) { |
| 120 | + while (current + 1 < nums.size && nums[current] == nums[current + 1]) current++ |
| 121 | + current + 1 |
| 122 | + } else { |
| 123 | + while (0 <= current - 1 && nums[current - 1] == nums[current]) current-- |
| 124 | + current - 1 |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun test() { |
| 130 | + threeSum(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf( |
| 131 | + listOf(-1, -1, 2), |
| 132 | + listOf(-1, 0, 1) |
| 133 | + ) |
| 134 | + threeSum(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>() |
| 135 | + threeSum(intArrayOf(0, 0, 0)) shouldBe listOf( |
| 136 | + listOf(0, 0, 0) |
| 137 | + ) |
| 138 | + threeSum(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf( |
| 139 | + listOf(-2, 0, 2) |
| 140 | + ) |
| 141 | + threeSum2(intArrayOf(-1, 0, 1, 2, -1, -4)) shouldBe listOf( |
| 142 | + listOf(-1, -1, 2), |
| 143 | + listOf(-1, 0, 1) |
| 144 | + ) |
| 145 | + threeSum2(intArrayOf(0, 1, 1)) shouldBe emptyList<Int>() |
| 146 | + threeSum2(intArrayOf(0, 0, 0)) shouldBe listOf( |
| 147 | + listOf(0, 0, 0) |
| 148 | + ) |
| 149 | + threeSum2(intArrayOf(-2, 0, 0, 2, 2)) shouldBe listOf( |
| 150 | + listOf(-2, 0, 2) |
| 151 | + ) |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments