forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignCookies.kt
63 lines (57 loc) · 2.02 KB
/
AssignCookies.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package questions
import _utils.UseCommentAsDocumentation
import kotlin.test.assertEquals
/**
* Assume you are an awesome parent and want to give your children some cookies.
* But, you should give each child at most one cookie.
* Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with;
* and each cookie j has a size s[j]. If s[j] >= g[i], we can assign the cookie j to the child i,
* and the child i will be content.
* Your goal is to maximize the number of your content children and output the maximum number.
*
* [Source](https://leetcode.com/problems/assign-cookies/)
*/
@UseCommentAsDocumentation
private fun findContentChildren(g: IntArray, s: IntArray): Int {
if (s.isEmpty()) return 0
// Sort
g.sort()
s.sort()
// Store index of g that was satisfied
val result = mutableSetOf<Int>()
for (i in 0..s.lastIndex) { // every cookie
for (j in 0..g.lastIndex) { // loop over kids
if (!result.contains(j) && g[j] <= s[i]) { // hasn't already been satisfied and can be satisfied
result.add(j) // give cookie
break
}
}
}
return result.size
}
private fun findContentChildrenOptimal(g: IntArray, s: IntArray): Int {
if (s.isEmpty()) return 0
// Sort
g.sort()
s.sort()
var count = 0
for (i in 0..s.lastIndex) { // every cookie
val greed = g.getOrNull(count) ?: return count // try to satisfy the NEXT easiest/lowest greed factor first
if (greed <= s[i]) count++
}
return count
}
fun main() {
assertEquals(
2, findContentChildrenOptimal(g = intArrayOf(10, 9, 8, 7), s = intArrayOf(5, 6, 7, 8))
)
assertEquals(
4, findContentChildrenOptimal(g = intArrayOf(10, 9, 8, 7), s = intArrayOf(10, 9, 8, 7))
)
assertEquals(
1, findContentChildrenOptimal(g = intArrayOf(1, 2, 3), s = intArrayOf(1, 1))
)
assertEquals(
2, findContentChildrenOptimal(g = intArrayOf(1, 2), s = intArrayOf(1, 2, 3))
)
}