Skip to content

Commit 62b72ae

Browse files
committed
estudando greedy algorithms e ordenacao em kotlin
1 parent 3f2ce11 commit 62b72ae

File tree

9 files changed

+165
-60
lines changed

9 files changed

+165
-60
lines changed

kotlin/LukeBalance/.idea/workspace.xml

Lines changed: 118 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

kotlin/LukeBalance/raw/teste0

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
3 2
2+
5 1
3+
1 1
4+
4 0
5+
6+
3 1
7+
5 1
8+
1 1
9+
4 0

kotlin/LukeBalance/src/Solution.kt

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
/**
22
* https://www.hackerrank.com/challenges/luck-balance/problem?h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen
3+
* DONE
34
* */
45

56

67
// Complete the luckBalance function below.
78
fun luckBalance(k: Int, contests: Array<Array<Int>>): Int {
89
var acc = 0
9-
10+
var qImportantContests = 0
11+
for( i in 0 until contests.size) {
12+
val p = contests[i]
13+
if (p[1] == 1) {
14+
// contar quantidade de contestes importantes que foram perdidos
15+
if (qImportantContests < k) {
16+
acc += p[0]
17+
qImportantContests++
18+
}
19+
else {
20+
acc -= p[0]
21+
}
22+
}
23+
else {
24+
acc += p[0]
25+
}
26+
}
1027
return acc
1128
}
12-
/**
13-
3 2
14-
5 1
15-
1 1
16-
4 0
17-
* */
1829

1930
fun main(args: Array<String>) {
2031
val data = readLine()!!.split(" ").map(String::toInt).toTypedArray()
@@ -25,7 +36,13 @@ fun main(args: Array<String>) {
2536
contest[i] = readLine()!!.split(" ").map(String::toInt).toTypedArray()
2637
}
2738
val comp = Comparator<Array<Int>> {
28-
p, q -> q[1].compareTo(p[1])
39+
p, q ->
40+
val rs = q[1].compareTo(p[1])
41+
if (rs == 0)
42+
q[0].compareTo(p[0])
43+
else
44+
rs
45+
2946
}
3047
contest.sortWith(comp)
3148
println(luckBalance(k, contest))

kotlin/LukeBalance/src/sort/BasicComparator.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,16 @@ package sort
33
/**
44
* https://grokonez.com/kotlin/kotlin-array-sort-sortby-sortwith#1_sortWith
55
* https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sort-with.html
6-
* */
6+
* */
7+
8+
9+
fun testComparator () {
10+
val comp = Comparator<Int> {
11+
p, q -> p.compareTo(q)
12+
}
13+
}
14+
15+
16+
fun main(args: Array<String>) {
17+
18+
}

0 commit comments

Comments
 (0)