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.
luckBalance<-function(k,contests){# redefine matrix as data framecontests<-as.data.frame(contests)colnames(contests)<-c("L","T")# tally luck for unimportant lossesunimportantLossLuck<-sum(contests[contests$T==0,"L"])# filter unimportant losses from datacontests<-contests[contests$T==1,]# sort data by luckcontests<-contests[order(contests$L,decreasing=T),]# tally luck for important lossesimportantLossLuck<-sum(head(contests$L,k))# tally luck lost to winswinPriceLuck<-sum(tail(contests$L,max(nrow(contests)-k,0)))# add luck for all losses and subtract luck for winsreturn(importantLossLuck+unimportantLossLuck-winPriceLuck)}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Luck Balance
You are viewing a single comment's thread. Return to all comments →
R: