diff options
author | Jack Sangdahl <[email protected]> | 2024-07-17 01:12:51 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-07-17 01:12:51 -0600 |
commit | 46f7efa9168b71af5c3820d542e965085a397d68 (patch) | |
tree | 59c9fb26a6de16cb34128d0eb540b45abd17edf7 | |
parent | 0f997410755df764113d8a8a1a9a146df51c7f90 (diff) | |
download | leetcode-46f7efa9168b71af5c3820d542e965085a397d68.tar.gz leetcode-46f7efa9168b71af5c3820d542e965085a397d68.zip |
2923. find champion 1
-rw-r--r-- | find_champion_1.c | 32 | ||||
-rw-r--r-- | find_champion_1.py | 28 |
2 files changed, 60 insertions, 0 deletions
diff --git a/find_champion_1.c b/find_champion_1.c new file mode 100644 index 0000000..3a35fa1 --- /dev/null +++ b/find_champion_1.c @@ -0,0 +1,32 @@ +// 2923. Find Champion I +#include "leetcode.h" + +/* + * there are 'n' teams numbered from 0 to 'n - 1' in a tournament. given + * 0-indexed 2d boolean matrix 'grid' of size 'n * n'. for all 'i, j' that '0 <= + * i, j <= n - 1' and 'i != j' team 'i' is stronger than team 'j' if 'grid[i][j] + * == 1' otherwise team 'j' is stronger tham team 'i'. team 'a' will be the + * champion of the tournament if there is no team 'b' that is stronger than team + * 'a'. return the team that will be the champion of the tournament. + */ + +int findChampion(int **grid, int gridSize, int *gridColSize) { + int max_idx = 0, max_cnt = 0; + for (int i = 0; i < gridSize; i++) { + int curr_cnt = 0; + for (int j = 0; j < gridColSize[i]; j++) + curr_cnt += grid[i][j]; + if (curr_cnt > max_cnt) { + max_cnt = curr_cnt; + max_idx = i; + } + } + return max_idx; +} + +int main() { + int g1[2][2] = {{0, 1}, {0, 0}}, g2[3][3] = {{0, 0, 1}, {1, 0, 1}, {0, 0, 0}}; + int gcs1 = 2, gcs2 = 3; + printf("%d\n", findChampion((int **)g1, ARRAY_SIZE(g1), &gcs1)); // expect: 0 + printf("%d\n", findChampion((int **)g2, ARRAY_SIZE(g2), &gcs2)); // expect: 1 +} diff --git a/find_champion_1.py b/find_champion_1.py new file mode 100644 index 0000000..1961241 --- /dev/null +++ b/find_champion_1.py @@ -0,0 +1,28 @@ +# 2923. Find Champion I + +""" +there are 'n' teams numbered from 0 to 'n - 1' in a tournament. given +0-indexed 2d boolean matrix 'grid' of size 'n n'. for all 'i, j' that '0 <= +i, j <= n - 1' and 'i != j' team 'i' is stronger than team 'j' if 'grid[i][j] +== 1' otherwise team 'j' is stronger tham team 'i'. team 'a' will be the +champion of the tournament if there is no team 'b' that is stronger than team +'a'. return the team that will be the champion of the tournament. +""" + + +class Solution(object): + def findChampion(self, grid): + """ + :type grid: List[List[int]] + :rtype: int + """ + n = len(grid) + for i in range(n): + if sum(grid[i]) == n - 1: + return i + + +if __name__ == "__main__": + obj = Solution() + print(obj.findChampion(grid=[[0, 1], [0, 0]])) + print(obj.findChampion(grid=[[0, 0, 1], [1, 0, 1], [0, 0, 0]])) |