From 4a90ba1e78c43dc87b40731ff1121f28029a8410 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 10:18:05 +0100 Subject: [PATCH 1/6] add prod variant for 981 --- .../contest/design/DesignProdVariant.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/contest/src/main/java/com/github/contest/design/DesignProdVariant.kt b/contest/src/main/java/com/github/contest/design/DesignProdVariant.kt index c346e0f0..cf173891 100644 --- a/contest/src/main/java/com/github/contest/design/DesignProdVariant.kt +++ b/contest/src/main/java/com/github/contest/design/DesignProdVariant.kt @@ -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>>() + + 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>, 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 + } + } + } + } \ No newline at end of file From cb0f583faa5ca9c96ad673b857056a1158313f79 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 11:35:14 +0100 Subject: [PATCH 2/6] add 1309 --- contest/src/main/java/com/github/contest/Execute.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index bd043331..e8a72017 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,7 +1,7 @@ package com.github.contest -import com.github.contest.strings.isNumber +import com.github.contest.strings.freqAlphabets import java.util.TreeMap @@ -11,7 +11,7 @@ import java.util.TreeMap fun main() { - isNumber("6+1").also { println(it) } + freqAlphabets("12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#").also { println(it) } } fun testing() { From 966ff98d3b82ffd60e443d152ee2be25736901ca Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 11:35:20 +0100 Subject: [PATCH 3/6] add 1309 --- .../github/contest/strings/StringsLeetcode.kt | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt index c3f38ce1..8028bcee 100644 --- a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt +++ b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt @@ -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 + } +} + + From f5d3e1e91dbf122a26c6f3d6624b6123a0ab0880 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 12:41:05 +0100 Subject: [PATCH 4/6] add 3396 --- .../main/java/com/github/contest/Execute.kt | 4 +- .../contest/hashTable/HashTableLeetcode.kt | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index e8a72017..37e1ba87 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,7 +1,7 @@ package com.github.contest -import com.github.contest.strings.freqAlphabets +import com.github.contest.hashTable.minimumOperations import java.util.TreeMap @@ -11,7 +11,7 @@ import java.util.TreeMap fun main() { - freqAlphabets("12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#").also { println(it) } + minimumOperations(intArrayOf(1, 2, 3, 4, 2, 3, 3, 5, 7)).also { println(it) } } fun testing() { diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt index aaf90379..d9980a9f 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -271,4 +271,45 @@ 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() + 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.size > 0) operations + 1 else operations + +} + +private fun MutableMap.reduceOrRemove(key: Int) { + this[key] = this.getOrDefault(key, 0) - 1 + if (this.getOrDefault(key, 0) == 0) this.remove(key) } \ No newline at end of file From 8adb8c07147fb20a9bd7f966bda142c85f9944d4 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 14:33:39 +0100 Subject: [PATCH 5/6] add 242 --- .../contest/hashTable/HashTableLeetcode.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt index d9980a9f..8647bbc5 100644 --- a/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt +++ b/contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt @@ -305,11 +305,30 @@ fun minimumOperations(nums: IntArray): Int { } } - return if (map.size > 0) operations + 1 else operations + return if (map.isNotEmpty()) operations + 1 else operations } private fun MutableMap.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 } \ No newline at end of file From c2b8615e28a30411bf72b6a58e19f16e079bbce5 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Tue, 8 Apr 2025 15:11:22 +0100 Subject: [PATCH 6/6] add new files and prod variant's and change 347 problem --- .../com/leetcode_kotlin/AlgorithmLeetcode.kt | 25 ---------------- .../priorityqueue/PriorityQueueLeetcode.kt | 29 +++++++++++++++++++ .../priorityqueue/PriorityQueueProdVariant.kt | 18 ++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueLeetcode.kt create mode 100644 contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueProdVariant.kt diff --git a/app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt b/app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt index 1187a83a..3127f6ed 100644 --- a/app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt +++ b/app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt @@ -2612,31 +2612,6 @@ fun findRelativeRanks(score: IntArray): Array { return ans } -/** - * 347. Top K Frequent Elements - */ - -fun topKFrequent(nums: IntArray, k: Int): IntArray? { - val map: MutableMap = HashMap() - for (n in nums) { - map[n] = map.getOrDefault(n, 0) + 1 - } - - val heap = PriorityQueue { a: Map.Entry, b: Map.Entry -> - 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 diff --git a/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueLeetcode.kt b/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueLeetcode.kt new file mode 100644 index 00000000..3f186a3b --- /dev/null +++ b/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueLeetcode.kt @@ -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 = HashMap() + for (n in nums) { + map[n] = map.getOrDefault(n, 0) + 1 + } + + val heap = PriorityQueue { a: Map.Entry, b: Map.Entry -> + 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 +} \ No newline at end of file diff --git a/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueProdVariant.kt b/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueProdVariant.kt new file mode 100644 index 00000000..2869c722 --- /dev/null +++ b/contest/src/main/java/com/github/contest/priorityqueue/PriorityQueueProdVariant.kt @@ -0,0 +1,18 @@ +package com.github.contest.priorityqueue + +/** + * 347. Top K Frequent Elements + */ + +fun topKFrequentProdVariant(nums: IntArray, k: Int): IntArray { + val freq = mutableMapOf() + val repeated = Array>(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() +} \ No newline at end of file