Skip to content

Commit 03b0953

Browse files
committed
1주차 3문제 추가
1 parent ee1ad8e commit 03b0953

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

contains-duplicate/jdalma.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.equals.shouldBeEqual
4+
import org.junit.jupiter.api.Test
5+
6+
class `contains-duplicate`{
7+
8+
fun containsDuplicate(nums: IntArray): Boolean {
9+
return third(nums)
10+
}
11+
12+
// 시간초과
13+
private fun first(nums: IntArray): Boolean {
14+
nums.forEachIndexed { i, e1 ->
15+
nums.forEachIndexed { j, e2 ->
16+
if (i != j && e1 == e2) {
17+
return true
18+
}
19+
}
20+
}
21+
return false
22+
}
23+
24+
private fun second(nums: IntArray): Boolean {
25+
nums.sort() // DualPivotQuicksort
26+
for (index in 1 until nums.size) {
27+
val prev = nums[index - 1]
28+
val curr = nums[index]
29+
if (prev == curr) {
30+
return true
31+
}
32+
}
33+
return false
34+
}
35+
36+
private fun third(nums: IntArray): Boolean {
37+
val set = nums.toSet()
38+
return nums.size != set.size
39+
}
40+
41+
@Test
42+
fun 동일한_원소가_존재하면_true_반환한다() {
43+
val nums1 = intArrayOf(1, 2, 3, 1)
44+
val nums2 = intArrayOf(1, 2, 3, 2)
45+
val nums3 = intArrayOf(1, 1, 1, 1, 3, 3, 4, 3, 2, 4, 2)
46+
47+
containsDuplicate(nums1) shouldBeEqual true
48+
containsDuplicate(nums2) shouldBeEqual true
49+
containsDuplicate(nums3) shouldBeEqual true
50+
}
51+
52+
@Test
53+
fun 동일한_원소가_존재하지_않으면_false_반환한다() {
54+
val nums1 = intArrayOf(1, 2, 3, 4)
55+
val nums2 = intArrayOf(1, 5, 7)
56+
57+
containsDuplicate(nums1) shouldBeEqual false
58+
containsDuplicate(nums2) shouldBeEqual false
59+
}
60+
}

number-of-1-bits/jdalma.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.equals.shouldBeEqual
4+
import org.junit.jupiter.api.Test
5+
6+
class `number-of-1-bits` {
7+
8+
fun hammingWeight(n: Int): Int {
9+
return second(n)
10+
}
11+
12+
private fun first(n: Int): Int {
13+
var calc = n
14+
var count = 0
15+
while(calc > 0) {
16+
if (calc % 2 != 0) {
17+
count++
18+
}
19+
calc /= 2
20+
}
21+
return count
22+
}
23+
24+
private fun second(n: Int): Int {
25+
var calc = n
26+
var count = 0
27+
while (calc > 0) {
28+
if (calc and 1 == 1) {
29+
count ++
30+
}
31+
calc = calc shr 1
32+
}
33+
return count
34+
}
35+
36+
@Test
37+
fun `이진수에서_0이_아닌_성분의_개수를 반환한다`() {
38+
hammingWeight(11) shouldBeEqual 3
39+
hammingWeight(128) shouldBeEqual 1
40+
hammingWeight(2147483645) shouldBeEqual 30
41+
}
42+
}

top-k-frequent-elements/jdalma.kt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package leetcode_study
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.jupiter.api.Test
5+
import java.util.PriorityQueue
6+
7+
class `top-k-frequent-elements` {
8+
9+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
10+
return third(nums, k)
11+
}
12+
13+
// Map 정렬
14+
private fun first(nums: IntArray, k: Int): IntArray {
15+
val map = mutableMapOf<Int, Int>()
16+
17+
nums.forEach {
18+
map.compute(it) { _, oldValue ->
19+
if (oldValue == null) 1
20+
else oldValue + 1
21+
}
22+
}
23+
24+
return map.entries.sortedByDescending { it.value }
25+
.map { it.key }
26+
.slice(0 until k)
27+
.toIntArray()
28+
}
29+
30+
// 우선순위 큐 사용
31+
private fun second(nums: IntArray, k: Int): IntArray {
32+
val map = mutableMapOf<Int, Int>()
33+
34+
nums.forEach { map.put(it, map.getOrDefault(it, 0) + 1) }
35+
36+
val heap: PriorityQueue<Map.Entry<Int, Int>> = PriorityQueue<Map.Entry<Int, Int>> {
37+
v1, v2 -> v2.value.compareTo(v1.value)
38+
}.apply {
39+
this.addAll(map.entries)
40+
}
41+
42+
return (0 until k).map { heap.poll().key }.toIntArray()
43+
}
44+
45+
// 이차원배열로 빈번도 저장
46+
private fun third(nums: IntArray, k: Int): IntArray {
47+
val map = mutableMapOf<Int, Int>()
48+
49+
nums.forEach { map.put(it, map.getOrDefault(it, 0) + 1) }
50+
51+
val freq = Array<MutableList<Int>>(nums.size + 1) { mutableListOf() }
52+
map.entries.forEach {
53+
val frequency = it.value
54+
freq[frequency].add(it.key)
55+
}
56+
57+
val result = IntArray(k)
58+
var index = 0
59+
(freq.size - 1 downTo 0).forEach { i ->
60+
freq[i].forEach {
61+
result[index++] = it
62+
if (index == k) {
63+
return result
64+
}
65+
}
66+
}
67+
68+
return IntArray(0)
69+
}
70+
71+
@Test
72+
fun `배열에서_가장_빈도가_높은_K개의_원소를_출력한다`() {
73+
topKFrequent(intArrayOf(1,1,1,2,2,3), 2) shouldBe intArrayOf(1,2)
74+
topKFrequent(intArrayOf(1,1,1,2,2,3,3,4), 3) shouldBe intArrayOf(1,2,3)
75+
topKFrequent(intArrayOf(2,2,3,3,1,1,4), 3) shouldBe intArrayOf(2,3,1)
76+
topKFrequent(intArrayOf(4,1,-1,2,-1,2,3), 2) shouldBe intArrayOf(-1,2)
77+
}
78+
}

0 commit comments

Comments
 (0)