Closed ajayg415 closed 3 years ago
Simple approach, 2 loops
function tournamentWinner(competitions, results) {
let resObj = {}
competitions.forEach((cv, ind) => {
const win = cv[!results[ind] ? 1 : 0]
if(resObj[win]){
resObj[win] = resObj[win] + 3;
}else{
resObj[win] = 3
}
})
return Object.keys(resObj).reduce((a, b) => resObj[a] > resObj[b] ? a : b);
}
Single loop approach, storing data in hashmap and comparing winner using points and storing in a different variable.
function tournamentWinner(competitions, results) {
let currentBestTeam = 'cbt';
const scores= {[currentBestTeam]:0}
for(let i=0;i<competitions.length;i++){
const winner = competitions[i][results[i] ? 0 : 1]
if(scores[winner]){
const score = scores[winner] + 3;
scores[winner] = score
if(scores[currentBestTeam] < score){
currentBestTeam = winner;
}
} else{
scores[winner] = 3
if(scores[currentBestTeam] < 3){
currentBestTeam = winner;
}
}
}
return currentBestTeam;
}
Single loop approach, storing data in hashmap and comparing winner using points and storing in a different variable.
function tournamentWinner(competitions, results) { let currentBestTeam = 'cbt'; const scores= {[currentBestTeam]:0} for(let i=0;i<competitions.length;i++){ const winner = competitions[i][results[i] ? 0 : 1] if(scores[winner]){ const score = scores[winner] + 3; scores[winner] = score if(scores[currentBestTeam] < score){ currentBestTeam = winner; } This condition is not required i feel, although i did not do good amount of testing :P } else{ scores[winner] = 3 if(scores[currentBestTeam] < 3){ currentBestTeam = winner; } } } return currentBestTeam; }
I did not see your solution but given same solution as a PR.
Solved and added to repo. https://github.com/ajayg415/Javascript-Algorithms/pull/20
There's an algorithms tournament taking place in which teams of programmers compete against each other to solve algorithmic problems as fast as possible. Teams compete in a round robin, where each team faces off against all other teams. Only two teams compete against each other at a time, and for each competition, one team is designated the home team, while the other team is the away team. In each competition there's always one winner and one loser; there are no ties. A team receives 3 points if it wins and 0 points if it loses. The winner of the tournament is the team that receives the most amount of points.
Given an array of pairs representing the teams that have competed against each other and an array containing the results of each competition, write a function that returns the winner of the tournament. The input arrays are named
competitions
andresults
, respectively. Thecompetitions
array has elements in the form of[homeTown, awayTown]
, where each team is a string of at most 30 characters representing the name of the team. The results array contains information about the winner of each corresponding competition in thecompetitions
array. Specificallyresults[i]
denotes the winner ofcompetitions[i]
, where a1
in theresults
array means that the home team in the corresponding competition won and a0
means that the away team won.It's guaranteed that exactly one team will win the tournament and that each team will compete against all other teams exactly once. It's also guaranteed that the tournament will always have at least two teams.