Skip to content

Commit 66f7c20

Browse files
authored
Merge pull request DaleStudy#709 from JisooPyo/main
[JisooPyo] WEEK 02 solutions
2 parents f6a57ff + 8fecf13 commit 66f7c20

File tree

5 files changed

+647
-0
lines changed

5 files changed

+647
-0
lines changed

โ€Ž3sum/JisooPyo.kt

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
}

โ€Žclimbing-stairs/JisooPyo.kt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
6+
/**
7+
* Leetcode
8+
* 70. Climbing Stairs
9+
* Easy
10+
*
11+
* ์‚ฌ์šฉ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜: Dynamic Programming
12+
* n๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ• = n-1๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ• ์ˆ˜ + n-2๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•
13+
*/
14+
class ClimbingStairs {
15+
/**
16+
* Runtime: 0 ms(Beats: 100.00 %)
17+
* Time Complexity: O(n)
18+
* - ๋ฐฐ์—ด ์ˆœํšŒ
19+
*
20+
* Memory: 33.94 MB(Beats: 18.06 %)
21+
* Space Complexity: O(n)
22+
* - n+1 ํฌ๊ธฐ์˜ ๋ฐฐ์—ด ์‚ฌ์šฉ
23+
*/
24+
fun climbStairs1(n: Int): Int {
25+
if (n == 1) return 1
26+
if (n == 2) return 2
27+
28+
val arr = IntArray(n + 1)
29+
arr[1] = 1
30+
arr[2] = 2
31+
for (i in 3..n) {
32+
arr[i] = arr[i - 1] + arr[i - 2]
33+
}
34+
return arr[n]
35+
}
36+
37+
/**
38+
* ๋ฐฐ์—ด์„ ์“ฐ์ง€ ์•Š๊ณ  ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„ ํ•œ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.
39+
* Runtime: 0 ms(Beats: 100.00 %)
40+
* Time Complexity: O(n)
41+
* - n๋ฒˆ ์ˆœํšŒ
42+
*
43+
* Memory: 34.06 MB(Beats: 15.90 %)
44+
* Space Complexity: O(1)
45+
* - ์‚ฌ์šฉ๋˜๋Š” ์ถ”๊ฐ€ ๊ณต๊ฐ„์ด ์ž…๋ ฅ ํฌ๊ธฐ์™€ ๋ฌด๊ด€ํ•˜๊ฒŒ ์ผ์ •ํ•จ
46+
*/
47+
fun climbStairs2(n: Int): Int {
48+
if (n == 1 || n == 2) return n
49+
var firstCase = 1
50+
var secondCase = 2
51+
var totalSteps = 0
52+
53+
for (steps in 3..n) {
54+
totalSteps = firstCase + secondCase
55+
firstCase = secondCase
56+
secondCase = totalSteps
57+
}
58+
return totalSteps
59+
}
60+
61+
@Test
62+
fun test() {
63+
climbStairs1(2) shouldBe 2
64+
climbStairs1(3) shouldBe 3
65+
climbStairs2(2) shouldBe 2
66+
climbStairs2(3) shouldBe 3
67+
}
68+
}

0 commit comments

Comments
ย (0)