-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 changed file
with
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
/** | ||
* @brief LC2923: Find the Champion | ||
* this solution time complexity is based on two layer of loops | ||
* So, really slow | ||
* @param grid | ||
* @return int | ||
*/ | ||
int findChampion(vector<vector<int>>& grid) { | ||
int sz = grid.size(); | ||
// int the bool matrix, if grid[i][j] == 1 && i != j, i win | ||
unordered_map<int, int> mp; | ||
using pair_type = decltype(mp)::value_type; | ||
for (int i = 0; i < sz; i++) { | ||
for (int j = 0; j < sz; j++) { | ||
if (i == j) { | ||
// nothing | ||
} | ||
if (grid[i][j] == 1) { | ||
mp[i]++; | ||
mp[j]--; | ||
} else { | ||
mp[i]--; | ||
mp[j]++; | ||
} | ||
} | ||
} | ||
auto best = std::max_element( | ||
mp.begin(), mp.end(), [](const pair_type& lhs, const pair_type& rhs) { | ||
return lhs.second < rhs.second; | ||
}); | ||
return best->first; | ||
} | ||
|
||
/// @brief Every time we update the result, this is fast | ||
/// @param grid | ||
/// @return | ||
int findChampionOP(vector<vector<int>>& grid) { | ||
int ans = 0; | ||
for (int i = 1; i < grid.size(); i++) { | ||
if (grid[i][ans]) { | ||
ans = i; | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |