Skip to content

Commit a6f549c

Browse files
committed
New Problems
New Problems
1 parent ccdf449 commit a6f549c

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed
1.52 KB
Binary file not shown.

src/chapter03linkedlists/CommonElementsInSortedLinkedLists.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*
1212
*/
1313

14-
1514
package chapter03linkedlists;
1615

1716
public class CommonElementsInSortedLinkedLists {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*Copyright (c) Feb 22, 2016 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : SkyLineBruteForce.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter18divideandconquer;
16+
17+
import java.util.Scanner;
18+
19+
public class SkyLineBruteForce {
20+
public static void main(String[] args) {
21+
int[] auxHeights = new int[1000];
22+
int rightMostBuildingRi = 0;
23+
@SuppressWarnings("resource")
24+
Scanner in = new Scanner(System.in);
25+
while (in.hasNext()) {
26+
int left = in.nextInt(), h = in.nextInt(), right = in.nextInt();
27+
for (int i = left; i < right; i++)
28+
auxHeights[i] = Math.max(h, auxHeights[i]);
29+
if(rightMostBuildingRi<right)
30+
rightMostBuildingRi=right;
31+
}
32+
int prev=0, left=-1, right = -1;
33+
for (int i = 0; i < rightMostBuildingRi; i++) {
34+
if (prev != auxHeights[i]) {
35+
if(left > 0) System.out.print(left +" "+ right + " ");
36+
prev = auxHeights[i];
37+
left =i;
38+
right =prev;
39+
}
40+
}
41+
System.out.println(left +" "+ right );
42+
}
43+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*Copyright (c) Feb 22, 2016 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : SkylinesDivideandConquer.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter18divideandconquer;
16+
class Building{
17+
int left, right, h;
18+
public Building(int x1, int h1, int x2) { left = x1; h = h1; right = x2; }
19+
}
20+
class strip{
21+
int left, h;
22+
strip(int x1, int h1) { left = x1; h = h1; }
23+
}
24+
class Skyline{
25+
strip[] strips;
26+
public int Count;
27+
int StartLoc;
28+
public Skyline(int n) {
29+
Count = 0; StartLoc = 0; strips = new strip[n];
30+
}
31+
public void Append(strip str) {
32+
strips[StartLoc+Count] = str; Count++;
33+
}
34+
public strip Head() {
35+
return strips[StartLoc];
36+
}
37+
public strip RemoveHead(){
38+
strip str = strips[StartLoc];
39+
Count--; StartLoc++;
40+
return str;
41+
}
42+
public String ToString(){
43+
String str = "";
44+
for(int i = StartLoc; i < StartLoc+Count; i++){
45+
if (i > StartLoc)
46+
str = str + ",";
47+
str = str + strips[i].left + "," + strips[i].h;
48+
}
49+
return "(" + str + ")";
50+
}
51+
}
52+
public class SkylinesDivideandConquer {
53+
static Skyline GetSkyline(Building[] buildings, int start, int end){
54+
if (start == end){
55+
Skyline skl = new Skyline(2);
56+
skl.Append(new strip(buildings[start].left, buildings[start].h) );
57+
skl.Append(new strip(buildings[start].right, 0));
58+
return skl;
59+
}
60+
int mid = (start+end)/2;
61+
Skyline sk1 = GetSkyline(buildings, start, mid);
62+
Skyline sk2 = GetSkyline(buildings, mid+1, end);
63+
return MergeSkylines(sk1, sk2);
64+
}
65+
static Skyline MergeSkylines(Skyline SK1, Skyline SK2){
66+
Skyline newSkl = new Skyline(SK1.Count + SK2.Count); // Allocate array space
67+
int currentHeight1 = 0; int currentHeight2 = 0;
68+
while ((SK1.Count > 0) && (SK2.Count > 0))
69+
if (SK1.Head().left < SK2.Head().left){
70+
int CurX = SK1.Head().left;
71+
currentHeight1 = SK1.Head().h;
72+
int MaxH = currentHeight1;
73+
if (currentHeight2 > MaxH) MaxH = currentHeight2;
74+
newSkl.Append(new strip(CurX, MaxH));
75+
SK1.RemoveHead();
76+
}
77+
else{
78+
int CurX = SK2.Head().left;
79+
currentHeight2 = SK2.Head().h;
80+
int MaxH = currentHeight1;
81+
if (currentHeight2 > MaxH) MaxH = currentHeight2;
82+
newSkl.Append(new strip(CurX, MaxH));
83+
SK2.RemoveHead();
84+
}
85+
while (SK1.Count > 0){
86+
strip str = SK1.RemoveHead(); newSkl.Append(str);
87+
}
88+
while (SK2.Count > 0){
89+
strip str = SK2.RemoveHead(); newSkl.Append(str);
90+
}
91+
return newSkl;
92+
}
93+
}

0 commit comments

Comments
 (0)