aboutsummaryrefslogtreecommitdiff
path: root/max_total_importance_road.c
diff options
context:
space:
mode:
Diffstat (limited to 'max_total_importance_road.c')
-rw-r--r--max_total_importance_road.c29
1 files changed, 19 insertions, 10 deletions
diff --git a/max_total_importance_road.c b/max_total_importance_road.c
index 145c3e2..55c9e07 100644
--- a/max_total_importance_road.c
+++ b/max_total_importance_road.c
@@ -1,12 +1,20 @@
// 2285. Maximum Total Importance of Roads
-#include <stdio.h>
-#include <stdlib.h>
+#include "leetcode.h"
+
+/*
+ * given an integer 'n' denoting the number of cities in a country. the cities
+ * are numbered from 0 to 'n - 1'. you are also given a 2d integer array 'roads'
+ * where 'roads[i] = [ai, bi]', where each value can only be used once. the
+ * importance of a road is then defined as the sum of the values of the two
+ * cities it connects. return the maximum total importance of all roads possible
+ * after assigning the values optimally.
+ */
int cmp(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
long long maximumImportance(int n, int **roads, int roadsSize,
int *roadsColSize) {
- long long ans = 0, *timeShown = calloc(n, sizeof(long long));
+ long long ans = 0, *timeShown = (long long *)calloc(n, sizeof(long long));
for (int i = 0; i < roadsSize; i++)
for (int j = 0; j < 2; j++)
timeShown[roads[i][j]]++;
@@ -18,12 +26,13 @@ long long maximumImportance(int n, int **roads, int roadsSize,
}
int main() {
- int roads1[][2] = {{0, 1}, {1, 2}, {2, 3}, {0, 2}, {1, 3}, {2, 4}},
+ int r1[][2] = {{0, 1}, {1, 2}, {2, 3}, {0, 2}, {1, 3}, {2, 4}},
roadsColSize1[] = {6};
- int roads2[][2] = {{0, 3}, {2, 4}, {1, 3}}, roadsColSize2[] = {3};
- printf("%llu\n", maximumImportance(5, roads1, 6, roadsColSize1)); // expect:
- // 43
- printf("%llu\n", maximumImportance(5, roads1, 3, roadsColSize1)); // expect:
- // 20
- return 0;
+ int r2[][2] = {{0, 3}, {2, 4}, {1, 3}}, roadsColSize2[] = {3};
+ printf("%llu\n",
+ maximumImportance(5, (int **)r1, 6, roadsColSize1)); // expect:
+ // 43
+ printf("%llu\n",
+ maximumImportance(5, (int **)r1, 3, roadsColSize1)); // expect:
+ // 20
}