Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 241
Good morning! Here's your coding interview problem for today.

This problem was asked by Palantir.

In academia, the h-index is a metric used to calculate the impact of a researcher's papers. It is calculated as follows:

A researcher has index h if at least h of her N papers have h citations each. If there are multiple h satisfying this formula, the maximum is chosen.

For example, suppose `N = 5`, and the respective citations of each paper are `[4, 3, 0, 1, 5]`. Then the h-index would be `3`, since the researcher has 3 papers with at least 3 citations.

Given a list of paper citations of a researcher, calculate their h-index.

Pull request description:
Day241
Resolves #493.

Patch:
diff --git a/README.md b/README.md
index c87a6a6..bcb745e 100644
--- a/README.md
+++ b/README.md
@@ -247,3 +247,4 @@ problems from
 * [Day 236](https://github.com/vaskoz/dailycodingproblem-go/issues/485)
 * [Day 237](https://github.com/vaskoz/dailycodingproblem-go/issues/487)
 * [Day 238](https://github.com/vaskoz/dailycodingproblem-go/issues/489)
+* [Day 241](https://github.com/vaskoz/dailycodingproblem-go/issues/493)
diff --git a/day241/problem.go b/day241/problem.go
new file mode 100644
index 0000000..a56f4ef
--- /dev/null
+++ b/day241/problem.go
@@ -0,0 +1,20 @@
+package day241
+
+import "sort"
+
+// HIndex returns the H-Index of the citations from an author.
+// Runs in O(N log N) using O(N) extra memory because we
+// don't want to mutate the original data by sorting.
+func HIndex(citations []int) int {
+	copied := make([]int, len(citations))
+	copy(copied, citations)
+	sort.Slice(copied, func(i, j int) bool {
+		return copied[i] > copied[j]
+	})
+	for paper, citationCount := range copied {
+		if citationCount < paper+1 {
+			return paper
+		}
+	}
+	return len(copied)
+}


Test patch:
diff --git a/day241/problem_test.go b/day241/problem_test.go
new file mode 100644
index 0000000..cdea427
--- /dev/null
+++ b/day241/problem_test.go
@@ -0,0 +1,28 @@
+package day241
+
+import "testing"
+
+var testcases = []struct {
+	citations []int
+	hindex    int
+}{
+	{[]int{4, 3, 0, 1, 5}, 3},
+	{[]int{10}, 1},
+}
+
+func TestHIndex(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := HIndex(tc.citations); result != tc.hindex {
+			t.Errorf("Expected %v, got %v", tc.hindex, result)
+		}
+	}
+}
+
+func BenchmarkHIndex(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			HIndex(tc.citations)
+		}
+	}
+}