Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 0 additions & 25 deletions app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2612,31 +2612,6 @@ fun findRelativeRanks(score: IntArray): Array<String> {
return ans
}

/**
* 347. Top K Frequent Elements
*/

fun topKFrequent(nums: IntArray, k: Int): IntArray? {
val map: MutableMap<Int, Int> = HashMap()
for (n in nums) {
map[n] = map.getOrDefault(n, 0) + 1
}

val heap = PriorityQueue { a: Map.Entry<Int, Int>, b: Map.Entry<Int, Int> ->
b.value.compareTo(a.value)
}

for (entry in map.entries) {
heap.offer(entry)
}

val res = IntArray(k)
for (i in 0 until k) {
res[i] = heap.poll().key
}

return res
}

/**
* 437. Path Sum III
Expand Down
4 changes: 2 additions & 2 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.contest


import com.github.contest.strings.isNumber
import com.github.contest.hashTable.minimumOperations
import java.util.TreeMap


Expand All @@ -11,7 +11,7 @@ import java.util.TreeMap

fun main() {

isNumber("6+1").also { println(it) }
minimumOperations(intArrayOf(1, 2, 3, 4, 2, 3, 3, 5, 7)).also { println(it) }
}

fun testing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,43 @@ class ProductOfNumbersProdVariant() {
}
return products.last() / products[products.size - k - 1]
}
}

/**
* 981. Time Based Key-Value Store
* Prod Variant
*/

