-
-
Notifications
You must be signed in to change notification settings - Fork 245
[정현준] 1주차 답안 제출 #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[정현준] 1주차 답안 제출 #298
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
return count | ||
} | ||
|
||
@Test | ||
fun `이진수에서_0이_아닌_성분의_개수를 반환한다`() { | ||
hammingWeight(11) shouldBeEqual 3 | ||
hammingWeight(128) shouldBeEqual 1 | ||
hammingWeight(2147483645) shouldBeEqual 30 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코틀린은
>>
를shr
으로 표현하는군요자바 유저라 처음보는데 재밌네요 ㅎㅎ
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 비트 연산자는 처음 써봐서 ㅎㅎ 이번에 처음 배웠습니당