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
21 changes: 21 additions & 0 deletions .idea/appInsightsSettings.xml

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

1 change: 1 addition & 0 deletions .idea/gradle.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

12 changes: 8 additions & 4 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.github.contest


import com.github.contest.bitManipulation.hammingWeight
import com.github.contest.binaryTree.TreeNode
import com.github.contest.binaryTree.findTilt
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.strings.fullJustify

import java.util.TreeMap


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

fun main() {

val str: String? = "ghdirfghdi"
val root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)

findTilt(root).also { println(it) }

}

Expand Down Expand Up @@ -151,7 +155,7 @@ fun workWithTreeMap() {


fun IntArray.printArray() {
var s = when (this.size) {
val s = when (this.size) {
0 -> "[]"
1 -> "[${this[0]}]"
2 -> "[${this[0]}, ${this[1]}]"
Expand Down
39 changes: 39 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,43 @@ fun maximumDifference(nums: IntArray): Int {
return diff
}

/**
* 54. Spiral Matrix
*/

fun spiralOrder(matrix: Array<IntArray>): List<Int> = when (matrix.size) {
1 -> matrix[0].toList()
2 -> matrix[0].toList() + matrix[1].reversed()

else -> {

val res = mutableListOf<Int>()
var top = 0
var bottom = matrix.size - 1
var left = 0
var right = matrix[0].size - 1

while (top <= bottom && left <= right) {
for (i in left..right) res.add(matrix[top][i])
top++

for (i in top..bottom) res.add(matrix[i][right])
right--

if (top <= bottom) {
for (i in right downTo left) res.add(matrix[bottom][i])
bottom--
}

if (left <= right) {
for (i in bottom downTo top) res.add(matrix[i][left])
left++
}

}

res
}
}


Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.contest.binaryTree

import java.util.LinkedList
import kotlin.math.abs

/**
* 1261. Find Elements in a Contaminated Binary Tree
Expand Down Expand Up @@ -68,3 +69,69 @@ fun recoverPreorderTraversal(traversal: String): TreeNode? {
}
return path.firstOrNull()
}

/**
* 563. Binary Tree Tilt
*/

fun findTilt(root: TreeNode?): Int {
root ?: return 0
return dfs(root).second
}

private fun dfs(root: TreeNode?): Pair<Int, Int> {
if (root == null) return Pair(0, 0)

val left = dfs(root?.left)
val right = dfs(root?.right)
val newTilt = abs(left.first - right.first)

return Pair(root.`val` + left.first + right.first, newTilt + left.second + right.second)
}

/**
* 993. Cousins in Binary Tree
*/

fun isCousins(root: TreeNode?, x: Int, y: Int): Boolean {
root ?: return false
val queue = LinkedList<Pair<Int, TreeNode>>()
queue.offer(0 to root)

while (queue.isNotEmpty()) {
val size = queue.size
var parentX = 0
var parentY = 0
var isX = false
var isY = false

for (i in 0 until size) {
val (parentValue, node) = queue.poll()

if (node.`val` == x) {
parentX = parentValue
isX = true
}

if (node.`val` == y) {
parentY = parentValue
isY = true
}

if (isX && isY) {
if (parentX != parentY) return true
}

node.left?.let {
queue.offer(node.`val` to it)
}

node.right?.let {
queue.offer(node.`val` to it)
}

}
}

return false
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.contest.binaryTree

import java.util.LinkedList

/**
* 1261. Find Elements in a Contaminated Binary Tree
* Prod Variant
Expand All @@ -22,4 +24,41 @@ class FindElementProdVariant(root: TreeNode?) {

fun find(target: Int): Boolean = values.contains(target)

}

/**
* 993. Cousins in Binary Tree
* Prod Variant I
*/

fun isCousinsProdVariant(root: TreeNode?, x: Int, y: Int): Boolean {
root ?: return false
val queue = LinkedList<Pair<TreeNode, Int>>().apply {
offer(root to 0)
}

while (queue.isNotEmpty()) {
val size = queue.size
val levelNodes = buildList {
repeat(size) {
val (node, parentValue) = queue.poll()
add(node to parentValue)
node.left?.let {
queue.offer(it to node.`val`)
}
node.right?.let {
queue.offer(it to node.`val`)
}
}
}

val potentialValue = levelNodes.filter { it.first.`val` == x || it.first.`val` == y }

when (potentialValue.size) {
2 -> if (potentialValue.first().second != potentialValue.last().second) return true
1 -> return false
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,31 @@ fun maxDifference(s: String): Int {

return odd - even
}

/**
* 3541. Find Most Frequent Vowel and Consonant
*/

fun maxFreqSum(s: String): Int {
val freq = IntArray(26)
var maxVowel = 0
var maxConsonant = 0

for (char in s) {
val index = char - 'a'
freq[index]++
}

for (i in 0 until 26) {
val letter = (i + 'a'.code).toChar()
if (isVowel(letter)) maxVowel = maxOf(maxVowel, freq[i])
else maxConsonant = maxOf(maxConsonant, freq[i])
}

return maxVowel + maxConsonant
}

fun isVowel(char: Char) = when {
char in "aeiou" -> true
else -> false
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,8 @@ fun intToRomanProdVariant(num: Int): String {
*/

fun numEquivDominoPairsProdVariant(dominoes: Array<IntArray>): Int =
dominoes.map { (a, b) -> if (a <= b) a to b else b to a }
.groupingBy { it }
.eachCount()
.values
.sumOf { count -> count * (count - 1) / 2 }
dominoes.map { (a, b) -> if (a <= b) a to b else b to a }.groupingBy { it }
.eachCount().values.sumOf { count -> count * (count - 1) / 2 }

/**
* 2062. Count Vowel Substrings of a String
Expand All @@ -142,12 +139,10 @@ fun countVowelSubstringProdVariant(word: String): Int = word.indices.sumOf { i -
fun countVowelSubstringsProdVariantIII(word: String) = sequence {
word.forEachIndexed { i, _ ->
val seen = mutableSetOf<Char>()
word.asSequence().drop(i)
.takeWhile { it in "aeiou" }
.forEach { c ->
seen += c
if (seen.size == 5) yield(1)
}
word.asSequence().drop(i).takeWhile { it in "aeiou" }.forEach { c ->
seen += c
if (seen.size == 5) yield(1)
}
}
}.sum()

Expand All @@ -169,16 +164,48 @@ fun findLHSProdVariant(nums: IntArray): Int =
fun findLHSProdVariantII(nums: IntArray): Int {
val freq = nums.groupBy { it }.mapValues { it.value.size }

return freq.asSequence()
.flatMap { (key, _) ->
sequenceOf(
key to (key + 1), // Check next number
key to (key - 1) // Also check previous number (optional)
)
}
.filter { (_, adjacent) -> freq.containsKey(adjacent) }
.maxOfOrNull { (key, adjacent) -> freq[key]!! + freq[adjacent]!! }
?: 0
return freq.asSequence().flatMap { (key, _) ->
sequenceOf(
key to (key + 1), // Check next number
key to (key - 1) // Also check previous number (optional)
)
}.filter { (_, adjacent) -> freq.containsKey(adjacent) }
.maxOfOrNull { (key, adjacent) -> freq[key]!! + freq[adjacent]!! } ?: 0
}

/**
* 3541. Find Most Frequent Vowel and Consonant
* Prod Variant I
*/

fun maxFreqSumProdVariantI(s: String): Int {
val freq = s.groupingBy { it }.eachCount()
var maxVowel = 0
var maxConsonant = 0

freq.forEach {
if (isVowel(it.key)) maxVowel = maxOf(maxVowel, it.value)
else maxConsonant = maxOf(maxConsonant, it.value)
}

return maxConsonant + maxVowel
}

/**
* Prod Variant II
*/

fun maxFreqSumProdVariantII(s: String): Int =
s.groupingBy { it }
.eachCount()
.entries
.partition { (char, _) -> char.isVowel() }
.let { (vowelEntries, consonantEntries) ->
(vowelEntries.maxOfOrNull { it.value }
?: 0) + (consonantEntries.maxOfOrNull { it.value } ?: 0)
}


fun Char.isVowel() = this in "aeiou"


Loading