Created
December 19, 2023 14:28
-
-
Save thiagozs/e0f1f5fd1c16403f05593bf280ed4526 to your computer and use it in GitHub Desktop.
BaieresDev golang challenge
This file contains hidden or 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
package main | |
import "fmt" | |
func solution (n int, ratings [][]int) int { | |
dishRatings := make(map[int][2]int) | |
for i := 0; i < n; i++ { | |
dishID, rating := ratings[i][0], ratings[i][1] | |
values, exists := dishRatings[dishID] | |
if !exists { | |
values = [2]int{0, 0} | |
} | |
values[0] += rating | |
values[1]++ | |
dishRatings[dishID] = values | |
} | |
highestAverageRating := -1.0 | |
bestDishID := -1 | |
for dishID, values := range dishRatings { | |
totalRatings, count := values[0], values[1] | |
averageRating := float64(totalRatings) / float64(count) | |
if averageRating > highestAverageRating || (averageRating == highestAverageRating && dishID < bestDishID) { | |
highestAverageRating = averageRating | |
bestDishID = dishID | |
} | |
} | |
return bestDishID | |
} | |
func main() { | |
var n int | |
fmt.Scanln(&n) | |
ratings := make([][]int, n) | |
for i_ratings := 0; i_ratings < n; i_ratings++ { | |
ratings[i_ratings] = make([]int, 2) | |
for j_ratings := 0; j_ratings < 2; j_ratings++ { | |
fmt.Scan(&ratings[i_ratings][j_ratings]) | |
} | |
} | |
var out_ int = solution(n, ratings) | |
fmt.Println(out_) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment