Skip to content

Commit 86b4251

Browse files
committed
july
1 parent 0b2d236 commit 86b4251

13 files changed

+863
-0
lines changed

July/Daily 11-07-23.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Keep a record of distance of target node from itself to root node, then traverse distances.
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
8+
Create a hashmap to store the distances of nodes in the path of target node to root node.
9+
10+
Then traverse from root node to check if asked distance(k) is equal to distance between target and that node. If not, then check node.left as well as node.right , after calling the same traverse function with incrementing distance by 1 . ( as by intuition we will be going away from target.)
11+
( But if we move closer to target node then don't worry , I have included the if statement to check is traversed node is in hashmap, if yes then update distance to the value of distance of that node in hashmap.)
12+
13+
# Complexity
14+
- Time complexity: $$O(n)$$
15+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
16+
17+
- Space complexity: $$O(n)$$
18+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
19+
20+
# Code
21+
```
22+
/**
23+
* Definition for a binary tree node.
24+
* public class TreeNode {
25+
* int val;
26+
* TreeNode left;
27+
* TreeNode right;
28+
* TreeNode(int x) { val = x; }
29+
* }
30+
*/
31+
class Solution {
32+
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
33+
List<Integer> li = new ArrayList<>();
34+
HashMap< TreeNode, Integer> dist = new HashMap<>();
35+
36+
getDists( root, target, dist);
37+
traverse( root, k, 0, dist, li);
38+
39+
return li;
40+
41+
}
42+
43+
static void getDists( TreeNode root, TreeNode target, HashMap< TreeNode, Integer> dist){
44+
45+
if( root == null){
46+
return;
47+
}
48+
if( root == target){
49+
dist.put(root, 0);
50+
return;
51+
}
52+
53+
getDists(root.left, target, dist);
54+
if( dist.containsKey(root.left)){
55+
dist.put(root, dist.get(root.left) + 1);
56+
return;
57+
}
58+
59+
getDists(root.right, target, dist);
60+
if( dist.containsKey(root.right)){
61+
dist.put(root, dist.get(root.right) + 1);
62+
return;
63+
}
64+
65+
}
66+
67+
static void traverse( TreeNode root, int k, int distance, HashMap< TreeNode, Integer> dist, List<Integer> li){
68+
if( root == null){
69+
return;
70+
}
71+
if( dist.containsKey(root)){
72+
distance = dist.get(root);
73+
}
74+
if( distance == k){
75+
li.add(root.val);
76+
}
77+
78+
traverse(root.left, k, distance + 1, dist, li);
79+
traverse(root.right, k, distance + 1, dist, li);
80+
}
81+
}
82+
```

July/Daily 12-07-23.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Visualise a little, it is just those nodes of the graph which aren't part of any cycle.
4+
5+
# Approach
6+
<!-- Describe your approach to solving the problem. -->
7+
For every node check if it is part of any cycle or not ( used a separate function for that ).
8+
9+
Used enum for storing the states of nodes, it was usefull in checking the presence of cycles.
10+
11+
Add all the nodes to answer list which aren't part of any cycles.
12+
13+
# Complexity
14+
- Time complexity : $$O( |V| + |E| )$$
15+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
16+
17+
- Space complexity : $$O( |V| + |E| )$$
18+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
19+
20+
# Code
21+
```
22+
class Solution {
23+
enum States { // These are special class that are used for storing cases // Ex: How are you : Good - Bad - Average ... it is just for example
24+
//Initial,
25+
Visiting,
26+
Visited
27+
}
28+
public List<Integer> eventualSafeNodes(int[][] graph) {
29+
List<Integer> li = new ArrayList<>();
30+
States[] state = new States[graph.length];
31+
32+
for( int i = 0; i < graph.length; i++){ // for every node check if it is part of any cycle or not
33+
if( !formsCycle( graph, i, state)){
34+
li.add(i);
35+
}
36+
}
37+
return li;
38+
39+
}
40+
41+
public boolean formsCycle( int[][] g, int n, States[] state){
42+
43+
if( state[n] == States.Visiting){ // if we recursively reach the node which was in our path then we have found a cycle , so return true
44+
return true;
45+
}
46+
if( state[n] == States.Visited){ // it means we reached a terminal node , so return false
47+
return false;
48+
}
49+
50+
state[n] = States.Visiting; // initialise state[0] to be as a visiting state
51+
for( int e : g[n]){ // for every outgoing edge of present node , check if the forms a cycle or not
52+
if( formsCycle( g, e, state)){
53+
return true;
54+
}
55+
}
56+
state[n] = States.Visited; // this code will execute if we reach terminal node
57+
58+
return false;
59+
}
60+
}
61+
```

July/Daily 13-07-23.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Intuition was pretty similar to 'Find Eventual Safe States(802)(Yesterday's daily challenge)'.
4+
5+
Simply they don't have to form cycles.
6+
7+
Anyway it required a visualisation that courses that was prerequisite of other course(s) must have to be completed before, and in order to do that they must be not required to complete those courses whom they are prerequisite of.
8+
9+
10+
11+
# Approach
12+
<!-- Describe your approach to solving the problem. -->
13+
Used List to store courses and their prerequisite(s).
14+
15+
For every course check if it is part of any loop or not ( used a separate function for that ).
16+
17+
Used enum for storing the status of course, it was usefull in checking the presence of loops.
18+
19+
[ If something is not understandable, then please let me know in comments, I will explain that further. ] :)
20+
21+
# Complexity
22+
23+
- Time complexity : $$O( ∣N∣ + ∣P∣ )$$
24+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
25+
26+
- Space complexity : $$O( ∣N∣ + ∣P∣ )$$
27+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
28+
29+
where , N = numCourses : P = prerequisites.length ( rows )
30+
# Code
31+
```
32+
enum Course{
33+
Completing,
34+
Completed
35+
}
36+
37+
class Solution {
38+
public boolean canFinish(int numCourses, int[][] prerequisites) {
39+
Course[] courses = new Course[numCourses];
40+
List<Integer>[] li = new List[numCourses];
41+
for( int i = 0; i < numCourses; i++){
42+
li[i] = new ArrayList<>();
43+
}
44+
for( int[] pre : prerequisites){
45+
li[ pre[0]].add( pre[1]);
46+
}
47+
for( int i = 0; i < numCourses; i++){
48+
if( checkLoop( i, li, courses)){
49+
return false;
50+
}
51+
}
52+
return true;
53+
}
54+
55+
boolean checkLoop( int a, List<Integer>[] li, Course[] c){
56+
57+
if( c[a] == Course.Completing){
58+
return true;
59+
}
60+
if( c[a] == Course.Completed){
61+
return false;
62+
}
63+
64+
c[a] = Course.Completing;
65+
for( int p : li[a]){
66+
if( checkLoop( p, li, c)){
67+
return true;
68+
}
69+
}
70+
c[a] = Course.Completed;
71+
return false;
72+
}
73+
}
74+
```

