Skip to content

Commit 168da7a

Browse files
Merge pull request #153
add new problems 17.02
2 parents 7deefad + abb4736 commit 168da7a

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
11
package com.github.contest
22

3+
import com.github.contest.heap.numberGame
4+
35

46
/**
57
* Stand
68
*/
79

810
fun main() {
911

12+
numberGame(intArrayOf(5, 4, 2, 3))
13+
14+
15+
}
16+
1017

18+
fun IntArray.printArray() {
19+
var s = when (this.size) {
20+
0 -> "[]"
21+
1 -> "[${this[0]}]"
22+
2 -> "[${this[0]}, ${this[1]}]"
23+
else -> {
24+
var temp = "[${this[0]}, "
25+
for (i in 1 until this.size - 1) temp += "${this[i]}, "
26+
temp += "${this[this.size - 1]}]"
27+
temp
28+
}
29+
}
30+
println(s)
1131
}
1232

1333

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.github.contest.backtracking
2+
3+
/**
4+
* 1718. Construct the Lexicographically Largest Valid Sequence
5+
* Alternative Solution -
6+
*/
7+
8+
data class State(
9+
val index: Int,
10+
val num: Int,
11+
val arr: IntArray,
12+
val used: BooleanArray
13+
)
14+
15+
fun constructDistancedSequenceIterativeSolution(n: Int): IntArray {
16+
val result = IntArray(2 * n - 1) { 0 }
17+
val used = BooleanArray(n + 1) { false }
18+
val stack = ArrayDeque<State>()
19+
20+
stack.addLast(State(0, n, result, used))
21+
22+
while (stack.isNotEmpty()) {
23+
val currentState = stack.removeLast()
24+
val (index, num, arr, used) = currentState
25+
26+
if (index == arr.size) {
27+
return arr
28+
}
29+
30+
if (arr[index] != 0) {
31+
stack.addLast(State(index + 1, n, arr, used))
32+
continue
33+
}
34+
35+
for (currentNum in n downTo 1) {
36+
if (!used[currentNum] && isValid(currentNum, index, arr)) {
37+
val arrCopy = arr.copyOf()
38+
val usedCopy = used.copyOf()
39+
40+
usedCopy[currentNum] = true
41+
arrCopy[index] = currentNum
42+
if (currentNum != 1) {
43+
arrCopy[index + currentNum + 1] = currentNum
44+
}
45+
46+
stack.addLast(State(index + 1, n, arrCopy, usedCopy))
47+
}
48+
}
49+
}
50+
51+
return intArrayOf()
52+
}
53+
54+
55+
private fun isValid(num: Int, pos: Int, arr: IntArray): Boolean {
56+
if (arr[pos] != 0) return false
57+
if (num == 1) return true
58+
if (pos + num + 1 >= arr.size) return false
59+
if (arr[pos + num + 1] != 0) return false
60+
return true
61+
}

contest/src/main/java/com/github/contest/backtracking/BacktrackingLeetcode.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,27 @@ fun constructDistancedSequence(n: Int): IntArray {
8484
backtrack(0)
8585
return result
8686
}
87+
88+
/**
89+
* 1079. Letter Tile Possibilities
90+
*/
91+
92+
fun numTilePossibilities(tiles: String): Int {
93+
val count = IntArray(26)
94+
for (c in tiles) {
95+
count[c - 'A']++
96+
}
97+
return dfs(count)
98+
}
99+
100+
private fun dfs(count: IntArray): Int {
101+
var sum = 0
102+
for (i in 0 until 26) {
103+
if (count[i] == 0) continue
104+
sum++
105+
count[i]--
106+
sum += dfs(count)
107+
count[i]++
108+
}
109+
return sum
110+
}

contest/src/main/java/com/github/contest/heap/HeapLeetcode.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,46 @@ private fun PriorityQueue<Int>.sum(): Int {
198198
while (this.isNotEmpty()) sum += this.poll()
199199
return sum
200200
}
201+
202+
/**
203+
* 2974. Minimum Number Game
204+
*/
205+
206+
fun numberGame(nums: IntArray): IntArray {
207+
val heap = PriorityQueue<Int> { a, b -> a - b }
208+
for (num in nums) heap.offer(num)
209+
var i = 0
210+
while (heap.isNotEmpty()) {
211+
val a = heap.poll()
212+
val b = heap.poll()
213+
nums[i] = b
214+
nums[i + 1] = a
215+
i += 2
216+
}
217+
218+
return nums
219+
}
220+
221+
/**
222+
* 2335. Minimum Amount of Time to Fill Cups
223+
*/
224+
225+
fun fillCups(amount: IntArray): Int {
226+
val heap = PriorityQueue<Int> { a, b -> b - a }
227+
var counter = 0
228+
for (water in amount) if (water != 0) heap.offer(water)
229+
while (heap.isNotEmpty()) {
230+
if (heap.size >= 2) {
231+
val one = heap.poll()
232+
val two = heap.poll()
233+
if (one - 1 != 0) heap.offer(one - 1)
234+
if (two - 1 != 0) heap.offer(two - 1)
235+
} else {
236+
val single = heap.poll()
237+
if (single - 1 != 0) heap.offer(single - 1)
238+
}
239+
counter++
240+
}
241+
242+
return counter
243+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.contest.sorting
2+
3+
import kotlin.random.Random
4+
5+
/**
6+
* The BogoSort
7+
*/
8+
9+
class BogoSort {
10+
fun bogoSort(list: MutableList<Int>) {
11+
while (!isSorted(list)) {
12+
shuffle(list)
13+
}
14+
}
15+
16+
private fun isSorted(list: List<Int>): Boolean {
17+
for (i in 0 until list.size - 1) {
18+
if (list[i] > list[i + 1]) {
19+
return false
20+
}
21+
}
22+
return true
23+
}
24+
25+
private fun shuffle(list: MutableList<Int>) {
26+
for (i in list.indices) {
27+
val j = Random.nextInt(list.size)
28+
list.swap(i, j)
29+
}
30+
}
31+
32+
private fun MutableList<Int>.swap(from: Int, to: Int) {
33+
val temp = this[from]
34+
this[from] = this[to]
35+
this[to] = temp
36+
}
37+
}
38+
39+

0 commit comments

Comments
 (0)