Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4f9efb6
add 2140 brute force solution
Ashwagandha-coder Apr 1, 2025
7def576
add 2140 two approach
Ashwagandha-coder Apr 1, 2025
f2b1a97
add 2467
Ashwagandha-coder Apr 1, 2025
26801a3
add 2873
Ashwagandha-coder Apr 2, 2025
8aed66a
add 2874
Ashwagandha-coder Apr 3, 2025
31fd53a
add 848
Ashwagandha-coder Apr 4, 2025
5ec47ef
add new file for strings and change last problem of strings
Ashwagandha-coder Apr 4, 2025
16f1881
add prod variant 848
Ashwagandha-coder Apr 5, 2025
24a01b6
small change
Ashwagandha-coder Apr 5, 2025
14cf3f6
add 2734
Ashwagandha-coder Apr 5, 2025
13642b6
add 2734 prod variant
Ashwagandha-coder Apr 5, 2025
f80280d
add 1863
Ashwagandha-coder Apr 5, 2025
f63e2c5
add 2351
Ashwagandha-coder Apr 5, 2025
6a0223f
add 2278
Ashwagandha-coder Apr 5, 2025
ce0aab7
add 2140 prod variant with custom ext
Ashwagandha-coder Apr 5, 2025
b14e505
small change
Ashwagandha-coder Apr 5, 2025
7ef2cd4
add new file and 165
Ashwagandha-coder Apr 6, 2025
c01c589
add 981
Ashwagandha-coder Apr 7, 2025
4a90ba1
add prod variant for 981
Ashwagandha-coder Apr 8, 2025
cb0f583
add 1309
Ashwagandha-coder Apr 8, 2025
966ff98
add 1309
Ashwagandha-coder Apr 8, 2025
f5d3e1e
add 3396
Ashwagandha-coder Apr 8, 2025
8adb8c0
add 242
Ashwagandha-coder Apr 8, 2025
c2b8615
add new files and prod variant's and change 347 problem
Ashwagandha-coder Apr 8, 2025
d4deeba
add 76 problem
Ashwagandha-coder Apr 9, 2025
7a8209c
add base structure for sliding window problems
Ashwagandha-coder Apr 9, 2025
b6d04a4
add 76 optimum solution
Ashwagandha-coder Apr 10, 2025
dca7656
add 2843
Ashwagandha-coder Apr 11, 2025
9441162
small change and save state
Ashwagandha-coder Apr 11, 2025
89ab3d1
add 68 text justification
Ashwagandha-coder Apr 12, 2025
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
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

549 changes: 0 additions & 549 deletions .idea/other.xml

This file was deleted.

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
57 changes: 44 additions & 13 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.contest


import com.github.contest.design.WordDictionary
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
import java.util.TreeMap


Expand All @@ -11,23 +13,52 @@ import java.util.TreeMap

fun main() {

fullJustify(
arrayOf(
"Science",
"is",
"what",
"we",
"understand",
"well",
"enough",
"to",
"explain",
"to",
"a",
"computer.",
"Art",
"is",
"everything",
"else",
"we",
"do"
), 20
).also {
println(
it
)
}

}

val wordDictionary = WordDictionary()
wordDictionary.addWord("bad")
wordDictionary.addWord("dad")
wordDictionary.addWord("mad")
wordDictionary.search("pad").also { println(it) } // return False
wordDictionary.search("bad").also { println(it) } // return True
wordDictionary.search(".ad").also { println(it) } // return True
wordDictionary.search("b..").also { println(it) }
fun subStrHashData() {
subStrHash("xxterzixjqrghqyeketqeynekvqhc", 15, 94, 4, 16).also { println(it) }
}

fun numberOfPowerfulIntData() {
val start = 141L
val finish = 148L
val limit = 9
val s = "9"

numberOfPowerfulInt(start, finish, limit, s).also { println(it) }
}


fun generateTesting() {
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
sequence.map { it * 2 }
.filter { it > 3 }
.filter { it > 2 }
.constrainOnce()
sequence.map { it * 2 }.filter { it > 3 }.filter { it > 2 }.constrainOnce()


}
Expand Down
37 changes: 37 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.contest.array