July/Daily 14-07-23.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
A simple use of hashmap.
4+
5+
6+
# Approach
7+
<!-- Describe your approach to solving the problem. -->
8+
Create a hashmap which will store numbers with their respective series possible till now, and for each numbers check if their previous elements were present in array or not, if yes then increse their count, otherwise let it be 1.
9+
10+
Let me explain by a example : a = [1,5,7,8,5,3,4,2,1], difference = -2
11+
Let max store the maximum length and initially its value is 0.
12+
13+
Star with first element : 1
14+
As ( 1 - (-2 ) ) isn't present in hashmap ( since it is the first element in our hashmap), so its length ( 0 + 1) = 1
15+
Included in hashmap : (1, 1)
16+
As 1 > max : max = 1
17+
18+
Second element : 5
19+
As ( 5 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1
20+
Included in hashmap : (5, 1)
21+
As 1 isn't greater than max, so no change in max
22+
23+
Third element : 7
24+
As ( 7 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1
25+
Included in hashmap : (7, 1)
26+
As 1 isn't greater than max, so no change in max
27+
28+
Fourth element : 8
29+
As ( 8 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1
30+
Included in hashmap : (8, 1)
31+
As 1 isn't greater than max, so no change in max
32+
33+
Fifth element : 5
34+
As ( 5 - (-2) ) = (7) is present in hashmap, so its length = ( length(7) + 1) = ( 1 + 1) = 2
35+
Included in hashmap : (5, 2)
36+
As 2 > max : max = 2
37+
38+
Sixth element : 3
39+
As ( 3 - (-2) ) = (5) is present in hashmap, so its length = ( length(5) + 1 ) = ( 2 + 1) = 3
40+
Included in hashmap : (3, 3)
41+
As 3 > max : max = 3
42+
43+
Seventh element : 4
44+
As ( 4 - (-2) ) isn't present in hashmap, so its length = ( 0 + 1) = 1
45+
Included in hashmap : (4, 1)
46+
As 1 isn't greater than max, so no change in max
47+
48+
Eigth element : 2
49+
As ( 2 - (-2) ) = (4) is present in hashmap, so its length = ( length(4) + 1 ) = ( 1 + 1) = 2
50+
Included in hashmap : (2, 2)
51+
As 2 isn't greater than max, so no change in max
52+
53+
Ninth element : 1
54+
As (1 - (-2) ) = (3) is present in hashmap, so its length = ( length(3) + 1 ) = ( 3 + 1) = 4
55+
Included in hashmap : (1, 4)
56+
As 4 > max : max = 4
57+
58+
After iterating over all numbers, answer is received as max.
59+
60+
61+
# Complexity
62+
- Time complexity:
63+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
64+
65+
- Space complexity:
66+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
67+
68+
# Code
69+
```
70+
class Solution {
71+
public int longestSubsequence(int[] arr, int difference) {
72+
73+
int max = 0;
74+
HashMap<Integer, Integer> m = new HashMap<>();
75+
for( int i : arr){
76+
m.put(i, m.getOrDefault( i - difference, 0) + 1); // if the previous possible element of the sequence is present already in hashmap then add thats length to the current element length + 1, otherwise 1
77+
max = Math.max( max, m.get(i)); // updating max value if length is greater than max
78+
}
79+
return max;
80+
81+
}
82+
}
83+
```

July/Daily 15-07-23.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Intuition
2+
<!-- Describe your first thoughts on how to solve this problem. -->
3+
Have to check each rows, seemed complex but breaked it down.
4+
5+
6+
# Approach
7+
<!-- Describe your approach to solving the problem. -->
8+
Sort events on the basis of their end date.
9+
10+
Create a matrix which will store maximum values that can be achieved till now.
11+
12+
Now iterate over each sorted events :
13+
14+
if an event is found that started before ( previous variable in code ) the current iterating event then check that if its value can be added to current
15+
else add the value of that event or previous row value in matrix, whichever is greater.
16+
17+
The last element of our created matrix will contain the answer.
18+
19+
Look at the code and still if you have any confusion then please let me know in comments. :)
20+
21+
# Complexity
22+
- Time complexity : $$O(n*(logn)*k)$$
23+
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
24+
25+
- Space complexity: $$O(nk)$$
26+
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
27+
28+
# Code
29+
```
30+
class Solution {
31+
public int maxValue(int[][] events, int k) {
32+
33+
Arrays.sort( events, ( int[] e1, int[] e2) -> e1[1] - e2[1]); //sorting events on the basis of their end date
34+
int len = events.length;
35+
int[][] m = new int[len+1][ k+1]; // create a matrix which will store maximum values that can be achieved till now
36+
for( int i = 0; i < len; i++){
37+
int startday = events[i][0], value = events[i][2];
38+
int previous = findprevious( events, i, startday); // previous will store any event that start date is before the current event
39+
for( int j = 1; j <= k; j++){
40+
m[ i+1][j] = Math.max( value + m[previous][j-1], m[i][j]); // if a event has started and ended before current event then add that value to the value of current event ; return which is bigger this updated value or the value at previous event
41+
}
42+
}
43+
return m[len][k];
44+
}
45+
46+
public int findprevious( int[][] events, int beforethis, int startday){ // binary search to find previous event
47+
int l = 0, b = beforethis;
48+
while( l < b){
49+
int m = ( l + b)/2;
50+
if( events[m][1] >= startday){
51+
b = m;
52+
}
53+
else{
54+
l = m + 1;
55+
}
56+
}
57+
return l;
58+
}
59+
}
60+
```

0 commit comments

Comments
 (0)