forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcoFriendlyAppleSu.kt
More file actions
104 lines (91 loc) ยท 3.41 KB
/
EcoFriendlyAppleSu.kt
File metadata and controls
104 lines (91 loc) ยท 3.41 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package leetcode_study
/**
* ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ธ ์์์ ํฉ์ด 0์ธ ๊ฒฝ์ฐ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ . (์ธ ๊ฐ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ ์ค๋ณต๋์ง ์์)
*
* ์ฃผ์ด์ง ์กฐ๊ฑด
* 3 <= nums.length <= 3000
* -105 <= nums[i] <= 105
*/
/**
* case01. ์กฐํฉ์ ์ฌ์ฉํ ํ์ด.
* ์๊ฐ ์ด๊ณผ ๋ฐ์ ์ด์
* -> ๋ชจ๋ ๊ฐ๋ฅํ ์ธ ๊ฐ์ ์กฐํฉ์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ๋ฐ์.
* ์๊ฐ ๋ณต์ก๋:
* -> ์ธ ๊ฐ์ ์กฐํฉ ์์ฑ ๊ณผ์ : O(n * (n-1) * (n-2)) / 3. ์ต์
์ ๊ฒฝ์ฐ n = 3000, 4.5 ์ต๊ฐ ์กฐํฉ ์์ฑ
* -> ์ธ ๊ฐ์ ์กฐํฉ ๊ฒฐ๊ณผ sorting ๊ณผ์ : O(klogk). k = 3
* -> ๊ฒฐ๊ณผ๊ฐ์ ํํฐ๋งํด ํฉ์ด 0์ธ ๋ฐฐ์ด์ ํํฐํ๋ ๊ณผ์ : O(n)
* ๋๋จธ์ง ์ฐ์ฐ์ด ์ธ ๊ฐ์ ์กฐํฉ ์์ฑ ๊ณผ์ ์ ์ํฅ์ ๋ฐ์ ๊ณ์ฐ ํ์ ์ฆ๊ฐ.
*
* ๊ณต๊ฐ ๋ณต์ก๋:
* -> ๊ฐ ์กฐํฉ์ ๋ชจ๋ ์ ์ฅ: O(n^3)
*/
fun threeSumUseCombination(nums: IntArray): List<List<Int>> {
// ๊ฒฐ๊ณผ๋ฅผ ๋ด์ Set ์๋ฃ๊ตฌ์กฐ
val processResult = mutableSetOf<List<Int>>()
// ์ฃผ์ด์ง ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ ๋ด๋ ๋ณ์
val maxNumber = nums.size
// ์กฐํฉ ๋ฐฐ์ด์ ํฌ๊ธฐ
val givenSize = 3
// ๋ํ๋ผ ์ธ๋ฑ์ค๋ฅผ ๊ตฌํ๋ ๋ฐฐ์ด ์ด๊ธฐํ
val indices = IntArray(givenSize)
for (i in 0 until givenSize) {
indices[i] = i
}
while (indices[givenSize - 1] < maxNumber) {
processResult.add(indices.map { nums[it] }.sorted())
var i = givenSize - 1
while (i >= 0 && indices[i] == i + maxNumber - givenSize) {
i--
}
if (i >= 0) {
indices[i]++
for (j in i + 1 until givenSize) {
indices[j] = indices[j-1] + 1
}
} else break
}
return processResult.filter { it.sum() == 0 }
}
/**
* case02. ํฌ ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ํ์ด
* ์กฐํฉ์ ์ฌ์ฉํ ํ์ด์ ๋ฌ๋ฆฌ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ์ง ์์. O(n^3)์ ์๊ฐ ๋ณต์ก๋๋ฅผ O(n^2)์ผ๋ก ์ค์
*
* ์๊ฐ ๋ณต์ก๋:
* -> ์ฃผ์ด์ง ์ซ์ ๋ฐฐ์ด ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ: O(nlogn)
* -> ์ธ ๊ฐ์ ์ซ์๋ฅผ ๋ํ๋ ๋ก์ง
* -> ์ธ๋ถ ๋ฐ๋ณต๋ฌธ์ ํตํด ์ฃผ์ด์ง ๋ฐฐ์ด ์ ์ฒด ์กฐํ: O(n)
* -> ๋ด๋ถ ๋ฐ๋ณต๋ฌธ์ ํตํด start to last index ์ํ: O(n)
* -> O(n^2)
* โด O(nlogn) + O(n^2) => O(n^2)
*
* ๊ณต๊ฐ ๋ณต์ก๋:
* -> ์ฃผ์ด์ง ์ซ์ ๋ฐฐ์ด์ ์ ๋ ฌ์ ๋ด๋ ๊ณต๊ฐ ํ์: O(n)
*/
fun threeSum(nums: IntArray): List<List<Int>> {
val processResult = mutableListOf<List<Int>>()
val sortedNums = nums.sorted()
for (i in sortedNums.indices) {
if (i > 0 && sortedNums[i] == sortedNums[i-1]) continue
var startIndex = i + 1
var lastIndex = sortedNums.size - 1
while (startIndex < lastIndex) {
val sum = sortedNums[i] + sortedNums[startIndex] + sortedNums[lastIndex]
when {
sum == 0 -> {
processResult.add(listOf(sortedNums[i], sortedNums[startIndex], sortedNums[lastIndex]))
while (startIndex < lastIndex && sortedNums[startIndex] == sortedNums[startIndex + 1]) startIndex++
while (startIndex < lastIndex && sortedNums[lastIndex] == sortedNums[lastIndex - 1]) lastIndex--
startIndex++
lastIndex--
}
sum < 0 -> {
startIndex++
}
else -> {
lastIndex--
}
}
}
}
return processResult
}