Created
February 8, 2024 09:13
-
-
Save manofi21/0a5255cf39979ccd359b43abaafd3fa1 to your computer and use it in GitHub Desktop.
Hasil untuk quest ini: https://www.hackerrank.com/challenges/cloudy-day/problem?isFullScreen=true
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
import 'dart:math'; | |
void main() { | |
// case 0 | |
// final townPopulation = [8, 10, 100, 40]; | |
// final townLocation = [4, 5, 100, 120]; | |
// final couldLocation = [4]; | |
// final coludExtendCoverage = [1]; | |
// case 1 | |
final townPopulation = [10, 1, 8, 3]; | |
final townLocation = [4, 5, 7, 2]; | |
final couldLocation = [3, 9, 3, 5]; | |
final coludExtendCoverage = [11, 10, 8, 7]; | |
final getResult = maximumPeople(townPopulation, townLocation, couldLocation, coludExtendCoverage); | |
print(getResult); | |
} | |
int maximumPeople(List<int> p, List<int> x, List<int> y, List<int> r) { | |
final cloudCount = y.length == r.length ? y.length : 0; | |
final townCount = p.length == x.length ? p.length : 0; | |
final listIndexTown = List.generate(townCount, (i) => i); | |
final blackZone = <int>[]; | |
for (var i = 0; i < cloudCount; i ++) { | |
final leftCloud = y[i] - r[i]; | |
final rightCloud = y[i] + r[i]; | |
for (var j = 0; j < townCount; j ++) { | |
if (leftCloud <= x[j] && rightCloud >= x[j]) { | |
blackZone.add(j); | |
} | |
} | |
} | |
for (final blackIndex in blackZone) { | |
listIndexTown.remove(blackIndex); | |
} | |
final whiteZone = listIndexTown.map((i) => p[i]).toList(); | |
final selectedList = blackZone.map((i) => p[i]).toList(); | |
final maxValue = selectedList.reduce(max); | |
final result = whiteZone.reduce((a, b) => a + b) + maxValue; | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment