diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..171e00fc --- /dev/null +++ b/Problem1.java @@ -0,0 +1,52 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + /* +102. Binary Tree Level Order Traversal +Ran on leetcode: Yes +TC: O(n) +SC: O(n) +At each level process the node and add the node to the list and add all of its children in the queue, do the same till we have processed aall the nodes + */ +class Solution { + + private List> result = new ArrayList<>(); + + public List> levelOrder(TreeNode root) { + bfs(root); + return result; + } + + private void bfs(TreeNode root){ + if(root == null) return; // if root node is null + Queue queue = new LinkedList<>(); + queue.offer(root); + while(!queue.isEmpty()){ // check if queue is empty + int size = queue.size(); + List sameLevel = new ArrayList<>(); + for(int i = 0; i < size; i++){ + TreeNode node = queue.poll(); + sameLevel.add(node.val); // add the node to the list + if(node.left != null){ // add left child if left child is not empty + queue.offer(node.left); + } + if(node.right != null){ // add right child if right child is not empty + queue.offer(node.right); + } + } + result.add(sameLevel); + } + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..dc427382 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,62 @@ +/* +207. Course Schedule +Ran on leetcode: Yes +TC:O(V+E) +SC:O(V+E) +Create adj map of for faster search, create indegrees array indicating how many courses is it dependent on, add to the queue all the courses with indegrees 0 and then mark it as completed and update indegrees array if the value becomes 0 for the course enqueue it. If the number of courses that can be completed is num of courses return true +*/ +class Solution { + public boolean canFinish(int numCourses, int[][] prerequisites) { + int[] indegress = new int[numCourses]; + Map> adjMap = new HashMap<>(); + Queue queue = new LinkedList<>(); + + for(int i = 0; i < prerequisites.length; i++){ + int dependent = prerequisites[i][0]; + int independent = prerequisites[i][1]; + // number of dependent on each course + indegress[dependent]++; + // Create map key = independent, value = list of dependents + adjMap.putIfAbsent(independent, new ArrayList<>()); + adjMap.get(independent).add(dependent); + } + + int count = 0; + for(int i = 0; i < indegress.length; i++){ // enqueue all the elements which are fully independent + if(indegress[i] == 0){ + count++; + queue.offer(i); + } + } + + if(count == numCourses){ // All the elements are enqueued, then all the courses can be completed + return true; + } + + if(count == 0){ // If no elements are enqueued, then there are no course from where we can start + return false; + } + + while(!queue.isEmpty()){ + int course = queue.poll(); + List dependents = adjMap.get(course); + if(dependents != null){ + int size = dependents.size(); + for(int i = 0; i < size; i++){ + int temp = dependents.get(i); + indegress[temp]--; // Dependent course completed so reduce indegree to it + if(indegress[temp] == 0){ // Course can be completed + queue.offer(temp); + count++; + if(count == numCourses){ // All courses can be completed + return true; + } + } + } + } + } + + return false; + + } +} \ No newline at end of file