Skip to content

Improved tasks 2471-3478 #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 14, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.ArrayDeque
*/
class Solution {
fun minimumOperations(root: TreeNode): Int {
val q = ArrayDeque<TreeNode?>()
val q = ArrayDeque<TreeNode>()
var count = 0
if (root.left != null && root.right != null && root.left!!.`val` > root.right!!.`val`) {
count++
Expand Down Expand Up @@ -55,14 +55,14 @@ class Solution {
sorted[i] = list[i]
}
sorted.sort()
val ind: MutableMap<Int, Int?> = HashMap()
val ind: MutableMap<Int, Int> = HashMap()
for (i in list.indices) {
ind[list[i]] = i
}
for (i in list.indices) {
if (list[i] != sorted[i]) {
swaps++
ind[list[i]] = ind[sorted[i]]
ind[list[i]] = ind[sorted[i]]!!
list[ind[sorted[i]]!!] = list[i]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Solution {
adj[edge[1]].add(edge[0])
}
for (i in 0 until n) {
dfs(adj, HashSet<Int?>(), i)
dfs(adj, HashSet<Int>(), i)
}
return if (min == Int.MAX_VALUE) -1 else min
}

private fun dfs(adj: List<MutableList<Int>>, set: HashSet<Int?>, node: Int) {
private fun dfs(adj: List<MutableList<Int>>, set: HashSet<Int>, node: Int) {
val queue: Queue<IntArray> = LinkedList()
set.add(node)
queue.add(intArrayOf(node, node))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlin.math.max

class Solution {
fun maximumSumSubsequence(nums: IntArray, queries: Array<IntArray>): Int {
val tree: Array<LongArray?> = build(nums)
val tree: Array<LongArray> = build(nums)
var result: Long = 0
for (i in queries.indices) {
result += set(tree, queries[i][0], queries[i][1])
Expand All @@ -23,103 +23,103 @@ class Solution {
private const val NN = 3
private const val MOD = 1000000007

private fun build(nums: IntArray): Array<LongArray?> {
private fun build(nums: IntArray): Array<LongArray> {
val len = nums.size
var size = 1
while (size < len) {
size = size shl 1
}
val tree = Array<LongArray?>(size * 2) { LongArray(4) }
val tree = Array<LongArray>(size * 2) { LongArray(4) }
for (i in 0 until len) {
tree[size + i]!![YY] = nums[i].toLong()
tree[size + i][YY] = nums[i].toLong()
}
for (i in size - 1 downTo 1) {
tree[i]!![YY] = max(
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]),
tree[i][YY] = max(
(tree[2 * i][YY] + tree[2 * i + 1][NY]),
(
tree[2 * i]!![YN] + max(
tree[2 * i + 1]!![YY],
tree[2 * i + 1]!![NY],
tree[2 * i][YN] + max(
tree[2 * i + 1][YY],
tree[2 * i + 1][NY],
)
),
)
tree[i]!![YN] = max(
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]),
tree[i][YN] = max(
(tree[2 * i][YY] + tree[2 * i + 1][NN]),
(
tree[2 * i]!![YN] + max(
tree[2 * i + 1]!![YN],
tree[2 * i + 1]!![NN],
tree[2 * i][YN] + max(
tree[2 * i + 1][YN],
tree[2 * i + 1][NN],
)
),
)
tree[i]!![NY] = max(
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]),
tree[i][NY] = max(
(tree[2 * i][NY] + tree[2 * i + 1][NY]),
(
tree[2 * i]!![NN] + max(
tree[2 * i + 1]!![YY],
tree[2 * i + 1]!![NY],
tree[2 * i][NN] + max(
tree[2 * i + 1][YY],
tree[2 * i + 1][NY],
)
),
)
tree[i]!![NN] = max(
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]),
tree[i][NN] = max(
(tree[2 * i][NY] + tree[2 * i + 1][NN]),
(
tree[2 * i]!![NN] + max(
tree[2 * i + 1]!![YN],
tree[2 * i + 1]!![NN],
tree[2 * i][NN] + max(
tree[2 * i + 1][YN],
tree[2 * i + 1][NN],
)
),
)
}
return tree
}

private fun set(tree: Array<LongArray?>, idx: Int, `val`: Int): Long {
private fun set(tree: Array<LongArray>, idx: Int, `val`: Int): Long {
val size = tree.size / 2
tree[size + idx]!![YY] = `val`.toLong()
tree[size + idx][YY] = `val`.toLong()
var i = (size + idx) / 2
while (i > 0) {
tree[i]!![YY] = max(
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NY]),
tree[i][YY] = max(
(tree[2 * i][YY] + tree[2 * i + 1][NY]),
(
tree[2 * i]!![YN] + max(
tree[2 * i + 1]!![YY],
tree[2 * i + 1]!![NY],
tree[2 * i][YN] + max(
tree[2 * i + 1][YY],
tree[2 * i + 1][NY],
)
),
)
tree[i]!![YN] = max(
(tree[2 * i]!![YY] + tree[2 * i + 1]!![NN]),
tree[i][YN] = max(
(tree[2 * i][YY] + tree[2 * i + 1][NN]),
(
tree[2 * i]!![YN] + max(
tree[2 * i + 1]!![YN],
tree[2 * i + 1]!![NN],
tree[2 * i][YN] + max(
tree[2 * i + 1][YN],
tree[2 * i + 1][NN],
)
),
)
tree[i]!![NY] = max(
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NY]),
tree[i][NY] = max(
(tree[2 * i][NY] + tree[2 * i + 1][NY]),
(
tree[2 * i]!![NN] + max(
tree[2 * i + 1]!![YY],
tree[2 * i + 1]!![NY],
tree[2 * i][NN] + max(
tree[2 * i + 1][YY],
tree[2 * i + 1][NY],
)
),
)
tree[i]!![NN] = max(
(tree[2 * i]!![NY] + tree[2 * i + 1]!![NN]),
tree[i][NN] = max(
(tree[2 * i][NY] + tree[2 * i + 1][NN]),
(
tree[2 * i]!![NN] + max(
tree[2 * i + 1]!![YN],
tree[2 * i + 1]!![NN],
tree[2 * i][NN] + max(
tree[2 * i + 1][YN],
tree[2 * i + 1][NN],
)
),
)
i /= 2
}
return max(
tree[1]!![YY],
max(tree[1]!![YN], max(tree[1]!![NY], tree[1]!![NN])),
tree[1][YY],
max(tree[1][YN], max(tree[1][NY], tree[1][NN])),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Solution {
}

// Initializes the alternating groups using the colors array.
private fun initializeGroups(colors: IntArray, groups: TreeMap<Int?, Int?>) {
private fun initializeGroups(colors: IntArray, groups: TreeMap<Int, Int>) {
val n = colors.size
var i = 0
while (i < n) {
Expand All @@ -95,7 +95,7 @@ class Solution {

// Processes a type 1 query: returns the number of alternating groups
// of at least the given size.
private fun processQueryType1(colors: IntArray, groups: TreeMap<Int?, Int?>, groupSize: Int): Int {
private fun processQueryType1(colors: IntArray, groups: TreeMap<Int, Int>, groupSize: Int): Int {
var ans = qry(groupSize)
val firstGroup = groups.firstEntry()
val lastGroup = groups.lastEntry()
Expand All @@ -114,7 +114,7 @@ class Solution {
// Processes a type 2 query: updates the color at index x and adjusts groups.
private fun processQueryType2(
colors: IntArray,
groups: TreeMap<Int?, Int?>,
groups: TreeMap<Int, Int>,
x: Int,
newColor: Int,
) {
Expand Down Expand Up @@ -175,10 +175,10 @@ class Solution {
}

// Main function to handle queries on alternating groups.
fun numberOfAlternatingGroups(colors: IntArray, queries: Array<IntArray>): MutableList<Int?> {
val groups = TreeMap<Int?, Int?>()
fun numberOfAlternatingGroups(colors: IntArray, queries: Array<IntArray>): MutableList<Int> {
val groups = TreeMap<Int, Int>()
val n = colors.size
val results: MutableList<Int?> = ArrayList<Int?>()
val results: MutableList<Int> = ArrayList<Int>()
// Initialize alternating groups.
initializeGroups(colors, groups)
// Process each query.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class Solution {
val m = grid[0].size
val dr = intArrayOf(0, 0, 1, -1)
val dc = intArrayOf(1, -1, 0, 0)
val visited = Array<Array<BooleanArray>?>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
val visited = Array<Array<BooleanArray>>(n) { Array<BooleanArray>(m) { BooleanArray(health + 1) } }
val bfs: Queue<IntArray?> = LinkedList<IntArray>()
bfs.add(intArrayOf(0, 0, health - grid[0][0]))
visited[0]!![0][health - grid[0][0]] = true
visited[0][0][health - grid[0][0]] = true
while (bfs.isNotEmpty()) {
var size = bfs.size
while (size-- > 0) {
Expand All @@ -32,8 +32,8 @@ class Solution {
val nc = c + dc[k]
if (isValidMove(nr, nc, n, m)) {
val nh: Int = h - grid[nr][nc]
if (nh >= 0 && !visited[nr]!![nc][nh]) {
visited[nr]!![nc][nh] = true
if (nh >= 0 && !visited[nr][nc][nh]) {
visited[nr][nc][nh] = true
bfs.add(intArrayOf(nr, nc, nh))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class Solution {
}

private fun longestIncreasingLength(array: List<IntArray>): Int {
val list: MutableList<Int?> = ArrayList<Int?>()
val list: MutableList<Int> = ArrayList<Int>()
for (pair in array) {
val m = list.size
if (m == 0 || list[m - 1]!! < pair[1]) {
if (m == 0 || list[m - 1] < pair[1]) {
list.add(pair[1])
} else {
val idx = binarySearch(list, pair[1])
Expand All @@ -52,15 +52,15 @@ class Solution {
return list.size
}

private fun binarySearch(list: List<Int?>, target: Int): Int {
private fun binarySearch(list: List<Int>, target: Int): Int {
val n = list.size
var left = 0
var right = n - 1
while (left < right) {
val mid = (left + right) / 2
if (list[mid] == target) {
return mid
} else if (list[mid]!! > target) {
} else if (list[mid] > target) {
right = mid
} else {
left = mid + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class Solution {
private fun countOfSubstringHavingAtleastXConsonants(word: String, k: Int): Long {
var start = 0
var end = 0
val vowels: MutableSet<Char?> = HashSet<Char?>()
val vowels: MutableSet<Char> = HashSet<Char>()
vowels.add('a')
vowels.add('e')
vowels.add('i')
vowels.add('o')
vowels.add('u')
var consonants = 0
val map: MutableMap<Char?, Int?> = HashMap<Char?, Int?>()
val map: MutableMap<Char, Int> = HashMap<Char, Int>()
var res: Long = 0
while (end < word.length) {
val ch = word[end]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Solution {
): Array<IntArray> {
var st = st
var res: Array<IntArray>
val al = ArrayList<Int?>()
val al = ArrayList<Int>()
var f = true
seen[st] = true
al.add(st)
Expand All @@ -86,7 +86,7 @@ class Solution {
}
res = Array<IntArray>(n / al.size) { IntArray(al.size) }
for (i in res[0].indices) {
res[0][i] = al[i]!!
res[0][i] = al[i]
}
for (i in 1 until res.size) {
for (j in res[0].indices) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Solution {
val n = nums.size
val ans = IntArray(n - k + 1)
for (i in 0 until n - k + 1) {
val map = HashMap<Int?, Int?>()
val map = HashMap<Int, Int>()
val pq =
PriorityQueue<Pair>(
Comparator { a: Pair, b: Pair ->
Expand All @@ -33,10 +33,10 @@ class Solution {
},
)
for (j in i until i + k) {
map.put(nums[j], map.getOrDefault(nums[j], 0)!! + 1)
map.put(nums[j], map.getOrDefault(nums[j], 0) + 1)
}
for (entry in map.entries) {
pq.add(Pair(entry.key!!, entry.value!!))
pq.add(Pair(entry.key, entry.value))
}
var count = x
var sum = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.util.Queue
* }
*/
class Solution {
private val pq: Queue<Int?> = PriorityQueue<Int?>()
private val pq: Queue<Int> = PriorityQueue<Int>()

fun kthLargestPerfectSubtree(root: TreeNode?, k: Int): Int {
dfs(root, k)
Expand Down
Loading