Skip to content

Commit c270302

Browse files
author
openset
committed
Add: Assign Cookies
1 parent 59e72d4 commit c270302

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
package assign_cookies
1+
package problem_455
2+
3+
import "sort"
4+
5+
func findContentChildren(g []int, s []int) int {
6+
sort.Ints(g)
7+
sort.Ints(s)
8+
var ans, i, j int
9+
for i < len(g) && j < len(s) {
10+
if g[i] <= s[j] {
11+
ans, i = ans+1, i+1
12+
}
13+
j++
14+
}
15+
return ans
16+
}
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
package assign_cookies
1+
package problem_455
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
g []int
7+
s []int
8+
expected int
9+
}
10+
11+
func TestFindContentChildren(t *testing.T) {
12+
tests := [...]caseType{
13+
{
14+
g: []int{1, 2, 3},
15+
s: []int{1, 1},
16+
expected: 1,
17+
},
18+
{
19+
g: []int{1, 2},
20+
s: []int{1, 2, 3},
21+
expected: 2,
22+
},
23+
}
24+
for _, tc := range tests {
25+
output := findContentChildren(tc.g, tc.s)
26+
if output != tc.expected {
27+
t.Fatalf("input: %v, output: %v, expected: %v", tc.g, output, tc.expected)
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)