class TimeMapProdVariant {

private val store = mutableMapOf<String, MutableList<Pair<Int, String>>>()

fun set(key: String, value: String, timestamp: Int) {
store.getOrPut(key) { mutableListOf(Pair(timestamp, value)) }.also {
it.add(Pair(timestamp, value))
}
}

fun get(key: String, timestamp: Int): String = when {
store[key] == null -> ""
else -> getFromList(store.getOrDefault(key, listOf()), timestamp)
}

private fun getFromList(list: List<Pair<Int, String>>, timestamp: Int): String =
list.binarySearch {
when {
it.first == timestamp -> 0
it.first < timestamp -> -1
else -> 1
}
}.let { index ->
when {
index >= 0 -> list[index].second
else -> {
val insertionPoint = -index - 1
if (insertionPoint == 0) "" else list[insertionPoint - 1].second
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,64 @@ fun repeatedCharacter(s: String): Char {
}

return 'a'
}

/**
* 3396. Minimum Number of Operations to Make Elements in Array Distinct
*/

fun minimumOperations(nums: IntArray): Int {
val map = mutableMapOf<Int, Int>()
var operations = 0

for (num in nums) map[num] = map.getOrDefault(num, 0) + 1

if (map.size == nums.size) return 0

for (i in nums.indices step 3) {
if (i + 2 < nums.size) {
val one = nums[i]
val two = nums[i + 1]
val three = nums[i + 2]
map.reduceOrRemove(one)
map.reduceOrRemove(two)
map.reduceOrRemove(three)
var isUnique = true
for (value in map.values) {
if (value > 1) {
isUnique = false
break
}
}
if (isUnique) return operations + 1
operations++
}
}

return if (map.isNotEmpty()) operations + 1 else operations

}

private fun MutableMap<Int, Int>.reduceOrRemove(key: Int) {
this[key] = this.getOrDefault(key, 0) - 1
if (this.getOrDefault(key, 0) == 0) this.remove(key)
}

/**
* 242. Valid Anagram
*/

fun isAnagram(s: String, t: String): Boolean {
if (s.length != t.length) return false
val first = IntArray(26)
val second = IntArray(26)

for (char in s) first[char - 'a']++
for (char in t) second[char - 'a']++

for (char in t) {
if (first[char - 'a'] == 0 || first[char - 'a'] != second[char - 'a']) return false
}

return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.contest.priorityqueue

import java.util.PriorityQueue

/**
* 347. Top K Frequent Elements
*/

fun topKFrequent(nums: IntArray, k: Int): IntArray? {
val map: MutableMap<Int, Int> = HashMap()
for (n in nums) {
map[n] = map.getOrDefault(n, 0) + 1
}

val heap = PriorityQueue { a: Map.Entry<Int, Int>, b: Map.Entry<Int, Int> ->
b.value.compareTo(a.value)
}

for (entry in map.entries) {
heap.offer(entry)
}

val res = IntArray(k)
for (i in 0 until k) {
res[i] = heap.poll().key
}

return res
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.contest.priorityqueue

/**
* 347. Top K Frequent Elements
*/

fun topKFrequentProdVariant(nums: IntArray, k: Int): IntArray {
val freq = mutableMapOf<Int, Int>()
val repeated = Array<MutableList<Int>>(nums.size + 1) {mutableListOf()}

for (num in nums) freq[num] = freq.getOrDefault(num, 0) + 1

for ((num, count) in freq) {
repeated[count].add(num)
}

return repeated.flatMap {it}.takeLast(k).toIntArray()
}
145 changes: 145 additions & 0 deletions contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,149 @@ fun percentageLetter(s: String, letter: Char): Int {
}


/**
* 1309. Decrypt String from Alphabet to Integer Mapping
*/

fun freqAlphabets(s: String): String {
val result = StringBuilder()
var i = s.length - 1

while (i >= 0) {
if (s[i] == '#') {
val twoDigit = s.substring(i - 2, i).toInt()
result.append(getLetterFromIndex(twoDigit - 1))
i -= 3
} else {
result.append(getLetterFromIndex(s[i].digitToInt() - 1))
i -= 1
}
}

return result.reverse().toString()
}

fun getLetterFromIndex(index: Int): Char {
require(index in 0..25) { "Index must be between 0 and 25 (inclusive)" }
return 'a' + index
}


/**
*
*/

fun isNumber(s: String): Boolean = when {
isOnlyNumbers(s) -> true
hasLetter(s) -> false
isValidNumberSign(s) && !hasExponential(s) -> true
isNotValidNumberSign(s) -> false
else -> hasNumberExponential(s)
}

private fun hasNumberExponential(str: String): Boolean {
var indexExp = 0
var isNotDigits = true
for (i in str.indices) {
if (isExponential(str[i])) {
indexExp = i
break
}
}

for (i in 0 until indexExp) {
if (isDigit(str[i])) isNotDigits = false
}

if (isNotDigits) return false
isNotDigits = true
for (i in indexExp until str.length) {
if (str[i] == '.') return false
if (isDigit(str[i])) isNotDigits = false
}

return !isNotDigits
}

private fun isValidNumberSign(str: String): Boolean {
var isPlusOrMinus = false
var isOneDot = false
for (char in str) {
if (isSign(char)) {
if (isPlusOrMinus) return false
else isPlusOrMinus = true
}
if (isDot(char) && !isOneDot) {
isOneDot = true
continue
}
if (isDot(char) && isOneDot) return false
}
return true
}

private fun hasExponential(str: String): Boolean {
for (char in str) if (isExponential(char)) return true
return false
}

private fun isNotValidNumberSign(str: String): Boolean {
var isPlusOrMinus = false
var isOneDot = false
for (char in str) {
if (isSign(char)) {
if (isPlusOrMinus) return true
else isPlusOrMinus = true
}
if (isDot(char) && !isOneDot) {
isOneDot = true
continue
}
if (isOneDot) return true
}
return false
}

private fun hasLetter(str: String): Boolean {
for (char in str) {
if (isLetter(char)) return true
}
return false
}

private fun isOnlyNumbers(str: String): Boolean {
for (char in str) {
if (isLetter(char) || isExponential(char) || isSign(char) || isDot(char)) return false
}
return true
}

fun isDigit(char: Char): Boolean = when {
char in '0'..'9' -> true
else -> false
}

fun isLetter(char: Char): Boolean = when {
(char != 'e' && char != 'E') && (char in 'a'..'z' || char in 'A'..'Z') -> true
else -> false
}

fun isExponential(char: Char): Boolean = when {
char == 'e' || char == 'E' -> true
else -> false
}

fun isDot(char: Char) = when {
char == '.' -> true
else -> false
}

fun isSign(char: Char): Boolean {
return when {
char == '+' || char == '-' -> true
else -> false
}
}