We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Luck Balance
  5. Discussions

Luck Balance

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 843 Discussions, By:

recency

Please Login in order to post a comment

  • rafaz182
    2 days ago+ 0 comments

    Java code. Passes all test.

    public static int luckBalance(int k, List<List<Integer>> contests) {
            //sort decreasing by luck
            Collections.sort(contests, (o1, o2) -> o2.get(0).compareTo(o1.get(0)));
            int lost = 0;
            int total = 0;
            for (List<Integer> contest : contests) {
                int luck = contest.get(0);
                boolean isImportant = contest.get(1) == 1;
                boolean needWin = k <= lost;
    
                if (needWin && isImportant) {
                    total += (luck * -1);
                } else {
                    total += luck;
                    if (isImportant) lost++;
                }
            }
    
            return total;
        }
    
    0|
    Permalink
  • thecodingsoluti2
    1 week ago+ 0 comments

    Here is problem solution - https://programs.programmingoneonone.com/2021/03/hackerrank-luck-balance-solution.html

    0|
    Permalink
  • nguyenlehuyuit
    1 week ago+ 0 comments

    Php

    function luckBalance($k, $contests) {
        $importants = [];
        $numOfImportant = 0;
        $sum = 0;
        foreach ($contests as $contest) {
          if ($contest[1] === 1) {
              $importants[] = $contest;
              $numOfImportant++;
          }
          $sum += $contest[0];
        }
    
        if ($numOfImportant <= $k) {
            return $sum;
        }
    
        usort($importants, function ($a, $b){
           return $a[0] - $b[0];
        });
        $subTract = 0;
        $subTractContests = $numOfImportant - $k;
        for($i = 0; $i < $subTractContests; $i++){
            $subTract += $importants[$i][0];
        }
    
        return $sum - 2 * $subTract;
    }
    
    0|
    Permalink
  • bhavesh_itankar
    2 weeks ago+ 0 comments

    Golang solution

    func luckBalance(k int32, contests [][]int32) int32 {
        // Write your code here
        var sum_luck int32
        var max_fail []int
        for i:=0; i<len(contests);i++{
            if contests[i][1] == 0 {
                // fmt.Println(contests[i][0])
                sum_luck += contests[i][0]
            } else {
                max_fail = append(max_fail, int(contests[i][0]))
            }
        }
        sort.Ints(max_fail)
        for i:=int32(0);i<int32(len(max_fail));i++{
            if i<int32(len(max_fail))-k {
                sum_luck -= int32(max_fail[i])
            } else {
                sum_luck += int32(max_fail[i])
            }
            
        }
        return sum_luck
    }
    
    0|
    Permalink
  • grbz_kerem
    3 weeks ago+ 0 comments

    JavaScript Solution

    function luckBalance(k, contests) {
      const important = contests
        .filter((a) => a[1] === 1)
        .sort((x, y) => y[0] - x[0])
        .reduce((sum, a, i) => (i < k ? sum + a[0] : sum - a[0]), 0);
      const unimportant = contests
        .filter((a) => a[1] === 0)
        .reduce((sum, a) => sum + a[0], 0);
      return important + unimportant;
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy