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

Amarnath Java 9

The document contains code solutions for four programming problems: finding the longest substring without repeating characters, determining the next greater element for an array, displaying a table of food orders in a restaurant, and finding minimum height trees in a graph. Each solution is presented in Java, demonstrating the use of data structures like HashSet, HashMap, and Queue. The document includes the code and a brief description of each problem's approach.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Amarnath Java 9

The document contains code solutions for four programming problems: finding the longest substring without repeating characters, determining the next greater element for an array, displaying a table of food orders in a restaurant, and finding minimum height trees in a graph. Each solution is presented in Java, demonstrating the use of data structures like HashSet, HashMap, and Queue. The document includes the code and a brief description of each problem's approach.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Day-9

Name –Amarnath Choudhary

Uid-21BCS9971

Sub - Java

Section- CC-627 / B

Q.1 Longest Substring Without Repeating Characters


Code:
class Solution {
public int lengthOfLongestSubstring(String s)
{int n = s.length();
int maxLength = 0;
Set<Character> charSet = new
HashSet<>(); int left = 0;
for (int right = 0; right < n; right++) {
if (!charSet.contains(s.charAt(right)))
{ charSet.add(s.charAt(right));
maxLength = Math.max(maxLength, right - left + 1); }
else { while
(charSet.contains(s.charAt(right))) {
charSet.remove(s.charAt(left)); left++;
}
charSet.add(s.charAt(right));
}
}

return maxLength;
}
}
Output:
Q.2 Next Greater Element I
Code:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2)
{HashMap<Integer, Integer> hMap = new HashMap<>();
Stack<Integer> st = new Stack<>(); for (int i =
nums2.length - 1; i >= 0; i--) { while (!st.isEmpty()
&& st.peek() <= nums2[i]) st.pop(); if
(!st.isEmpty()) hMap.put(nums2[i], st.peek());
else hMap.put(nums2[i], -1);
st.push(nums2[i]);
} for (int i = 0; i < nums1.length; i++)
{nums1[i] =
hMap.get(nums1[i]);
}
return nums1;
}
}
Output:
Q.3 Display Table of Food Orders in a Restaurant
Code:
class Solution { public List<List<String>> displayTable(final
List<List<String>> orders) { final TreeMap<Integer, Map<String,
Integer>> tables = new TreeMap<>(); final TreeSet<String> meals =
new TreeSet<>();
for(final List<String> order : orders) { final int
tableNum = Integer.valueOf(order.get(1));
tables.putIfAbsent(tableNum, new
HashMap<>());
final Map<String, Integer> table = tables.get(tableNum);

table.put(order.get(2), table.getOrDefault(order.get(2), 0) + 1);

meals.add(order.get(2));
}

final List<List<String>> result = new ArrayList<>();

result.add(new ArrayList<>());
result.get(0).add("Table");
result.get(0).addAll(meals);

for(final int tableNum : tables.keySet()) {


final List<String> tableOrders = new ArrayList<>();
tableOrders.add(String.valueOf(tableNum)); final
Map<String, Integer> table =
tables.get(tableNum);

for(int i = 1; i < result.get(0).size(); ++i) {


final String meal = result.get(0).get(i);
tableOrders.add(table.containsKey(meal) ?
String.valueOf(table.get(meal)) : "0");
}

result.add(tableOrders);
}

return result;
}
}
Output:

Q.4 Minimum Height Trees


Code:
class Solution { public List<Integer> findMinHeightTrees(int
n, int[][] edges)
{if(n == 1) return Collections.singletonList(0);

int ind[] = new int[n];


Map<Integer, List<Integer>> map = new HashMap();
for(int[] edge: edges) {
ind[edge[0]]++; ind[edge[1]]++;
map.putIfAbsent(edge[0], new
ArrayList()); map.putIfAbsent(edge[1],
new ArrayList());
map.get(edge[0]).add(edge[1]);
map.get(edge[1]).add(edge[0]);
}

Queue<Integer> queue = new


LinkedList(); for(int
i=0;i<ind.length;i++) { if(ind[i] == 1) {
queue.add(i);
}
}

int processed = 0;
while(n - processed > 2)
{int size = queue.size();
processed += size;
for(int i=0;i<size;i++) {
int poll =
queue.poll(); for(int
adj: map.get(poll))
{if(--ind[adj] == 1) {
queue.add(adj);
}
}
}
}

List<Integer> list = new


ArrayList(); list.addAll(queue);
return list;
}
}
Output:

You might also like