Skip to content

Commit 0b23246

Browse files
Merge pull request #180
add new problems 25.04
2 parents 5013f5f + 553a57d commit 0b23246

File tree

6 files changed

+184
-4
lines changed

6 files changed

+184
-4
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.github.contest
22

33

4-
import com.github.contest.binarySearch.twoSum
54
import com.github.contest.hashTable.countGoodAlternativeSolution
5+
import com.github.contest.hashTable.intToRoman
66
import com.github.contest.math.numberOfPowerfulInt
77
import com.github.contest.strings.fullJustify
88
import com.github.contest.strings.subStrHash
@@ -15,7 +15,7 @@ import java.util.TreeMap
1515

1616
fun main() {
1717

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

2020

2121
}

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,70 @@ fun countPairs(nums: IntArray, k: Int): Int {
411411
}
412412

413413
return count
414-
}
414+
}
415+
416+
/**
417+
* 2799. Count Complete SubArrays in an Array
418+
*/
419+
420+
fun countCompleteSubArrays(nums: IntArray): Int {
421+
if (nums.size == 1) return 1
422+
423+
var count = 0
424+
val distinct = distinctElementsOfArray(nums)
425+
var lastIndex = nums.size - 1
426+
427+
for (i in nums.indices) {
428+
var j = i
429+
val set = mutableSetOf<Int>()
430+
431+
while (j < nums.size && set.size != distinct) {
432+
set.add(nums[j])
433+
j++
434+
}
435+
j--
436+
if (set.size == distinct) count += (lastIndex - j) + 1
437+
}
438+
439+
return count
440+
441+
}
442+
443+
private fun distinctElementsOfArray(nums: IntArray): Int {
444+
val set = mutableSetOf<Int>()
445+
for (num in nums) set.add(num)
446+
return set.size
447+
}
448+
449+
/**
450+
* 12. Integer to Roman
451+
*/
452+
453+
fun intToRoman(num: Int): String {
454+
val values = listOf(
455+
1000 to "M",
456+
900 to "CM",
457+
500 to "D",
458+
400 to "CD",
459+
100 to "C",
460+
90 to "XC",
461+
50 to "L",
462+
40 to "XL",
463+
10 to "X",
464+
9 to "IX",
465+
5 to "V",
466+
4 to "IV",
467+
1 to "I"
468+
)
469+
var n = num
470+
val result = StringBuilder()
471+
for ((value, symbol) in values) {
472+
while (n >= value) {
473+
result.append(symbol)
474+
n -= value
475+
}
476+
}
477+
return result.toString()
478+
}
479+
480+

contest/src/main/java/com/github/contest/hashTable/HashTableProdVariant.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,65 @@ fun findMissingAndRepeatedValuesProdVariant(grid: Array<IntArray>): IntArray {
4848
}
4949

5050

51+
/**
52+
* 2799. Count Complete Subarrays in an Array
53+
* Prod Variant
54+
*/
55+
56+
fun countCompleteSubArrayProdVariant(nums: IntArray): Int {
57+
val unique = nums.distinct().size
58+
var count = 0
59+
val freq = mutableMapOf<Int, Int>()
60+
var left = 0
61+
val n = nums.size
62+
63+
nums.forEachIndexed { right, num ->
64+
freq[num] = freq.getOrDefault(num, 0) + 1
65+
66+
while (freq.size == unique) {
67+
count += n - right
68+
val delete = nums[left]
69+
freq[delete] = freq.getOrDefault(delete, 0) - 1
70+
if (freq[delete] == 0) freq.remove(delete)
71+
left++
72+
}
73+
}
74+
75+
return count
76+
}
77+
78+
/**
79+
* 12. Integer to Roman
80+
* Prod Variant
81+
*/
82+
83+
fun intToRomanProdVariant(num: Int): String {
84+
val values = mapOf(
85+
1000 to "M",
86+
900 to "CM",
87+
500 to "D",
88+
400 to "CD",
89+
100 to "C",
90+
90 to "XC",
91+
50 to "L",
92+
40 to "XL",
93+
10 to "X",
94+
9 to "IX",
95+
5 to "V",
96+
4 to "IV",
97+
1 to "I"
98+
)
99+
var n = num
100+
return when {
101+
values.contains(num) -> values.getOrDefault(num, "")
102+
else -> buildString {
103+
values.forEach { (value, symbol) ->
104+
while (n >= value) {
105+
append(symbol)
106+
n -= value
107+
}
108+
}
109+
}
110+
}
111+
}
112+

contest/src/main/java/com/github/contest/math/MathLeetcode.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ fun countSymmetricIntegers(low: Int, high: Int): Int {
132132
return count
133133
}
134134

135+
/**
136+
* 1399. Count Largest Group
137+
*/
138+
139+
fun countLargestGroup(n: Int): Int {
140+
val freq = IntArray(40)
141+
var maxCount = 0
142+
143+
for (i in 1..n) {
144+
var sum = 0
145+
var x = i
146+
while (x > 0) {
147+
sum += x % 10
148+
x /= 10
149+
}
150+
freq[sum]++
151+
maxCount = maxOf(maxCount, freq[sum])
152+
}
153+
154+
var groupCount = 0
155+
for (count in freq) if (count == maxCount) groupCount++
156+
157+
return groupCount
158+
}
159+
135160
/**
136161
*
137162
*/

contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,26 @@ private fun compress(input: String): String {
369369
return res
370370
}
371371

372+
/**
373+
* 58. Length of Last Word
374+
*/
375+
376+
377+
fun lengthOfLastWord(s: String): Int {
378+
var i = s.length - 1
379+
var size = 0
380+
381+
while (i >= 0 && s[i] == ' ') i--
382+
383+
while (i >= 0 && s[i] != ' ') {
384+
size++
385+
i--
386+
}
387+
388+
return size
389+
}
390+
391+
372392
/**
373393
*
374394
*/

contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ fun smallestStringProdVariant(s: String): String {
5858
}
5959
String(modifiedChars)
6060
}
61-
}
61+
}
62+
63+
/**
64+
* 58. Length of Last Word
65+
* Prod Variant
66+
*/
67+
68+
fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0

0 commit comments

Comments
 (0)