Skip to content

Instantly share code, notes, and snippets.

void withIf() {
Map<int, void> items = {
1: (),
2: (),
3: (),
4: (),
5: (),
};
items.forEach(
@JunJaBoy
JunJaBoy / JesseAndCookies2.kt
Created April 29, 2024 07:31
HackerRank/Algorithm/JesseAndCookies kotlin2
import java.util.*
import kotlin.*
import kotlin.collections.*
private fun List<Int>.sweetenCookies(targetSweetness: Int): Int {
val heap = PriorityQueue(this)
var count = 0
while (heap.size > 1 && heap.peek() <= targetSweetness) {
val first = heap.poll()
@JunJaBoy
JunJaBoy / IsPrime.kt
Created April 28, 2024 06:03
Best way to check if the number is prime
val Int.isPrime: Boolean
get() {
if (this <= 1) return false
for (i in 2..<this) {
if (this % i == 0) return false
}
return true
}
@JunJaBoy
JunJaBoy / JesseAndCookies.kt
Created April 27, 2024 13:18
HackerRank/Algorithm/JesseAndCookies kotlin
import java.util.*
import kotlin.*
import kotlin.collections.*
private fun cookies(
sweetness: Int,
cookies: Array<Int>,
): Int {
val result = PriorityQueue<Int>().apply { addAll(cookies) }
var countOfIteration = 0
@JunJaBoy
JunJaBoy / SimpleTextEditor.kt
Created April 27, 2024 04:55
HackerRank/Algorithm/SimpleTextEditor kotlin
import java.util.*
class TextEditor(initialValue: String = "") {
private val operationHistory = Stack<String>().apply { push(initialValue) }
val value: String
get() = operationHistory.peek()
fun append(text: String) {
operationHistory.push(operationHistory.peek() + text)
@JunJaBoy
JunJaBoy / BalancedBrackets.kt
Created April 26, 2024 14:00
HackerRank/Algorithm/BalancedBrackets kotlin
import java.util.*
const val RESULT_YES = "YES"
const val RESULT_NO = "NO"
fun isBalanced(s: String): String {
val stack = Stack<Char>()
s.forEach { char ->
when (char) {
'[' -> stack.push(']')
@JunJaBoy
JunJaBoy / QueueUsingTwoStacks.kt
Created April 26, 2024 13:28
HackerRank/Algorithm/QueueUsingTwoStacks kotlin
import java.util.*
const val QUEUE_ENQUEUE = 1
const val QUEUE_DEQUEUE = 2
const val QUEUE_PRINT_HEAD = 3
fun queueUsingTwoStacks(
numberOfQueues: Int,
scanner: Scanner = Scanner(System.`in`),
) {
@JunJaBoy
JunJaBoy / AlternativeSort.kt
Created April 26, 2024 04:22
AlternativeSort
fun IntArray.alternateSort(): IntArray {
require(this.isNotEmpty()) { "There must be at least one element" }
this.sort()
val result = mutableListOf<Int>()
var j = this.lastIndex
var i = 0
while (i < j) {
@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) {