Skip to content

Commit 2520043

Browse files
committed
Added 2 solutions
1 parent 46e7ad8 commit 2520043

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
6+
public class Solution {
7+
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in);
10+
scanner.nextInt();
11+
List<Integer> heights = new ArrayList<>();
12+
while(scanner.hasNext()) {
13+
heights.add(scanner.nextInt());
14+
}
15+
System.out.println(areaOfLargestRectangle(heights));
16+
}
17+
18+
public static int areaOfLargestRectangle(List<Integer> heights) {
19+
heights.add(0);
20+
Stack<Integer> stack = new Stack<>();
21+
int maxAreaSoFar = -1;
22+
for(int height : heights) {
23+
int n = 0;
24+
while(!stack.isEmpty() && stack.peek() > height) {
25+
n++;
26+
maxAreaSoFar = Math.max(maxAreaSoFar, n* stack.pop());
27+
}
28+
for(int i = 0; i < n+1; i++) {
29+
stack.push(height);
30+
}
31+
}
32+
return maxAreaSoFar;
33+
}
34+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import java.util.*;
2+
3+
class Student implements Comparable {
4+
private int id;
5+
private String fname;
6+
private double cgpa;
7+
public Student(int id, String fname, double cgpa) {
8+
super();
9+
this.id = id;
10+
this.fname = fname;
11+
this.cgpa = cgpa;
12+
}
13+
public int getId() {
14+
return id;
15+
}
16+
public String getFname() {
17+
return fname;
18+
}
19+
public double getCgpa() {
20+
return cgpa;
21+
}
22+
23+
public int compareTo(Object o){
24+
Student s=(Student)o;
25+
if(cgpa==s.cgpa){
26+
if(fname.equals(s.fname)){
27+
return id-s.id;
28+
}else{
29+
return fname.compareTo(s.fname);
30+
}
31+
}else{
32+
if(s.cgpa-cgpa>0)
33+
return 1;
34+
else
35+
return -1;
36+
}
37+
}
38+
}
39+
40+
//Complete the code
41+
public class Solution {
42+
public static void main(String[] args){
43+
Scanner in = new Scanner(System.in);
44+
int testCases = Integer.parseInt(in.nextLine());
45+
46+
List<Student> studentList = new ArrayList<Student>();
47+
while(testCases>0){
48+
int id = in.nextInt();
49+
String fname = in.next();
50+
double cgpa = in.nextDouble();
51+
52+
Student st = new Student(id, fname, cgpa);
53+
studentList.add(st);
54+
55+
testCases--;
56+
}
57+
58+
Collections.sort(studentList);
59+
60+
for(Student st: studentList){
61+
System.out.println(st.getFname());
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)