|
| 1 | +package leetcode_study |
| 2 | + |
| 3 | +/** |
| 4 | + * ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ธ ์์์ ํฉ์ด 0์ธ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ . (์ธ ๊ฐ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ ์ค๋ณต๋์ง ์์) |
| 5 | + * |
| 6 | + * ์ฃผ์ด์ง ์กฐ๊ฑด |
| 7 | + * 3 <= nums.length <= 3000 |
| 8 | + * -105 <= nums[i] <= 105 |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * case01. ์กฐํฉ์ ์ฌ์ฉํ ํ์ด. |
| 13 | + * ์๊ฐ ์ด๊ณผ ๋ฐ์ ์ด์ |
| 14 | + * -> ๋ชจ๋ ๊ฐ๋ฅํ ์ธ ๊ฐ์ ์กฐํฉ์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋ฐ์. |
| 15 | + * ์๊ฐ ๋ณต์ก๋: |
| 16 | + * -> ์ธ ๊ฐ์ ์กฐํฉ ์์ฑ ๊ณผ์ : O(n * (n-1) * (n-2)) / 3. ์ต์
์ ๊ฒฝ์ฐ n = 3000, 4.5 ์ต๊ฐ ์กฐํฉ ์์ฑ |
| 17 | + * -> ์ธ ๊ฐ์ ์กฐํฉ ๊ฒฐ๊ณผ sorting ๊ณผ์ : O(klogk). k = 3 |
| 18 | + * -> ๊ฒฐ๊ณผ๊ฐ์ ํํฐ๋งํด ํฉ์ด 0์ธ ๋ฐฐ์ด์ ํํฐํ๋ ๊ณผ์ : O(n) |
| 19 | + * ๋๋จธ์ง ์ฐ์ฐ์ด ์ธ ๊ฐ์ ์กฐํฉ ์์ฑ ๊ณผ์ ์ ์ํฅ์ ๋ฐ์ ๊ณ์ฐ ํ์ ์ฆ๊ฐ. |
| 20 | + * |
| 21 | + * ๊ณต๊ฐ ๋ณต์ก๋: |
| 22 | + * -> ๊ฐ ์กฐํฉ์ ๋ชจ๋ ์ ์ฅ: O(n^3) |
| 23 | + */ |
| 24 | +fun threeSumUseCombination(nums: IntArray): List<List<Int>> { |
| 25 | + // ๊ฒฐ๊ณผ๋ฅผ ๋ด์ Set ์๋ฃ๊ตฌ์กฐ |
| 26 | + val processResult = mutableSetOf<List<Int>>() |
| 27 | + |
| 28 | + // ์ฃผ์ด์ง ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ด๋ ๋ณ์ |
| 29 | + val maxNumber = nums.size |
| 30 | + |
| 31 | + // ์กฐํฉ ๋ฐฐ์ด์ ํฌ๊ธฐ |
| 32 | + val givenSize = 3 |
| 33 | + |
| 34 | + // ๋ํ๋ผ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๋ ๋ฐฐ์ด ์ด๊ธฐํ |
| 35 | + val indices = IntArray(givenSize) |
| 36 | + for (i in 0 until givenSize) { |
| 37 | + indices[i] = i |
| 38 | + } |
| 39 | + |
| 40 | + while (indices[givenSize - 1] < maxNumber) { |
| 41 | + processResult.add(indices.map { nums[it] }.sorted()) |
| 42 | + var i = givenSize - 1 |
| 43 | + |
| 44 | + while (i >= 0 && indices[i] == i + maxNumber - givenSize) { |
| 45 | + i-- |
| 46 | + } |
| 47 | + |
| 48 | + if (i >= 0) { |
| 49 | + indices[i]++ |
| 50 | + for (j in i + 1 until givenSize) { |
| 51 | + indices[j] = indices[j-1] + 1 |
| 52 | + } |
| 53 | + } else break |
| 54 | + } |
| 55 | + |
| 56 | + return processResult.filter { it.sum() == 0 } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * case02. ํฌ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ํ์ด |
| 61 | + * ์กฐํฉ์ ์ฌ์ฉํ ํ์ด์ ๋ฌ๋ฆฌ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ์ง ์์. O(n^3)์ ์๊ฐ ๋ณต์ก๋๋ฅผ O(n^2)์ผ๋ก ์ค์ |
| 62 | + * |
| 63 | + * ์๊ฐ ๋ณต์ก๋: |
| 64 | + * -> ์ฃผ์ด์ง ์ซ์ ๋ฐฐ์ด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ: O(nlogn) |
| 65 | + * -> ์ธ ๊ฐ์ ์ซ์๋ฅผ ๋ํ๋ ๋ก์ง |
| 66 | + * -> ์ธ๋ถ ๋ฐ๋ณต๋ฌธ์ ํตํด ์ฃผ์ด์ง ๋ฐฐ์ด ์ ์ฒด ์กฐํ: O(n) |
| 67 | + * -> ๋ด๋ถ ๋ฐ๋ณต๋ฌธ์ ํตํด start to last index ์ํ: O(n) |
| 68 | + * -> O(n^2) |
| 69 | + * โด O(nlogn) + O(n^2) => O(n^2) |
| 70 | + * |
| 71 | + * ๊ณต๊ฐ ๋ณต์ก๋: |
| 72 | + * -> ์ฃผ์ด์ง ์ซ์ ๋ฐฐ์ด์ ์ ๋ ฌ์ ๋ด๋ ๊ณต๊ฐ ํ์: O(n) |
| 73 | + */ |
| 74 | +fun threeSum(nums: IntArray): List<List<Int>> { |
| 75 | + val processResult = mutableListOf<List<Int>>() |
| 76 | + val sortedNums = nums.sorted() |
| 77 | + |
| 78 | + for (i in sortedNums.indices) { |
| 79 | + if (i > 0 && sortedNums[i] == sortedNums[i-1]) continue |
| 80 | + |
| 81 | + var startIndex = i + 1 |
| 82 | + var lastIndex = sortedNums.size - 1 |
| 83 | + |
| 84 | + while (startIndex < lastIndex) { |
| 85 | + val sum = sortedNums[i] + sortedNums[startIndex] + sortedNums[lastIndex] |
| 86 | + when { |
| 87 | + sum == 0 -> { |
| 88 | + processResult.add(listOf(sortedNums[i], sortedNums[startIndex], sortedNums[lastIndex])) |
| 89 | + while (startIndex < lastIndex && sortedNums[startIndex] == sortedNums[startIndex + 1]) startIndex++ |
| 90 | + while (startIndex < lastIndex && sortedNums[lastIndex] == sortedNums[lastIndex - 1]) lastIndex-- |
| 91 | + startIndex++ |
| 92 | + lastIndex-- |
| 93 | + } |
| 94 | + sum < 0 -> { |
| 95 | + startIndex++ |
| 96 | + } |
| 97 | + else -> { |
| 98 | + lastIndex-- |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return processResult |
| 104 | +} |
0 commit comments