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.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Greedy
  4. Luck Balance
  5. Discussions

Luck Balance

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial

    You are viewing a single comment's thread. Return to all comments →

  • trkmos 1 year ago+ 1 comment

    javaScript solution

    function luckBalance(k, contests) {
        let important = contests.filter(ar => ar[1] === 1).length;
        let sumAll = contests.reduce((a, b) => a+b[0],0);
        let sorted = contests.sort((a, b) => a[0] - b[0])
        let win = important-k >=0 ?important-k : 0 
        let min = 0
        for(let i=0;  i<sorted.length; i++){
            if(win === 0) break;
            if(sorted[i][1] === 0)continue;
            min += sorted[i][0];
            win--
        }
        return sumAll - (2*min);
    }
    
    1|
    Permalink
    • jrcastillo2001 8 months ago+ 0 comments

      There seems to be a lot of loops, with this solutions is the time complexity a: O(n*3) + O(m) ... n being constests - m being sorted ... ?

      how about this for a change ?

      function luckBalance(k, contests) {
        const importantContestsVals = []
        let val = 0;
        
        for (let i = 0; i < contests.length; i++) { 
          if (contests[i][1]) {
            importantContestsVals.push(contests[i][0])
          } else { 
            val += contests[i][0]
          }
        }
        
        importantContestsVals.sort((a, b) => a - b)
        const negativeArr = importantContestsVals.splice(0, importantContestsVals.length - k)
        const largestArr = negativeArr.length >= importantContestsVals.length ? negativeArr.length : importantContestsVals.length
      
        for (let j = 0; j < largestArr; j++) { 
          if (negativeArr[j]) { 
            val -= negativeArr[j]
          }
          if (importantContestsVals[j]) { 
            val += importantContestsVals[j]
          }
        }
      
        return val
      }
      
      0|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature