Sort by

recency

|

925 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/7L1lI6JScR8

    int luckBalance(int k, vector<vector<int>> contests) {
        sort(contests.begin(), contests.end(), [](vector<int> l, vector<int>r){
            return l[0] > r[0];
        });
        int ans = 0, im = 0;
        for(int i = 0; i < contests.size(); i++){
            if(im < k || contests[i][1] == 0) ans += contests[i][0];
            else ans -= contests[i][0];
            if(contests[i][1] == 1) im++;
        }
        return ans;
    }
    
  • + 0 comments

    PHP solutions

    unimportant = 0; foreach (key => $value) {

         if ($value[1] == 1) {
             `$importantContest[] = $`value[0];
         }
         else
         {
            `$unimportant+= $`value[0];
         }
    
        }
        rsort($importantContest);
    
        `$len = count($`importantContest);
        for (`$i=0; $`i < `$len; $`i++) { 
            if(`$i < $`k)
            {
                `$unimportant+=$`importantContest[$i];
            }
            else
            {
                `$unimportant-=$`importantContest[$i];
            }
    
        }
        echo $unimportant;
    
  • + 0 comments
        public static int luckBalance(int k, List<List<Integer>> contests) {       
            // Categorize importants and count unimportant
            int luck = 0;
            List<Integer> importantContests = new ArrayList<>();
    
            for (List<Integer> arr : contests) {
                int luckValue = arr.get(0);
                int isImportant = arr.get(1);
            
                if (isImportant == 1) {
                    importantContests.add(luckValue);
                } else {
                    luck += luckValue;
                }
            }
            
            // Sort by huge value
            importantContests.sort(Collections.reverseOrder());
            
            // Plus or minus 
            for (int i = 0; i < importantContests.size(); i++) {
                luck += (i < k) ? importantContests.get(i) : -importantContests.get(i);
            }
    
            return luck;
        }
    
  • + 0 comments

    JS - use of array-functions: filter, sort & reduce

    function luckBalance(k, contests) {
        // Write your code here
        const unimportantArr = contests.filter(c => c[1] === 0);
        const importantArr = contests.filter(c => c[1] === 1);
        // sort -> from least important to max important
        importantArr.sort((a, b) => a[0] - b[0] < 0 ? 1 : -1);
        
        // we lose all unimportant constests for max luck
        const luckU = unimportantArr.reduce((a, c) => a + c[0] , 0);
        // the least important ones (lower than k) we win, else we lose to gain luck
        const luckI = importantArr.reduce(
            (a, c, idx) => idx >= k ? a - c[0] : a + c[0], 0);
        
        return luckU + luckI;
    }
    
  • + 0 comments

    Java:

    public static int luckBalance(int k, List<List<Integer>> contests) {
      int balance = 0;
      Collections.sort(contests, new Comparator<List<Integer>>() {
        @Override
        public int compare(List<Integer> o1, List<Integer> o2) {
          return o1.get(0).compareTo(o2.get(0));
        }
      });
      int n = contests.size();
      for (int i = n - 1; i >= 0; i--) {
        int current = contests.get(i).get(0);
        if (contests.get(i).get(1) == 1) {
          if (k > 0) {
            balance += current;
            k--;
          } else {
            balance -= current;
          }
    
        } else {
          balance += current;
        }
      }
    
      return balance;
    }