-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'SnowScriptWinterOfCode:main' into Q5
- Loading branch information
Showing
15 changed files
with
557 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
## Python Code | ||
``` | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
curr_max, maxsofar = 0, 0 | ||
for i in range(1, len(prices)): | ||
curr_max += prices[i] - prices[i-1] | ||
curr_max = max(curr_max, 0) | ||
maxsofar = max(maxsofar, curr_max) | ||
return maxsofar | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
``` | ||
import java.util.Arrays; | ||
public class d13q3 { | ||
public static boolean eq(String str1, String str2) { | ||
char[] a = str1.toCharArray(); | ||
char[] b = str2.toCharArray(); | ||
Arrays.sort(a); | ||
Arrays.sort(b); | ||
String s = new String(a); | ||
String p = new String(b); | ||
return s.equals(p); | ||
} | ||
public static boolean checkInclusion(String s1, String s2) { | ||
if (s1.length() > s2.length()) { | ||
return false; | ||
} | ||
String temp = s2.substring(0, s1.length()); | ||
if (eq(s1, temp)) { | ||
return true; | ||
} | ||
int j = s1.length(); | ||
while (j < s2.length()) { | ||
temp = temp.substring(1); | ||
temp += s2.charAt(j); | ||
if (eq(s1, temp)) { | ||
return true; | ||
} | ||
j++; | ||
} | ||
return false; | ||
} | ||
public static void main(String[] args) { | ||
System.out.println(checkInclusion("ab","eidbaooo")); | ||
System.out.println(checkInclusion("ab","eidboaoo")); | ||
} | ||
} | ||
``` |
33 changes: 33 additions & 0 deletions
33
Day 18/q1: Maximum Difference Between Node and Ancestor/question.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b. | ||
|
||
A node a is an ancestor of b if either: any child of a is equal to b or any child of a is an ancestor of b. | ||
|
||
|
||
|
||
Example 1: | ||
|
||
Input: root = [8,3,10,1,6,null,14,null,null,4,7,13] | ||
Output: 7 | ||
|
||
![tmp-tree](https://github.com/SnowScriptWinterOfCode/LeetCode_Q/assets/97434896/108fd5df-8e90-4f6c-bbfc-f2e1e08b4503) | ||
|
||
Explanation: We have various ancestor-node differences, some of which are given below : | ||
|8 - 3| = 5 | ||
|3 - 7| = 4 | ||
|8 - 1| = 7 | ||
|10 - 13| = 3 | ||
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7. | ||
|
||
Example 2: | ||
|
||
Input: root = [1,null,2,null,0,3] | ||
Output: 3 | ||
|
||
![tmp-tree-1](https://github.com/SnowScriptWinterOfCode/LeetCode_Q/assets/97434896/bedf5d7f-cf61-40ee-bdcf-3a4a066abbc0) | ||
|
||
Constraints: | ||
|
||
The number of nodes in the tree is in the range [2, 5000]. | ||
0 <= Node.val <= 105 | ||
Problem link: https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/description/?envType=daily-question&envId=2024-01-11 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
``` | ||
import java.util.*; | ||
public class d11q3 { | ||
static class Node { | ||
int val; | ||
Node left; | ||
Node right; | ||
Node() { | ||
} | ||
Node(int val) { | ||
this.val = val; | ||
} | ||
Node(int val, Node left, Node right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
public static Node construct(int s, int e, int[] nums) { | ||
if (s > e) { | ||
return null; | ||
} | ||
int maxi = Integer.MIN_VALUE; | ||
int id = -1; | ||
for (int i = s; i <= e; i++) { | ||
if (nums[i] > maxi) { | ||
maxi = nums[i]; | ||
id = i; | ||
} | ||
} | ||
Node root = new Node(maxi); | ||
root.left = construct(s, id - 1, nums); | ||
root.right = construct(id + 1, e, nums); | ||
return root; | ||
} | ||
public static List<String> traverse(Node root) { | ||
Queue<Node> q = new LinkedList<>(); | ||
List<String> al = new ArrayList<>(); | ||
q.add(root); | ||
al.add(root.val + ""); | ||
while (q.size() > 0) { | ||
int n = q.size(); | ||
for (int i = 0; i < n; i++) { | ||
Node temp = q.remove(); | ||
if (temp.left != null) { | ||
al.add(temp.left.val + ""); | ||
q.add(temp.left); | ||
} | ||
if (temp.right != null) { | ||
al.add(temp.right.val + ""); | ||
q.add(temp.right); | ||
} | ||
} | ||
} | ||
return al; | ||
} | ||
public static void main(String[] args) { | ||
int[] nums = { 3, 2, 1, 6, 0, 5 }; | ||
Node root = construct(0, nums.length - 1, nums); | ||
List<String> ans = traverse(root); | ||
System.out.print("["); | ||
for (String str : ans) { | ||
System.out.print(str + ","); | ||
} | ||
System.out.print("]"); | ||
} | ||
} | ||
``` |
49 changes: 49 additions & 0 deletions
49
Day-14/q1: Amount of Time for Binary Tree to be Infected /Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
``` | ||
class Solution { | ||
public void convert(TreeNode root,int parent, HashMap<Integer,Set<Integer>> hm){ | ||
if(root==null){ | ||
return; | ||
} | ||
if(!hm.containsKey(root.val)){ | ||
hm.put(root.val,new HashSet<>()); | ||
} | ||
Set<Integer> hs=hm.get(root.val); | ||
if(parent!=0){ | ||
hs.add(parent); | ||
} | ||
if(root.left!=null){ | ||
hs.add(root.left.val); | ||
} | ||
if(root.right!=null){ | ||
hs.add(root.right.val); | ||
} | ||
convert(root.left,root.val,hm); | ||
convert(root.right,root.val,hm); | ||
} | ||
public int amountOfTime(TreeNode root, int start) { | ||
HashMap<Integer,Set<Integer>> hm= new HashMap<>(); | ||
convert(root,0,hm); | ||
Queue<Integer> q=new LinkedList<>(); | ||
q.add(start); | ||
int ans=0; | ||
Set<Integer> visited=new HashSet<>(); | ||
visited.add(start); | ||
while(q.size()>0){ | ||
int n=q.size(); | ||
for(int i=0;i<n;i++){ | ||
int curr=q.remove(); | ||
Set<Integer> temp=hm.get(curr); | ||
for(int key: temp){ | ||
if(!visited.contains(key)){ | ||
visited.add(key); | ||
q.add(key); | ||
} | ||
} | ||
} | ||
ans++; | ||
} | ||
return ans-1; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
``` | ||
class Solution { | ||
public TreeNode searchBST(TreeNode root, int val) { | ||
if(root==null){ | ||
return null; | ||
} | ||
TreeNode l=searchBST(root.left,val); | ||
TreeNode r=searchBST(root.right,val); | ||
if(l!=null && l.val==val){ | ||
return l; | ||
} | ||
if(r!=null && r.val==val){ | ||
return r; | ||
} | ||
if(root.val==val){ | ||
return root; | ||
} | ||
return null; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
## Intuition | ||
Recursively traversing the tree based on the BST Property; comparing data of nodes and value that we have to search. | ||
|
||
## Properties of a BST | ||
- The left subtree of a node contains only nodes with keys lesser than the node’s key. | ||
- The right subtree of a node contains only nodes with keys greater than the node’s key. | ||
- The left and right subtree each must also be a binary search tree. | ||
|
||
## Approach - Recursive | ||
### Two Base Cases | ||
1. if root is NULL, we return NULL ; | ||
2. if data of root matches the value to be searched, we return the node; | ||
### Recursive Case | ||
1. Recursive Traversal Statements based on comparison of node's data, the value to be searched, and the BST Property | ||
2. If data of node is greater than value, we search in the left subtree and OMIT right subtree | ||
3. Else, we search in the right subtree & OMIT the left subtree. | ||
|
||
## Complexity Analysis | ||
|
||
`Time complexity`: O(logN) | ||
|
||
`Space complexity`: O(N) | ||
|
||
## C++ Code | ||
``` | ||
class Solution { | ||
public: | ||
TreeNode* searchBST(TreeNode* root, int val) { | ||
// base case | ||
// if element not found or if element found | ||
if (root == NULL){ | ||
return NULL ; | ||
} | ||
if (root -> val == val){ | ||
return root ; | ||
} | ||
// recursive case | ||
// traversing left subtree | ||
if (root -> val > val){ | ||
return searchBST(root -> left, val) ; | ||
} | ||
// traversing right subtree | ||
else{ | ||
return searchBST(root -> right, val) ; | ||
} | ||
} | ||
}; | ||
``` | ||
## Approach - Iterative | ||
|
||
1. Create a temp node and make it equal to `root` | ||
2. Now, traverse till temp is NOT EQUAL to NULL | ||
3. While traversing | ||
- Compare temp's data and the `val` that we have to find. | ||
- Move to left or right child of temp based on the comparison using the BST Property. | ||
|
||
## Complexity Analysis | ||
`Time complexity`: O(logN) | ||
|
||
`Space complexity`: O(1) | ||
|
||
## C++ Code | ||
|
||
``` | ||
class Solution { | ||
public: | ||
TreeNode* searchBST(TreeNode* root, int val) { | ||
TreeNode *temp = root ; | ||
while(temp != NULL){ | ||
if (temp -> val == val){ | ||
return temp ; | ||
} | ||
if (temp -> val > val){ | ||
temp = temp -> left; | ||
} | ||
else{ | ||
temp = temp -> right ; | ||
} | ||
} | ||
return NULL ; | ||
} | ||
}; | ||
``` |
22 changes: 22 additions & 0 deletions
22
Day-15/q1-Lowest Common Ancestor of a Binary Tree/Anushreebasics--java.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
``` | ||
class Solution { | ||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | ||
if(root==null){ | ||
return null; | ||
} | ||
if(root==p || root==q){ | ||
return root; | ||
} | ||
TreeNode l=lowestCommonAncestor(root.left,p,q); | ||
TreeNode r=lowestCommonAncestor(root.right,p,q); | ||
if(l!=null && r!=null){ | ||
return root; | ||
} | ||
if(l!=null){ | ||
return l; | ||
} | ||
return r; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution { | ||
public: | ||
int lengthOfLIS(vector<int>& nums) { | ||
vector<int> temp; | ||
temp.push_back(nums[0]); | ||
int l = 1; | ||
|
||
for(int i=1;i<nums.size();i++){ | ||
if(temp.back() < nums[i]){ | ||
temp.push_back(nums[i]); | ||
l++; | ||
} | ||
else{ | ||
int ind = lower_bound(temp.begin(),temp.end(),nums[i]) - temp.begin(); | ||
temp[ind] = nums[i]; | ||
} | ||
} | ||
return l; | ||
} | ||
}; |
Oops, something went wrong.