Regret Java
Regret Java
All the competitors in a stock car race have completed their qualifying laps. Each lap, the driver with the current slowest "best"
time is eliminated (that is, the highest personal best time). If multiple drivers tie for the slowest time, they are all are eliminated.
You are given a two-dimensional string array with each driver's name and lap time in seconds for each lap. Your task is to return
the drivers in the order in which they were eliminated, ending with the last driver or drivers remaining. When multiple drivers are
eliminated on the same lap, their names should be listed alphabetically.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse
than O(laps.length · laps[0].length) will fit within the execution time limit.
Example
•For
Explanation:
•After the first lap, Harold's best time is 154, Gina's best time is 155 and Juan's best time is 160. Juan is
eliminated, leaving Harold and Gina.
•After the second lap, Harold's best time is still 154 and Gina's best time is 153, so Harold is eliminated.
•Gina is the only racer remaining on the third lap.
•For
Explanation:
•After the first lap, Gina's best time is 155, Eddie's best time is 160, Joy's best time is 161, and Harold's best
time is 163. Harold is eliminated.
•After the second lap, Gina's best time is 153, Eddie's best time is 160, and Joy's best time is also 160. Eddie
and Joy are eliminated.
•Because Eddie and Joy were eliminated on the same round, their names are listed alphabetically in
the output.
•Gina is the only racer remaining on the third lap and fourth laps.
Input/Output
•[memory limit] 1 GB
•[output] array.string
Return the list of drivers sorted in the order which they were eliminated, and ordered alphabetically in the case of ties.
/******************************************************************************
*******************************************************************************/
import java.util.*;
// String[][] laps = {{"Gina 155", "Eddie 160", "Joy 161", "Harold 163"},
if(n==0){
System.out.println("");
int m = laps[0].length;
for(int i=0;i<n;i++){
for(int j=0;j<laps[i].length;j++){
if(!hs.contains(name)){
if(grade>max_number){
max_number = grade;
hm.put(arr[0],grade);
}
// find max , it can be one , two , many : find all max_number string
for(Map.Entry<String,Integer> entry:hm.entrySet()){
if(entry.getValue()==max_number){
list.add(entry.getKey());
hs.add(entry.getKey());
if(list.size()==1){
al.add(list);
}else if(list.size()>1){
Collections.sort(list);
al.add(list);
for(String s:list){
hm.remove(s);
System.out.println("hs: "+hs);
System.out.println("hm: "+hm);
System.out.println("al: "+al);
for(String str:al2){
System.out.println("name: "+str);
}
}
4
Let's imagine objects located on the canvas at a certain moment in time. You are given an array of integer
pairs centers representing the coordinates of those objects. Each object has a collision box - a square area around its center
with a side equal to 2. Two objects are supposed to collide if their collision boxes have at least one common point. Calculate the
number of object pairs that collide.
The example of the object located in coordinates [1, 1] can be represented as following:
Object collision boxes intersect if the distance in each coordinate between the object centers does not exceed 2.
If x1, y1 are coordinates of one object and x2, y2 are coordinates of the second object, then the collision condition for them can
be written in the form |x[j] - x[i]| <= 2 and |y[j] - y[i]| <= 2 .
Example
•For
centers = [
[1, 1],
[2, 2],
[0, 4]
]
Explanation:
•|x[1] - x[0]| = 1 and |y[1] - y[0]| = 1 both are <= 2, so the first two objects collide.
•|x[2] - x[1]| = 2 and |y[2] - y[1]| = 2 both are <= 2, so the last two objects collide.
•|x[2] - x[0]| = 1 which is <= 2, but and |y[2] - y[0]| = 3 which is > 2, so the first and the last object
don't collide.
•For
centers = [
[1, 1],
[2, 2],
[0, 4],
[1, 1]
]
Explanation:
The first three objects collide the same as in the previous example.
The last object is located in the same point as the first one, so:
•It also collides with the second one and does not collide with the third one (+1 collision),
•It collides with the first one (+1 collision).
Input/Output
•[memory limit] 1 GB
•[output] integer
Given a list of student grades (a value between 1 to 5) records in the following format: "[name]: [grade]" , find the student
with the highest average grade. It is guaranteed that all students have different average grades.
Note: Names do not contain spaces, and each grade is an integer in the string.
Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse
than O(records.length3) will fit within the execution time limit.
Example
•For records = ["John: 5", "Michael: 4", "Ruby: 2", "Ruby: 5", "Michael: 5"] , the output should
be solution(records) = "John" .
Let's calculate students' average grades:
•"John" = 5
•"Michael" = (4 + 5) / 2 = 4.5
•"Ruby" = (2 + 5) / 2 = 3.5 .
Since 5 > 4.5 > 3.5 , the result is "John" .
•For records = ["Kate: 5", "Kate: 5", "Maria: 2", "John: 5", "Michael: 4", "John: 4"] , the output
should be solution(records) = "Kate" .
•"Kate" = (5 + 5) / 2 = 5
•"Maria" = 2
•"John" = (5 + 4) / 2 = 4.5
•"Michael" = 4
Since 5 > 4.5 > 4 > 2 , the result is "Kate" .
Input/Output
•[memory limit] 1 GB
Guaranteed constraints:
1 ≤ records.length ≤ 100 ,
1 ≤ records[i] ≤ 20 .
•[output] string
Return the name of the student who has the highest average grade.
/******************************************************************************
•Write your code in this editor and press "Run" button to execute it.
•*******************************************************************************/
•import java.util.*;
• System.out.println("Hello World");
• int ans = 0;
• int n = mat.length;
• int m = mat[0].length;
• for(int i=0;i<n;i++){
• int x1 = arr[0];
• int y1 = arr[1];
• for(int j=i+1;j<n;j++){
• int x2 = brr[0];
• int y2 = brr[1];
• ans++;
• }
• }
• }
• System.out.println("ans: "+ans);
• }
•}
1.
Given an integer n, your task is to create a square frame of size n, represented as an array of strings.
The frame should consist of empty space, enclosed by lines made of * characters as follows:
********
* *
* *
* *
* *
* *
* *
********
Note: You are not expected to provide the most optimal solution, but a solution with time
complexity not worse than O(n3) will fit within the execution time limit.
Example
solution(n) = [
"********",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"********"
]
(This is the frame that was provided in the description)
For n = 5, the output should be
solution(n) = [
"*****",
"* *",
"* *",
"* *",
"*****"
]
For n = 2, the output should be
solution(n) = [
"**",
"**"
]
Input/Output
[memory limit] 1 GB
[input] integer n
Guaranteed constraints:
2 ≤ n ≤ 100.
[output] array.string
An array of strings representing the frame. The ith element of the resulting array corresponds to the
ith line of the frame.