/**
* 1800. Maximum Ascending Subarray Sum
*/
Expand Down Expand Up @@ -140,6 +141,42 @@ fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray>
}.toTypedArray()
}

/**
* 2873. Maximum Value of an Ordered Triplet I
*/

fun maximumTripletValueI(nums: IntArray): Long {
var max = 0L
for (i in nums.indices) {
for (j in i + 1 until nums.size) {
for (k in j + 1 until nums.size) {
max = maxOf(max, ((nums[i] - nums[j]).toLong() * nums[k].toLong()))
}
}
}

return max
}


/**
* 2874. Maximum Value of an Ordered Triplet II
*/


fun maximumTripletValue(nums: IntArray): Long {
var maxi = Int.MIN_VALUE
var diff = 0
var res = 0L

for (i in nums.indices) {
maxi = maxOf(maxi, nums[i])
if (i >= 2) res = maxOf(res, (diff.toLong() * nums[i]))
if (i >= 1) diff = maxOf(diff, (maxi - nums[i]))
}

return res
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
package com.github.contest.bitManipulation

/**
* 1863. Sum of All Subset XOR Totals
*/

fun subsetXORSum(nums: IntArray): Int {
var totalXORSum = 0

fun calculateSubsetXOR(index: Int, currentXOR: Int) {
if (index == nums.size) {
totalXORSum += currentXOR
return
}

// Include the current element
calculateSubsetXOR(index + 1, currentXOR xor nums[index])

// Exclude the current element
calculateSubsetXOR(index + 1, currentXOR)
}

calculateSubsetXOR(0, 0)
return totalXORSum
}
35 changes: 35 additions & 0 deletions contest/src/main/java/com/github/contest/design/DesignLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,39 @@ class Trie() {
return curr
}

}

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

class TimeMap() {

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

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

fun get(key: String, timestamp: Int): String {
val list = store[key] ?: return emptyString()
var left = 0
var right = list.size - 1
var res = emptyString()

while (left <= right) {
val mid = (left + right) / 2
val (currentTimeStamp, currentValue) = list[mid]
if (currentTimeStamp == timestamp) return currentValue
if (currentTimeStamp < timestamp) {
left = mid + 1
res = currentValue
} else right = mid
}

return res
}

private fun emptyString() = ""

}
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
}
}
}

}
41 changes: 41 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,47 @@ fun longestSubsequence(arr: IntArray, difference: Int): Int {
return maxLen
}

/**
* 2140. Solving Questions With Brainpower
*/

fun mostPoints(questions: Array<IntArray>): Long {
return solve(questions, 0)
}

private fun solve(questions: Array<IntArray>, index: Int): Long {
if (index >= questions.size) {
return 0
}

val points = questions[index][0]
val brainpower = questions[index][1]
val take = points.toLong() + solve(questions, index + brainpower + 1)


val skip = solve(questions, index + 1)

return maxOf(take, skip)
}

fun mostPointsDp(questions: Array<IntArray>): Long {
val n = questions.size
val dp = LongArray(n + 1) { 0L }

for (i in n - 1 downTo 0) {
val points = questions[i][0]
val brainpower = questions[i][1]
val nextQuestion = i + brainpower + 1

val take = points.toLong() + (if (nextQuestion < n) dp[nextQuestion] else 0L)
val skip = dp[i + 1]

dp[i] = maxOf(take, skip)
}

return dp[0]
}




Expand Down
26 changes: 26 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpProdVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,30 @@ fun longestArithSeqLengthProdVariant(nums: IntArray): Int {
len
}
}
}

/**
* 2140. Solving Questions With Brainpower
* Prod Variant
*/


fun mostPointsProdVariant(questions: Array<IntArray>): Long {
val n = questions.size
val dp = LongArray(n + 1)
questions.forEachReversedIndexed { index, arr ->
val points = arr[0]
val brainpower = arr[1]
dp[index] = points + when {
brainpower + index + 1 < n -> dp[brainpower + index + 1]
else -> 0L
}
dp[index] = maxOf(dp[index], dp[index + 1])
}

return dp.first()
}

fun <T> Array<T>.forEachReversedIndexed(action: (Int, T) -> Unit) {
for (i in this.size - 1 downTo 0) action(i, this[i])
}
Loading