JAR
BAN USERimport java.util.*;
class TestResult
{
int studentId;
String testDate;
int testScore;
public TestResult(int studentId, String testDate, int testScore) {
super();
this.studentId = studentId;
this.testDate = testDate;
this.testScore = testScore;
}
}
public class FinalScoreQuestion {
Map<Integer, Double> calculateFinalScores(List<TestResult> results)
{
Map<Integer, Double> StuMap = new HashMap<>();
Map<Integer,PriorityQueue<Integer>> map = new HashMap<>();
if(results == null || results.size() == 0) return StuMap;
for(TestResult result: results)
{
int id = result.studentId;
int score = result.testScore;
PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
if(map.containsKey(id))
{
queue = map.get(id);
if(queue.size()>6)
{
queue.poll();
queue.offer(score);
map.put(id,queue);
}
else
{
queue.offer(score);
map.put(id,queue);
}
}
else
{
queue.offer(score);
map.put(id, queue);
}
}
//Calculate average
for(int key: map.keySet())
{
PriorityQueue<Integer> queue = new PriorityQueue<>();
queue = map.get(key);
int sum=0;
double avg;
for(int i=0;i<5;i++)
sum += queue.poll();
avg = sum/5;
StuMap.put(key, avg);
}
return StuMap;
}
public static void main(String[] args) {
List<TestResult> testResults = new ArrayList<TestResult>(0);
testResults.add(new TestResult(1, "02-07-2013", 60));
testResults.add(new TestResult(1, "02-07-2013", 70));
testResults.add(new TestResult(1, "02-07-2013", 80));
testResults.add(new TestResult(1, "02-07-2013", 90));
testResults.add(new TestResult(1, "02-07-2013", 95));
testResults.add(new TestResult(1, "02-07-2013", 100));
testResults.add(new TestResult(2, "02-07-2013", 60));
testResults.add(new TestResult(2, "02-07-2013", 65));
testResults.add(new TestResult(2, "02-07-2013", 70));
testResults.add(new TestResult(2, "02-07-2013", 75));
testResults.add(new TestResult(2, "02-07-2013", 80));
FinalScoreQuestion finalScoreQuestion = new FinalScoreQuestion();
finalScoreQuestion.calculateFinalScores(testResults);
}
}
Output:
Peek, Key = 1 value 100
Peek, Key = 1 value 95
Peek, Key = 1 value 90
Peek, Key = 1 value 80
Peek, Key = 1 value 70
1 Key AVG = 87.0
Peek, Key = 2 value 80
Peek, Key = 2 value 75
Peek, Key = 2 value 70
Peek, Key = 2 value 65
Peek, Key = 2 value 60
2 Key AVG = 70.0
Sort all of your rectangles' min and max x coordinates into an array, as "start-rectangle" and "end-rectangle" events
Step through the array, adding each new rectangle encountered (start-event) into a current set
Simultaneously, maintain a set of "non-overlapping rectangles" that will be your output set
Any time you encounter a new rectangle you can check whether it's completely contained already in the current / output set (simple comparisons of y-coordinates will suffice)
If it isn't, add a new rectangle to your output set, with y-coordinates set to the part of the new rectangle that isn't already covered.
Any time you hit a rectangle end-event, stop any rectangles in your output set that aren't covering anything anymore.
Repaaronfreunda, Human resources coordinator at Prestiga-Biz
Aaron , My work is to facilitate daily HR functions like track of employees records and supporting the interview process. I ...
Repsheilaahollins, Blockchain Developer at ASAPInfosystemsPvtLtd
Hello, I am working as a mineralogist studying rocks, gems, and other minerals, including their chemical and crystalline structures. I ...
Replevijhoyt, Android Engineer at A9
Hello, I am working as a machine operator and we are working in manufacturing units and operate equipment to create ...
Repritahmixon, Analyst at ADP
Hi, I am a physiotherapy to help people handle everyday problems. I also assist peoples who have issues caused by ...
Repjd5636176, Area Sales Manager at Achieve Internet
I am currently in the tool and die maker in Titania to start my career. I work from engineering drawings ...
Repjaksanjak17, Sports official at Avant Garde Appraisal Group
I am a Sports official in Avant Garde Appraisal Group . in a variety of sports and competition, responsible for enforcing ...
Efficient algorithm which works for any input size.
Time efficient because of use of bit shift.
Thanks to Stackoverflow for the awesome trick!
- JAR July 16, 2017