Skip to content

Instantly share code, notes, and snippets.

@JunJaBoy
JunJaBoy / MergeTwoSortedLinkedLists.kt
Created April 26, 2024 03:48
HackerRank/Algorithm/MergeTwoSortedLinkedLists kotlin
fun main(args: Array<String>) {
repeat(readln().trim().toInt()) {
val result = mutableListOf<Int>()
repeat(readln().trimEnd().toInt()) {
result.add(readln().toInt())
}
repeat(readln().trimEnd().toInt()) {
result.add(readln().toInt())
}
result.sorted().forEach { print("$it ") }
@JunJaBoy
JunJaBoy / TowerBreakers.kt
Created April 26, 2024 03:03
HackerRank/Algorithm/TowerBreakers kotlin
import kotlin.collections.*
import kotlin.io.*
import kotlin.text.*
const val WIN_PLAYER_1 = 1
const val WIN_PLAYER_2 = 2
fun towerBreakers(n: Int, m: Int): Int =
// when the height is higher than 1, or count of towers are odd, the player 2 wins(player 1 loses)
if (m == 1 || n % 2 == 0) {
@JunJaBoy
JunJaBoy / CountingSort.kt
Created April 25, 2024 10:27
HackerRank/Algorithm/CountingSort kotlin
fun countingSort(arr: Array<Int>): Array<Int> =
IntArray(100) { 0 }
.apply {
arr.forEach { this[it]++ }
}
.toTypedArray()
fun main(args: Array<String>) {
val n = readln().trim().toInt()
@JunJaBoy
JunJaBoy / DiagonalDifference.kt
Created April 25, 2024 03:02
HackerRank/Algorithm/DiagonalDifference kotlin
import kotlin.*
import kotlin.math.absoluteValue
fun diagonalDifference(arr: Array<Array<Int>>): Int {
var sum = 0
for (i in arr.indices) {
sum += arr[i][i]
sum -= arr[i][arr.lastIndex - i]
}
return sum.absoluteValue
@JunJaBoy
JunJaBoy / LonelyInteger.kt
Created April 25, 2024 02:36
HackerRank/Algorithm/LonelyInteger kotlin
import kotlin.*
fun lonelyinteger(a: Array<Int>): Int =
a.groupingBy { it }
.eachCount()
.filterValues { it == 1 }
.entries
.single()
.key
@JunJaBoy
JunJaBoy / TimeConversion.kt
Created April 25, 2024 02:03
HackerRank/Algorithm/TimeConversion kotlin
import java.time.format.DateTimeFormatter
fun timeConversion(s: String): String {
val format = DateTimeFormatter.ofPattern("hh:mm:ssa")
val time = format.parse(s)
return DateTimeFormatter.ofPattern("HH:mm:ss").format(time)
}
fun main(args: Array<String>) {
val s = readln()
@JunJaBoy
JunJaBoy / MiniMaxSum.kt
Created April 25, 2024 01:14
HackerRate/Algorithm/MiniMaxSum kotlin
fun miniMaxSum(arr: Array<Int>) {
val sorted = arr.map(Int::toLong).sorted()
println("${sorted.dropLast(1).sum()} ${sorted.drop(1).sum()}")
}
fun main(args: Array<String>) {
val arr = readln().trimEnd()
.split(" ")
.map { it.toInt() }
.toTypedArray()
@JunJaBoy
JunJaBoy / Subset.kt
Created April 15, 2024 05:23
부분집합 탐색 알고리즘 with Kotlin
fun main() {
val target = readln().toInt()
findSubset(
initialValue = 1,
targetValue = target,
/*
방문 여부 확인 그래프.
target + 1개의 인덱스를 가진 리스트 생성, 초기값 지정(초기값 상관 없음)
인덱스 번호는 탐색한 숫자들을 나타내기 때문에 인덱스 0은 사용하지 않는다.
@JunJaBoy
JunJaBoy / CheckIfOnline.kt
Last active February 12, 2024 02:04
Snippet for checking if the device internet is available in Android
/**
* copied from jetcaster, android compose samples
* https://github.com/android/compose-samples/blob/main/Jetcaster/app/src/main/java/com/example/jetcaster/ui/JetcasterAppState.kt
*/
@Suppress("DEPRECATION")
fun checkIfOnline(): Boolean {
val cm = ContextCompat.getSystemService(context, ConnectivityManager::class.java)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val capabilities = cm?.getNetworkCapabilities(cm.activeNetwork) ?: return false
@JunJaBoy
JunJaBoy / Gradle Version Catalog vs buildSrc.csv
Created October 8, 2023 02:02
Gradle Version Catalog vs buildSrc
Gradle Version Catalog buildSrc
버전 검색 자동 수동
버전 번호 변경 후 Rebuild 버전의 영향을 받는 모듈만 프로젝트의 모든 모듈