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
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,8 +1,8 @@
package com.github.contest


import com.github.contest.binarySearch.twoSum
import com.github.contest.hashTable.countGoodAlternativeSolution
import com.github.contest.hashTable.intToRoman
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
Expand All @@ -15,7 +15,7 @@ import java.util.TreeMap

fun main() {

twoSum(intArrayOf(-1, 0), -1).printArray()
intToRoman(3799).also { println(it) }


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,70 @@ fun countPairs(nums: IntArray, k: Int): Int {
}

return count
}
}

/**
* 2799. Count Complete SubArrays in an Array
*/

fun countCompleteSubArrays(nums: IntArray): Int {
if (nums.size == 1) return 1

var count = 0
val distinct = distinctElementsOfArray(nums)
var lastIndex = nums.size - 1

for (i in nums.indices) {
var j = i
val set = mutableSetOf<Int>()

while (j < nums.size && set.size != distinct) {
set.add(nums[j])
j++
}
j--
if (set.size == distinct) count += (lastIndex - j) + 1
}

return count

}

private fun distinctElementsOfArray(nums: IntArray): Int {
val set = mutableSetOf<Int>()
for (num in nums) set.add(num)
return set.size
}

/**
* 12. Integer to Roman
*/

fun intToRoman(num: Int): String {
val values = listOf(
1000 to "M",
900 to "CM",
500 to "D",
400 to "CD",
100 to "C",
90 to "XC",
50 to "L",
40 to "XL",
10 to "X",
9 to "IX",
5 to "V",
4 to "IV",
1 to "I"
)
var n = num
val result = StringBuilder()
for ((value, symbol) in values) {
while (n >= value) {
result.append(symbol)
n -= value
}
}
return result.toString()
}


Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,65 @@ fun findMissingAndRepeatedValuesProdVariant(grid: Array<IntArray>): IntArray {
}


/**
* 2799. Count Complete Subarrays in an Array
* Prod Variant
*/

fun countCompleteSubArrayProdVariant(nums: IntArray): Int {
val unique = nums.distinct().size
var count = 0
val freq = mutableMapOf<Int, Int>()
var left = 0
val n = nums.size

nums.forEachIndexed { right, num ->
freq[num] = freq.getOrDefault(num, 0) + 1

while (freq.size == unique) {
count += n - right
val delete = nums[left]
freq[delete] = freq.getOrDefault(delete, 0) - 1
if (freq[delete] == 0) freq.remove(delete)
left++
}
}

return count
}

/**
* 12. Integer to Roman
* Prod Variant
*/

fun intToRomanProdVariant(num: Int): String {
val values = mapOf(
1000 to "M",
900 to "CM",
500 to "D",
400 to "CD",
100 to "C",
90 to "XC",
50 to "L",
40 to "XL",
10 to "X",
9 to "IX",
5 to "V",
4 to "IV",
1 to "I"
)
var n = num
return when {
values.contains(num) -> values.getOrDefault(num, "")
else -> buildString {
values.forEach { (value, symbol) ->
while (n >= value) {
append(symbol)
n -= value
}
}
}
}
}

25 changes: 25 additions & 0 deletions contest/src/main/java/com/github/contest/math/MathLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ fun countSymmetricIntegers(low: Int, high: Int): Int {
return count
}

/**
* 1399. Count Largest Group
*/

fun countLargestGroup(n: Int): Int {
val freq = IntArray(40)
var maxCount = 0

for (i in 1..n) {
var sum = 0
var x = i
while (x > 0) {
sum += x % 10
x /= 10
}
freq[sum]++
maxCount = maxOf(maxCount, freq[sum])
}

var groupCount = 0
for (count in freq) if (count == maxCount) groupCount++

return groupCount
}

/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ private fun compress(input: String): String {
return res
}

/**
* 58. Length of Last Word
*/


fun lengthOfLastWord(s: String): Int {
var i = s.length - 1
var size = 0

while (i >= 0 && s[i] == ' ') i--

while (i >= 0 && s[i] != ' ') {
size++
i--
}

return size
}


/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ fun smallestStringProdVariant(s: String): String {
}
String(modifiedChars)
}
}
}

/**
* 58. Length of Last Word
* Prod Variant
*/

fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0