Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions contains-duplicate/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package leetcode_study

import io.kotest.matchers.equals.shouldBeEqual
import org.junit.jupiter.api.Test

class `contains-duplicate`{

fun containsDuplicate(nums: IntArray): Boolean {
return third(nums)
}

// 시간초과
private fun first(nums: IntArray): Boolean {
nums.forEachIndexed { i, e1 ->
nums.forEachIndexed { j, e2 ->
if (i != j && e1 == e2) {
return true
}
}
}
return false
}

private fun second(nums: IntArray): Boolean {
nums.sort() // DualPivotQuicksort
for (index in 1 until nums.size) {
val prev = nums[index - 1]
val curr = nums[index]
if (prev == curr) {
return true
}
}
return false
}

private fun third(nums: IntArray): Boolean {
val set = nums.toSet()
return nums.size != set.size
}

@Test
fun 동일한_원소가_존재하면_true를_반환한다() {
val nums1 = intArrayOf(1, 2, 3, 1)
val nums2 = intArrayOf(1, 2, 3, 2)
val nums3 = intArrayOf(1, 1, 1, 1, 3, 3, 4, 3, 2, 4, 2)

containsDuplicate(nums1) shouldBeEqual true
containsDuplicate(nums2) shouldBeEqual true
containsDuplicate(nums3) shouldBeEqual true
}

@Test
fun 동일한_원소가_존재하지_않으면_false를_반환한다() {
val nums1 = intArrayOf(1, 2, 3, 4)
val nums2 = intArrayOf(1, 5, 7)

containsDuplicate(nums1) shouldBeEqual false
containsDuplicate(nums2) shouldBeEqual false
}
}
42 changes: 42 additions & 0 deletions number-of-1-bits/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package leetcode_study

import io.kotest.matchers.equals.shouldBeEqual
import org.junit.jupiter.api.Test

class `number-of-1-bits` {

fun hammingWeight(n: Int): Int {
return second(n)
}

private fun first(n: Int): Int {
var calc = n
var count = 0
while(calc > 0) {
if (calc % 2 != 0) {
count++
}
calc /= 2
}
return count
}

private fun second(n: Int): Int {
var calc = n
var count = 0
while (calc > 0) {
if (calc and 1 == 1) {
count ++
}
calc = calc shr 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코틀린은 >>shr 으로 표현하는군요
자바 유저라 처음보는데 재밌네요 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 비트 연산자는 처음 써봐서 ㅎㅎ 이번에 처음 배웠습니당

}
return count
}

@Test
fun `이진수에서_0이_아닌_성분의_개수를 반환한다`() {
hammingWeight(11) shouldBeEqual 3
hammingWeight(128) shouldBeEqual 1
hammingWeight(2147483645) shouldBeEqual 30
}
}
78 changes: 78 additions & 0 deletions top-k-frequent-elements/jdalma.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package leetcode_study

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import java.util.PriorityQueue

class `top-k-frequent-elements` {

fun topKFrequent(nums: IntArray, k: Int): IntArray {
return third(nums, k)
}

// Map 정렬
private fun first(nums: IntArray, k: Int): IntArray {
val map = mutableMapOf<Int, Int>()

nums.forEach {
map.compute(it) { _, oldValue ->
if (oldValue == null) 1
else oldValue + 1
}
}

return map.entries.sortedByDescending { it.value }
.map { it.key }
.slice(0 until k)
.toIntArray()
}

// 우선순위 큐 사용
private fun second(nums: IntArray, k: Int): IntArray {
val map = mutableMapOf<Int, Int>()

nums.forEach { map.put(it, map.getOrDefault(it, 0) + 1) }

val heap: PriorityQueue<Map.Entry<Int, Int>> = PriorityQueue<Map.Entry<Int, Int>> {
v1, v2 -> v2.value.compareTo(v1.value)
}.apply {
this.addAll(map.entries)
}

return (0 until k).map { heap.poll().key }.toIntArray()
}

// 이차원배열로 빈번도 저장
private fun third(nums: IntArray, k: Int): IntArray {
val map = mutableMapOf<Int, Int>()

nums.forEach { map.put(it, map.getOrDefault(it, 0) + 1) }

val freq = Array<MutableList<Int>>(nums.size + 1) { mutableListOf() }
map.entries.forEach {
val frequency = it.value
freq[frequency].add(it.key)
}

val result = IntArray(k)
var index = 0
(freq.size - 1 downTo 0).forEach { i ->
freq[i].forEach {
result[index++] = it
if (index == k) {
return result
}
}
}

return IntArray(0)
}

@Test
fun `배열에서_가장_빈도가_높은_K개의_원소를_출력한다`() {
topKFrequent(intArrayOf(1,1,1,2,2,3), 2) shouldBe intArrayOf(1,2)
topKFrequent(intArrayOf(1,1,1,2,2,3,3,4), 3) shouldBe intArrayOf(1,2,3)
topKFrequent(intArrayOf(2,2,3,3,1,1,4), 3) shouldBe intArrayOf(2,3,1)
topKFrequent(intArrayOf(4,1,-1,2,-1,2,3), 2) shouldBe intArrayOf(-1,2)
}
}