다음 문제를 해결하기 위해 의사 코드를 작성하려고합니다.일치 결과 집합이 주어지면 O (n) 시간에 최대 10 개의 손실을 가진 팀을 어떻게 얻을 수 있습니까?
입력 : 농구 경기의 n 팀. 각 팀 TI는, TJ 재생과 승리/손실 기록이 매트릭스에있는
그들이 돈 경우
는 팀이 메달을 얻는다 (TI는 TJ 이기면 내가 < J, A [I, J = 1) ' 10 회 이상 잃지 마라.
출력 : 메달을받는 모든 팀. O (n) 시간에 찾으십시오.
이것은 지금까지 내 의사 코드입니다. 가짜 코드는 지금은 충분하지만 나는 이것이 올바르게 작동하지 않는다고 생각하지 않는다. 또한 선형 시간에 작동하지 않는다.
//input: matrix A of win/losses
//output: list of teams with medals
set S to an array of length n, with value 0 for all indices //keeps track of number of losses
for each team:
for each game played in the team's row in M:
if the team won:
increment the opponent’s number of losses in the opponent’s index of S
else:
increment the team's number of losses in team's index of S
if at any point a value in the array S reaches 11, remove that team from the list of teams we consider //so basically ignore all the games they play from hereon out
end for loop
end for loop
take this new potential list of teams with medals
iterate through each team's row in M, counting losses //to check if they lost against any of the teams we removed from the previous for loop
if losses > 10, remove the team from list
return final list
이 코드로 질문/문제를 잊어 버린 것 같습니다. –
O (n) 시간에는 찾을 수 없습니다. 손실 수를 올바르게 계산하려면 2 차원 어레이를 반복해야하므로 분명히 최악입니다. case time complexity는 O (n^2)가 될 것이다. – zenwraight
당신은 모든 게임을 이처럼 두 번 계산합니다. 0을보고 1을 무시하는 것이 더 쉽습니다. 또는 다음 팀마다 내부 루프 만 수행하십시오. – maraca