Skip to content

Commit cc76341

Browse files
Merge pull request #213
add new problems 15.09
2 parents c9eb5f2 + 190ed07 commit cc76341

File tree

15 files changed

+506
-153
lines changed

15 files changed

+506
-153
lines changed

.idea/appInsightsSettings.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

33

4-
import com.github.contest.bitManipulation.hammingWeight
4+
import com.github.contest.binaryTree.TreeNode
5+
import com.github.contest.binaryTree.findTilt
56
import com.github.contest.math.numberOfPowerfulInt
67
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
78
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
89
import com.github.contest.strings.fullJustify
9-
1010
import java.util.TreeMap
1111

1212

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

1717
fun main() {
1818

19-
val str: String? = "ghdirfghdi"
19+
val root = TreeNode(1)
20+
root.left = TreeNode(2)
21+
root.right = TreeNode(3)
22+
23+
findTilt(root).also { println(it) }
2024

2125
}
2226

@@ -151,7 +155,7 @@ fun workWithTreeMap() {
151155

152156

153157
fun IntArray.printArray() {
154-
var s = when (this.size) {
158+
val s = when (this.size) {
155159
0 -> "[]"
156160
1 -> "[${this[0]}]"
157161
2 -> "[${this[0]}, ${this[1]}]"

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,43 @@ fun maximumDifference(nums: IntArray): Int {
439439
return diff
440440
}
441441

442+
/**
443+
* 54. Spiral Matrix
444+
*/
445+
446+
fun spiralOrder(matrix: Array<IntArray>): List<Int> = when (matrix.size) {
447+
1 -> matrix[0].toList()
448+
2 -> matrix[0].toList() + matrix[1].reversed()
449+
450+
else -> {
451+
452+
val res = mutableListOf<Int>()
453+
var top = 0
454+
var bottom = matrix.size - 1
455+
var left = 0
456+
var right = matrix[0].size - 1
457+
458+
while (top <= bottom && left <= right) {
459+
for (i in left..right) res.add(matrix[top][i])
460+
top++
461+
462+
for (i in top..bottom) res.add(matrix[i][right])
463+
right--
464+
465+
if (top <= bottom) {
466+
for (i in right downTo left) res.add(matrix[bottom][i])
467+
bottom--
468+
}
469+
470+
if (left <= right) {
471+
for (i in bottom downTo top) res.add(matrix[i][left])
472+
left++
473+
}
474+
475+
}
476+
477+
res
478+
}
479+
}
480+
442481

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeLeetcode.kt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.contest.binaryTree
22

33
import java.util.LinkedList
4+
import kotlin.math.abs
45

56
/**
67
* 1261. Find Elements in a Contaminated Binary Tree
@@ -68,3 +69,69 @@ fun recoverPreorderTraversal(traversal: String): TreeNode? {
6869
}
6970
return path.firstOrNull()
7071
}
72+
73+
/**
74+
* 563. Binary Tree Tilt
75+
*/
76+
77+
fun findTilt(root: TreeNode?): Int {
78+
root ?: return 0
79+
return dfs(root).second
80+
}
81+
82+
private fun dfs(root: TreeNode?): Pair<Int, Int> {
83+
if (root == null) return Pair(0, 0)
84+
85+
val left = dfs(root?.left)
86+
val right = dfs(root?.right)
87+
val newTilt = abs(left.first - right.first)
88+
89+
return Pair(root.`val` + left.first + right.first, newTilt + left.second + right.second)
90+
}
91+
92+
/**
93+
* 993. Cousins in Binary Tree
94+
*/
95+
96+
fun isCousins(root: TreeNode?, x: Int, y: Int): Boolean {
97+
root ?: return false
98+
val queue = LinkedList<Pair<Int, TreeNode>>()
99+
queue.offer(0 to root)
100+
101+
while (queue.isNotEmpty()) {
102+
val size = queue.size
103+
var parentX = 0
104+
var parentY = 0
105+
var isX = false
106+
var isY = false
107+
108+
for (i in 0 until size) {
109+
val (parentValue, node) = queue.poll()
110+
111+
if (node.`val` == x) {
112+
parentX = parentValue
113+
isX = true
114+
}
115+
116+
if (node.`val` == y) {
117+
parentY = parentValue
118+
isY = true
119+
}
120+
121+
if (isX && isY) {
122+
if (parentX != parentY) return true
123+
}
124+
125+
node.left?.let {
126+
queue.offer(node.`val` to it)
127+
}
128+
129+
node.right?.let {
130+
queue.offer(node.`val` to it)
131+
}
132+
133+
}
134+
}
135+
136+
return false
137+
}

contest/src/main/java/com/github/contest/binaryTree/BinaryTreeProdVariant.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.contest.binaryTree
22

3+
import java.util.LinkedList
4+
35
/**
46
* 1261. Find Elements in a Contaminated Binary Tree
57
* Prod Variant
@@ -22,4 +24,41 @@ class FindElementProdVariant(root: TreeNode?) {
2224

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

27+
}
28+
29+
/**
30+
* 993. Cousins in Binary Tree
31+
* Prod Variant I
32+
*/
33+
34+
fun isCousinsProdVariant(root: TreeNode?, x: Int, y: Int): Boolean {
35+
root ?: return false
36+
val queue = LinkedList<Pair<TreeNode, Int>>().apply {
37+
offer(root to 0)
38+
}
39+
40+
while (queue.isNotEmpty()) {
41+
val size = queue.size
42+
val levelNodes = buildList {
43+
repeat(size) {
44+
val (node, parentValue) = queue.poll()
45+
add(node to parentValue)
46+
node.left?.let {
47+
queue.offer(it to node.`val`)
48+
}
49+
node.right?.let {
50+
queue.offer(it to node.`val`)
51+
}
52+
}
53+
}
54+
55+
val potentialValue = levelNodes.filter { it.first.`val` == x || it.first.`val` == y }
56+
57+
when (potentialValue.size) {
58+
2 -> if (potentialValue.first().second != potentialValue.last().second) return true
59+
1 -> return false
60+
}
61+
}
62+
63+
return false
2564
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,31 @@ fun maxDifference(s: String): Int {
634634

635635
return odd - even
636636
}
637+
638+
/**
639+
* 3541. Find Most Frequent Vowel and Consonant
640+
*/
641+
642+
fun maxFreqSum(s: String): Int {
643+
val freq = IntArray(26)
644+
var maxVowel = 0
645+
var maxConsonant = 0
646+
647+
for (char in s) {
648+
val index = char - 'a'
649+
freq[index]++
650+
}
651+
652+
for (i in 0 until 26) {
653+
val letter = (i + 'a'.code).toChar()
654+
if (isVowel(letter)) maxVowel = maxOf(maxVowel, freq[i])
655+
else maxConsonant = maxOf(maxConsonant, freq[i])
656+
}
657+
658+
return maxVowel + maxConsonant
659+
}
660+
661+
fun isVowel(char: Char) = when {
662+
char in "aeiou" -> true
663+
else -> false
664+
}

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

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,8 @@ fun intToRomanProdVariant(num: Int): String {
114114
*/
115115

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

123120
/**
124121
* 2062. Count Vowel Substrings of a String
@@ -142,12 +139,10 @@ fun countVowelSubstringProdVariant(word: String): Int = word.indices.sumOf { i -
142139
fun countVowelSubstringsProdVariantIII(word: String) = sequence {
143140
word.forEachIndexed { i, _ ->
144141
val seen = mutableSetOf<Char>()
145-
word.asSequence().drop(i)
146-
.takeWhile { it in "aeiou" }
147-
.forEach { c ->
148-
seen += c
149-
if (seen.size == 5) yield(1)
150-
}
142+
word.asSequence().drop(i).takeWhile { it in "aeiou" }.forEach { c ->
143+
seen += c
144+
if (seen.size == 5) yield(1)
145+
}
151146
}
152147
}.sum()
153148

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

172-
return freq.asSequence()
173-
.flatMap { (key, _) ->
174-
sequenceOf(
175-
key to (key + 1), // Check next number
176-
key to (key - 1) // Also check previous number (optional)
177-
)
178-
}
179-
.filter { (_, adjacent) -> freq.containsKey(adjacent) }
180-
.maxOfOrNull { (key, adjacent) -> freq[key]!! + freq[adjacent]!! }
181-
?: 0
167+
return freq.asSequence().flatMap { (key, _) ->
168+
sequenceOf(
169+
key to (key + 1), // Check next number
170+
key to (key - 1) // Also check previous number (optional)
171+
)
172+
}.filter { (_, adjacent) -> freq.containsKey(adjacent) }
173+
.maxOfOrNull { (key, adjacent) -> freq[key]!! + freq[adjacent]!! } ?: 0
174+
}
175+
176+
/**
177+
* 3541. Find Most Frequent Vowel and Consonant
178+
* Prod Variant I
179+
*/
180+
181+
fun maxFreqSumProdVariantI(s: String): Int {
182+
val freq = s.groupingBy { it }.eachCount()
183+
var maxVowel = 0
184+
var maxConsonant = 0
185+
186+
freq.forEach {
187+
if (isVowel(it.key)) maxVowel = maxOf(maxVowel, it.value)
188+
else maxConsonant = maxOf(maxConsonant, it.value)
189+
}
190+
191+
return maxConsonant + maxVowel
182192
}
183193

194+
/**
195+
* Prod Variant II
196+
*/
197+
198+
fun maxFreqSumProdVariantII(s: String): Int =
199+
s.groupingBy { it }
200+
.eachCount()
201+
.entries
202+
.partition { (char, _) -> char.isVowel() }
203+
.let { (vowelEntries, consonantEntries) ->
204+
(vowelEntries.maxOfOrNull { it.value }
205+
?: 0) + (consonantEntries.maxOfOrNull { it.value } ?: 0)
206+
}
207+
208+
209+
fun Char.isVowel() = this in "aeiou"
210+
184211

0 commit comments

Comments
 (0)