0% found this document useful (0 votes)
4 views7 pages

Problem Statement

The document outlines three algorithmic problems: finding the longest consecutive sequence in an array, merging overlapping intervals, and generating a skyline from building data. Each problem is accompanied by a clear problem statement, understanding, algorithm steps, and Java code implementations. The outputs demonstrate the results of the algorithms applied to sample inputs.

Uploaded by

Nishant Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Problem Statement

The document outlines three algorithmic problems: finding the longest consecutive sequence in an array, merging overlapping intervals, and generating a skyline from building data. Each problem is accompanied by a clear problem statement, understanding, algorithm steps, and Java code implementations. The outputs demonstrate the results of the algorithms applied to sample inputs.

Uploaded by

Nishant Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Problem Statement:

Given an unsorted array of integers nums, return the length of the longest
consecutive elements sequence.

Problem Understanding:

We have a bunch of numbers all mixed up. We want to see how long a chain of
numbers we can make where each number is just one more than the previous
one.

Here is Algorithm:

1. Create an empty list called hm to remember each number.

2. Go through each number in the list you received.

3. Remember Numbers:
 Remember each number in the list you got earlier.

4. Check for Nearby Numbers:


 Look at each number again.
 If the number before it is also remembered, then don't remember it
as the start of a new group.

5. Find Groups:
 Start counting from the remembered numbers.
 Count the numbers next to each remembered number until you
can't find any more remembered numbers.
 Record the length of each group of consecutive numbers.

6. Longest Group:
 Check all the recorded group lengths.
 Find the longest group.

7. Give back the length of the longest group you found.

Code:

class Solution {
public int longestConsecutive(int[] nums) {
HashMap<Integer, Boolean> hm = new HashMap<>();
for(int i =0; i <nums.length; i++)
hm.put(nums[i], true);

for(int i =0; i <nums.length; i++){


if(hm.containsKey(nums[i] - 1)){
hm.put(nums[i], false);
}
}

int max = 0;
for(Integer key : hm.keySet()){
if(hm.get(key) == true){
max = Math.max(max, findLength(hm, key));
}
}
return max;
}

private static int findLength(HashMap<Integer, Boolean> hm, int k){


int ans = 0;
while(hm.containsKey(k)){
ans++;
k++;
}
return ans;
}

public static void main(String[] args) {


int nums[] = {9, 8, 7, 5, 1, 2, 3, 4};
Solution solution = new Solution();
int result = solution.longestConsecutive(nums);
System.out.println("Length of the longest consecutive sequence: " +
result);
}
}

Output :

Given input as array: 9, 8, 7, 5, 1, 2, 3, 4


Length of the largest consecutive sequence: 5

Problem Statement:

If we have given range of intervals. Merge all overlapping intervals, and


return an array of non-overlapping intervals which cover all the intervals
in the given input. We have to see the range or sequence in between 2
array elements.

Like [1,3] so the interval and segment will be 1,2,3


Like [2,6] so the interval and segment will be 2,3,4,5,6

Problem Understanding:

In this problem, we need to combine all the overlapping sections in an array.


This means each section should smoothly connect with the next one without any
overlap.

For example:

Input: intervals = [[1,3],[2,7],[9,12],[16,18]]


Output: [[1,7],[9,12],[16,18]]
Since intervals [1,3] and [2,6] overlap, merge them into [1,7].

Here is Algorithm:

1.First, sort the intervals based on the start time.


2.Merge Overlapping Intervals:
 Create an ArrayList to store merged intervals.
 Iterate through each interval in the sorted intervals.
 If the list is empty, add the current interval.
 If the current interval overlaps with the last interval in the list,
update the end time of the last interval to the maximum of its
current end time and the end time of the current interval.
 If the current interval doesn't overlap, add it to the list.

3.Convert the ArrayList to a 2D array and return it.


Code:

import java.util.*;

class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a,b)-> Integer.compare(a[0],b[0]));

ArrayList<int[]> mergedIntervals = new ArrayList<>();

for(int[] interval: intervals){


if(mergedIntervals.size() == 0 || interval[0] >
mergedIntervals.get(mergedIntervals.size() - 1)[1]){
mergedIntervals.add(interval);
} else {
mergedIntervals.get(mergedIntervals.size() - 1)[1] =
Math.max(mergedIntervals.get(mergedIntervals.size() - 1)[1], interval[1]);
}
}
return mergedIntervals.toArray(new int[mergedIntervals.size()][]);
}
}

public class Main {


public static void main(String[] args) {
Solution solution = new Solution();

int[][] intervals1 = {{1,3},{2,7},{9,12},{16,18}};


int[][] mergedIntervals1 = solution.merge(intervals1);
System.out.println("Merged intervals: " +
Arrays.deepToString(mergedIntervals1));

int[][] intervals2 = {{1,4},{4,5}};


int[][] mergedIntervals2 = solution.merge(intervals2);
System.out.println("Merged intervals: " +
Arrays.deepToString(mergedIntervals2));
}
}
Output:

Merged intervals: [[1, 7], [9, 12], [15, 18]]


Merged intervals: [[1, 5]]

Problem Statement:
Imagine you are looking at a city from far away.you see a line made by the tops
of all buildings. This line is called the city’s skyline,if you know where each
building is and how tall it is,can you draw this skyline? Given the locations and
heights of all the buildings, return the skyline formed by these buildings
collectively.
The geometric information of each building is given:

Problem understanding:
Every point in the skyline represents the beginning of a horizontal line that
finishes at the intersection of the last building and the ground level. Avoid
merging consecutive horizontal lines of the same height.
In other words we can say that the aim is to create a skyline based on the given
list of buildings. It uses a linked list structure to manage the buildings and their
heights efficiently.

Here is Algorithm:

Code:

import java.util.*;

class Solution {
public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> result = new ArrayList<>();
List<int[]> heights = new ArrayList<>();
for (int[] b : buildings) {
heights.add(new int[] {b[0], -b[2]});
heights.add(new int[] {b[1], b[2]});
}

Collections.sort(heights, (a, b) -> {


if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return a[1] - b[1];
}
});

PriorityQueue<Integer> pq = new
PriorityQueue<>(Collections.reverseOrder());
pq.offer(0);

int prev = 0;
for (int[] h : heights) {
if (h[1] < 0) {
pq.offer(-h[1]);
} else {
pq.remove(h[1]);
}
int curr = pq.peek();
if (prev != curr) {
List<Integer> point = new ArrayList<>();
point.add(h[0]);
point.add(curr);
result.add(point);
prev = curr;
}
}

return result;
}
}

public class Main {


public static void main(String[] args) {
Solution solution = new Solution();
int[][] buildings1 = {{2,9,10},{3,7,15},{5,12,12},{15,20,10},{19,24,8}};
List<List<Integer>> skyline1 = solution.getSkyline(buildings1);
System.out.println("Skyline 1: " + skyline1);

int[][] buildings2 = {{0,2,3},{2,5,3}};


List<List<Integer>> skyline2 = solution.getSkyline(buildings2);
System.out.println("Skyline 2: " + skyline2);
}
}

Output:

Skyline 1: [[2, 10], [3, 15], [7, 12], [12, 0], [15, 10], [20, 8], [24, 0]]
Skyline 2: [[0, 3], [5, 0]]

You might also like