Skip to content

Commit

Permalink
Merge 3997d84 into 50f4261
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Apr 20, 2019
2 parents 50f4261 + 3997d84 commit 7969c61
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 20 additions & 0 deletions day241/problem.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 28 additions & 0 deletions day241/problem_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 7969c61

Please sign in to comment.