-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |