Skip to content

Commit cbc4c30

Browse files
authored
Merge pull request #1 from batCoder95/branch1
Branch1
2 parents 534f310 + 5491ba1 commit cbc4c30

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

BalancedParantheses.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Created by Vatsal Gosaliya on 15-Jul-16.
3+
*
4+
* PROBLEM: Check if the given string contains a balanced pattern of parantheses.
5+
*/
6+
7+
import java.util.Stack;
8+
class BalancedParantheses {
9+
10+
public boolean solution(String a){
11+
int size = a.length();
12+
int check = 0;
13+
Stack s = new Stack();
14+
15+
for(int i=0;i<size;i++){
16+
if(a.charAt(i)!='(' && a.charAt(i)!=')') check = -1;
17+
if(a.charAt(i)=='(') s.push(a.charAt(i));
18+
else{
19+
if(!s.empty()) s.pop();
20+
else return false;
21+
}
22+
}
23+
if(check==-1) return false;
24+
else{
25+
if(s.empty()) return true;
26+
else return false;
27+
}
28+
29+
}
30+
public static void main(String[] args) {
31+
String a = "((()))";
32+
boolean result = new BalancedParantheses().solution(a);
33+
System.out.println(result);
34+
35+
}
36+
37+
}

FrogJumps.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by Vatsal on 15-Jul-16.
3+
* PROBLEM: Given the current position(x) of a frog and destination(y),
4+
* compute the number of jumps required to reach from x to y,
5+
* if the covers D blocks in a single jump.
6+
*/
7+
8+
class FrogJumps {
9+
10+
public int solution(int x, int y, int D){
11+
int jumps;
12+
13+
if(x==y) return 0;
14+
15+
else{
16+
int distance = Math.abs((y-x));
17+
if(distance%D==0) jumps = distance/D;
18+
else jumps = (distance/D)+1;
19+
20+
return jumps;
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
FrogJumps t = new FrogJumps();
26+
int jumps = t.solution(0, 10, 3);
27+
System.out.println("No. of jumps = "+jumps);
28+
29+
30+
}
31+
32+
}

OverlappingRectangles.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Created by Vatsal Gosaliya on 15-Jul-16.
3+
*
4+
* PROBLEM: Given are two rectangles defined over the 2-D cartesian coordinate system.
5+
* Each rectangle is expressed using the top-left corner, width and height.
6+
* The aim is to detect whether they overlap, and compute the overlapping
7+
* area, if they do.
8+
*/
9+
10+
class OverlappingRectangles {
11+
private double x,y,width,height;
12+
13+
OverlappingRectangles(double x, double y, double width, double height){
14+
this.x = x;
15+
this.y = y;
16+
this.width = width;
17+
this.height = height;
18+
}
19+
20+
OverlappingRectangles intersection(OverlappingRectangles rect2) {
21+
double newX = Math.max(this.x, rect2.x);
22+
double newY = Math.min(this.y, rect2.y);
23+
//System.out.println("newX = "+newX);
24+
//System.out.println("newY = "+newY);
25+
26+
double newWidth = Math.min(this.x + this.width, rect2.x + rect2.width) - newX;
27+
double newHeight = newY - Math.max(this.y - this.height, rect2.y - rect2.height);
28+
//System.out.println("newWidth = "+newWidth);
29+
//System.out.println("newHeight = "+newHeight);
30+
31+
if (newWidth <= 0d || newHeight <= 0d){
32+
System.out.print("No intersection.");
33+
return null;
34+
}
35+
36+
return new OverlappingRectangles(newX, newY, newWidth, newHeight);
37+
}
38+
39+
40+
public double getArea(){
41+
return this.width*this.height;
42+
}
43+
44+
public static void main(String args[]){
45+
OverlappingRectangles r1 = new OverlappingRectangles(2,8,3,4);
46+
OverlappingRectangles r2 = new OverlappingRectangles(1,7,6,2);
47+
OverlappingRectangles r = r1.intersection(r2);
48+
double areaOfIntersection = r == null ? 0 : r.getArea();
49+
System.out.print("Area of intersection : "+areaOfIntersection);
50+
}
51+
}

0 commit comments

Comments
 (0)