给出以下对象阵容:
var lineup = [
{
"Name": "Matt Ryan",
"Team": "ATL",
"Position": "QB"
},{
"Name": "Devonte Freeman",
"Team": "ATL",
"Position": "RB"
}, {
"Name": "Jahvid Best",
"Team": "DET",
"Position": "RB"
}, {
"Name": "Calvin Johnson",
"Team": "DET",
"Position": "WR"
},{
"Name": "Julio Jones",
"Team": "ATL",
"Position":"WR"
}, {
"Name": "Julian Edelman",
"Team": "NE",
"Position": "WR"
}, {
"Name": "Kyle Rudolph",
"Team": "MIN",
"Position": "TE"
}, {
"Name": "Jordy Nelson",
"Team": "GB",
"Position": "WR"
}, {
"Name": "Cincinatti Bengals",
"Team": "CIN",
"Position": "DST"
}
];
评估阵容并在有效时返回true,在要求下返回false:
//rule 1: must use 9 players
//rule 2: must use a roster spanning 2 games
//rule 3: Must fit to position requirements
//rule 4: Limit maximum number of players from one team to 4
var validateLineup(lineup){
var posReq = positionRequirement(lineup); //checks rule 1 and 3
var validGames = checkValidGames(lineup); //check rule 2
var maxPlayers = checkMaxPlayers(lineup); //check rule 4
return posReq && validGames && maxPlayers;
}
var checkMaxPlayers(lineup){
var teamDict = [];
var maxTeam = 0;
for (var i = 0; i < lineup.length; i++){
for (var j = 1; j < lineup.length; j++){
if (lineup[i].Team === lineup[j].Team){
maxTeam++;
}
}
if (maxTeam > 3) return false;
}
return true;
}
var checkValidGames(lineup){
var arrTeams = [];
for (var i = 0; i < lineup.length; i++){
if (!lineup[i].Team in arrTeams ){
arrTeams.push(lineup[i].Team);
}
}
if (arrTeams.length >= 2) return true;
else return false;
}
//O(N) time complexity O(1) space
var positionRequirement(lineup){
var noQB, noRB, noWR, noTE, noDST = 0;
for (var i = 0; i < lineup.length; i++){
if (lineup[i].Position === "QB"){
noQB++;
}
else if (lineup[i].Position === "RB"){
noRB++;
}
else if (lineup[i].Position === "WR"){
noWR++;
}
else if (lineup[i].Position === "TE"){
noTE++;
}
else if (lineup[i].Position === "DST"){
noDST++;
}
}
if (noQB != 1) return false;
else if (noRB > 3 || noRB < 2) return false;
else if (noWR > 4 || noWR < 3) return false;
else if (noTE > 2 || noTE < 1) return false;
else if (noDST != 1) return false;
if (noQB+noRB+noWR+noTE+noDST === 9) return true;
else return false;
}
我相信一切都应该没问题,除非我特别喜欢在一支球队中检查最多球员的功能。如果是任何其他语言,我觉得使用地图会更快,但因为它的javascript,初始化字典然后评估似乎它比只做一个O(N ^ 2)循环要慢。我们将非常感谢代码检查和进一步改进提示
答案 0 :(得分:0)
在JavaScript中,对象的工作方式类似于地图。因此,您可以在线性时间内浏览您的数据并与其玩家分享不同的团队。
答案 1 :(得分:0)
checkMaxPlayers函数有一个修复程序,我猜每个内部循环之后maxTeam必须重置为0,这是修复程序:
var checkMaxPlayers(lineup){
var teamDict = [];
var maxTeam = 0;
for (var i = 0; i < lineup.length; i++){
for (var j = 1; j < lineup.length; j++){
if (lineup[i].Team === lineup[j].Team){
maxTeam++;
}
}
if (maxTeam > 4)
{
return false;
}
else
{
maxTeam=0;
}
}
return true;
}
答案 2 :(得分:0)
使用像字典这样的对象,一次遍历数据,如果不存在,则将团队添加到对象的键中;如果存在,则增加该团队的值。
遍历键列表,如果键值大于4,则返回false。
function checkMaxPlayers(lineup)
{
var team = [];
lineup.forEach(el => {
if(Object.keys(team).includes(el.Team))
{
team[el.Team]++;
}
else
{
team[el.Team] = 1;
}
});
for(let key of Object.entries(team))
{
if(key[1]>4)
{
return false;
}
}
return true;
}
答案 3 :(得分:0)
所有 3 个函数都有小问题,这里是每个函数的修复程序。它们现在都使用给定的列表返回 true
//rule 1: must use 9 players
//rule 2: must use a roster spanning 2 games
//rule 3: Must fit to position requirements
//rule 4: Limit maximum number of players from one team to 4
function validateLineup(lineup){
var posReq = positionRequirement(lineup); //checks rule 1 and 3
var validGames = checkValidGames(lineup); //check rule 2
var maxPlayers = checkMaxPlayers(lineup); //check rule 4
return posReq && validGames && maxPlayers;
}
//O(N) time complexity O(1) space
function positionRequirement(lineup){
var noQB = noRB = noWR = noTE = noDST = 0;
for (var i = 0; i < lineup.length; i++){
if (lineup[i].Position === "QB"){
noQB++;
}
else if (lineup[i].Position === "RB"){
noRB++;
}
else if (lineup[i].Position === "WR"){
noWR++;
}
else if (lineup[i].Position === "TE"){
noTE++;
}
else if (lineup[i].Position === "DST"){
noDST++;
}
}
if (noQB != 1) return false;
else if (noRB > 3 || noRB < 2) return false;
else if (noWR > 4 || noWR < 3) return false;
else if (noTE > 2 || noTE < 1) return false;
else if (noDST != 1) return false;
if (noQB+noRB+noWR+noTE+noDST === 9) return true;
else return false;
}
function checkValidGames(lineup){
var arrTeams = [];
for (var i = 0; i < lineup.length; i++){
if (!arrTeams.includes(lineup[i].Team) ){
arrTeams.push(lineup[i].Team);
}
}
if (arrTeams.length >= 2) return true;
else return false;
}
function checkMaxPlayers(lineup){
var teamMap = {};
var maxTeam = 0;
for (var i = 0; i < lineup.length; i++){
teamMap[lineup[i].Team] = 0;
}
for (var i = 0; i < lineup.length; i++){
teamMap[lineup[i].Team]++;
if(teamMap[lineup[i].Team] > maxTeam){
maxTeam = teamMap[lineup[i].Team]
}
}
if (maxTeam > 3) return false;
return true;
}