Skip to content

Commit d99d36c

Browse files
committed
estudos
1 parent 2b08fa2 commit d99d36c

File tree

11 files changed

+858
-1
lines changed

11 files changed

+858
-1
lines changed

kotlin/MaxArraySum/.idea/dbnavigator.xml

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

kotlin/MaxArraySum/.idea/kotlinc.xml

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

kotlin/MaxArraySum/.idea/libraries/KotlinJavaRuntime.xml

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

kotlin/MaxArraySum/.idea/misc.xml

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

kotlin/MaxArraySum/.idea/modules.xml

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

kotlin/MaxArraySum/.idea/workspace.xml

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

kotlin/MaxArraySum/MaxArraySum.iml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<option name="createStubs" value="true" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
</content>
9+
<orderEntry type="inheritedJdk" />
10+
<orderEntry type="sourceFolder" forTests="false" />
11+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
12+
</component>
13+
</module>
Binary file not shown.
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import kotlin.math.max
2+
3+
/**
4+
* https://www.hackerrank.com/challenges/max-array-sum/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dynamic-programming
5+
* */
6+
7+
8+
fun TLE(arr: Array<Int>): Int {
9+
val lim = arr.size
10+
var max = 0
11+
for ( i in 0 until lim) {
12+
var s = 2
13+
while (i + s < lim) {
14+
var parcial = arr[i]
15+
for ( j in i+s until lim step s) {
16+
parcial += arr[j]
17+
if (parcial > max)
18+
max = parcial
19+
}
20+
s++
21+
}
22+
}
23+
return max
24+
}
25+
26+
fun maxSubsetSum(arr: Array<Int>): Int {
27+
return if (arr.size == 1)
28+
arr[0]
29+
else {
30+
arr[0] = max(0, arr[0])
31+
arr[1] = max(arr[0], arr[1])
32+
for (i in 2 until arr.size) {
33+
val p = arr[i] + arr[i-2]
34+
val q = arr[i-1]
35+
arr[i] = max(p, q)
36+
}
37+
arr[arr.size-1]
38+
}
39+
}
40+
41+
fun main(args: Array<String>) {
42+
val n = readLine()!!.toInt()
43+
val arr = readLine()!!.split(" ").map { it.toInt() }.toTypedArray()
44+
println(maxSubsetSum(arr))
45+
}

0 commit comments

Comments
 (0)