Advertisement
Artim_Anton

Untitled

Apr 6th, 2021 (edited)
1,613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.57 KB | None | 0 0
  1. import 'package:trotter/trotter.dart';
  2.  
  3. int chooseBestDistance(int t, int k, List<int> ls) {
  4.  if((t>=0) && (k>=1) && (ls.length!=0)){
  5.   var max = 0;
  6.    var dist = 0;
  7.     var combos = Combinations(k, ls);
  8.     for (final combo in combos()) {
  9.       for(int i=1; i<=k; i++){dist=dist+combo[i];}
  10.       if ((dist>max) && (dist<=t) ){max = dist;}
  11.     }
  12.          
  13.    if (max!=0 && (k<=ls.length)) {
  14.      return max;
  15.    }else{
  16.     return -1;
  17.     }
  18.   }
  19. }
  20.  
  21. void main(){
  22. chooseBestDistance(174, 3, [51, 56, 58, 59, 61]); //173
  23. chooseBestDistance(163, 3, [50]); // -1
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement