0% found this document useful (0 votes)
630 views

Regret Java

Uploaded by

bhujbalgovind177
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
630 views

Regret Java

Uploaded by

bhujbalgovind177
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 12

3

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

laps = [["Harold 154", "Gina 155", "Juan 160"],


["Juan 152", "Gina 153", "Harold 160"],
["Harold 148", "Gina 150", "Juan 151"]]

The output should be solution(laps) = ["Juan", "Harold", "Gina"] .

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

laps = [["Gina 155", "Eddie 160", "Joy 161", "Harold 163"],


["Harold 151", "Gina 153", "Joy 160", "Eddie 160"],
["Harold 149", "Joy 150", "Gina 152", "Eddie 154"],
["Harold 148", "Gina 150", "Eddie 151", "Joy 155"]]

The output should be solution(laps) = ["Harold", "Eddie", "Joy", "Gina"] .

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

•[execution time limit] 3 seconds (java)

•[memory limit] 1 GB

•[input] array.array.string laps


An array of string arrays of driver's name and lap time. It is guaranteed that the same drivers will appear in every lap.
All laps[i] are guaranteed to be given in format " str(NAME) int(TIME) ".
Guaranteed constraints:
laps.length = laps[i].length ,
1 ≤ laps[i].length ≤ 100 ,
1 ≤ TIME ≤ 104 .

•[output] array.string

Return the list of drivers sorted in the order which they were eliminated, and ordered alphabetically in the case of ties.

/******************************************************************************

laps = [["Gina 155", "Eddie 160", "Joy 161", "Harold 163"],

["Harold 151", "Gina 153", "Joy 160", "Eddie 160"],

["Harold 149", "Joy 150", "Gina 152", "Eddie 154"],

["Harold 148", "Gina 150", "Eddie 151", "Joy 155"]]

The output should be solution(laps) = ["Harold", "Eddie", "Joy", "Gina"].

*******************************************************************************/

import java.util.*;

public class Main

public static void main(String[] args) {

String[][] laps = {{"Harold 154", "Gina 155", "Juan 160"},

{"Juan 152", "Gina 153", "Harold 160"},

{"Harold 148", "Gina 150", "Juan 151"}};

// String[][] laps = {{"Gina 155", "Eddie 160", "Joy 161", "Harold 163"},

// {"Harold 151", "Gina 153", "Joy 160", "Eddie 160"},

// {"Harold 149", "Joy 150", "Gina 152", "Eddie 154"},

// {"Harold 148", "Gina 150", "Eddie 151", "Joy 155"}};


int n = laps.length;

if(n==0){

System.out.println("");

int m = laps[0].length;

List<List<String>> al = new ArrayList<>();

HashSet<String> hs = new HashSet<>();

HashMap<String,Integer> hm = new HashMap<>();

for(int i=0;i<n;i++){

String[] rec = laps[i];

int max_number = -1;

for(int j=0;j<laps[i].length;j++){

String[] arr = laps[i][j].split(" ");

int grade = Integer.valueOf(arr[1]);

String name = arr[0];

if(!hs.contains(name)){

System.out.println("line 37: "+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

List<String> list = new ArrayList<>();

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(List<String> al2 : 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:

Where the green square is the collision box.


Note

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]
]

the output should be solution(centers) = 2 .

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]
]

the output should be solution(centers) = 4 .

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

•[execution time limit] 3 seconds (java)

•[memory limit] 1 GB

•[input] array.array.integer centers

An array of pairs of integers representing coordinates of the object. It is guaranteed that:

•The maximum absolute value of coordinate is 104 .


•The number of object is no more than 4⋅104 .
•Different objects can have the same positions.
Guaranteed constraints:
1 ≤ centers.length ≤ 4⋅104 ,
centers[i].length = 2 ,
-105 ≤ centers[i][j] ≤ 105 .

•[output] integer

The number of object pairs that collide.

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" .

Let's calculate students' average grades:

•"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

•[execution time limit] 3 seconds (java)

•[memory limit] 1 GB

•[input] array.string records

An array of strings representing students' names and their grades.

Guaranteed constraints:
1 ≤ records.length ≤ 100 ,
1 ≤ records[i] ≤ 20 .

•[output] string

Return the name of the student who has the highest average grade.

/******************************************************************************

• Online Java Compiler.

• Code, Compile, Run and Debug java program online.

•Write your code in this editor and press "Run" button to execute it.

•*******************************************************************************/

•import java.util.*;

•public class Main


•{

• public static void main(String[] args) {

• System.out.println("Hello World");

• int[][] mat = {{1, 1},{2, 2},{0, 4},{1,1}};

• int ans = 0;

• int n = mat.length;

• int m = mat[0].length;

• for(int i=0;i<n;i++){

• int[] arr = mat[i];

• int x1 = arr[0];

• int y1 = arr[1];

• for(int j=i+1;j<n;j++){

• int[] brr = mat[j];

• int x2 = brr[0];

• int y2 = brr[1];

• if(Math.abs(x2-x1)<=2 && Math.abs(y2-y1)<=2){

• 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

For n = 8, the output should be

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

[execution time limit] 3 seconds (java)

[memory limit] 1 GB

[input] integer n

The size of the frame.

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.

[Java] Syntax Tips

// Prints help message to the console


// Returns a string
//
// Globals declared here will cause a compilation error,
// declare variables inside the function instead!
String helloWorld(String name) {
System.out.println("This prints to the console when you Run Tests");
return "Hello, " + name;
}

You might also like