Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HACKTOBERFEST-2022 #140

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions 1340-JUMP GAME V SOLUTION.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:int d;
int max(int a,int b){
return a>b?a:b;
}
int fun(vector< int>&v,int i,vector< int>&dp){
if(i>=v.size()||i<0)return 0;
if(dp[i]!=-1)return dp[i];
int res=0;
int j;
for(j=i+1;j<v.size()&&v[j]<v[i]&&j<=i+d;j++){
res=max(res,1+fun(v,j,dp));
}
for(j=i-1;j>=0&&v[j]<v[i]&&j>=i-d;j--){
res=max(res,1+fun(v,j,dp));
}
return dp[i]=res;


}


int maxJumps(vector<int>& arr,int d1) {
// vector<int>val,a;
d=d1;


int ans=0;vector<int>dp(arr.size(),-1);
for(int i=0;i<arr.size();i++){
ans=max(ans,fun(arr,i,dp));
}
return ans+1;
}
};
45 changes: 45 additions & 0 deletions 1340-JUMP GAME V.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Given an array of integers arr and an integer d. In one step you can jump from index i to index:

i + x where: i + x < arr.length and 0 < x <= d.
i - x where: i - x >= 0 and 0 < x <= d.
In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)).

You can choose any index of the array and start jumping. Return the maximum number of indices you can visit.

Notice that you can not jump outside of the array at any time.


Example 1:
Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2
Output: 4
Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown.
Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9.
Similarly You cannot jump from index 3 to index 2 or index 1.



Example 2:

Input: arr = [3,3,3,3,3], d = 3
Output: 1
Explanation: You can start at any index. You always cannot jump to any index.




Example 3:

Input: arr = [7,6,5,4,3,2,1], d = 1
Output: 7
Explanation: Start at index 0. You can visit all the indicies.






Constraints:

1 <= arr.length <= 1000
1 <= arr[i] <= 10^5
1 <= d <= arr.length
45 changes: 45 additions & 0 deletions 1466- Reorder Routes to Make All Paths Lead to the City Zero.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.

Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.

This year, there will be a big event in the capital (city 0), and many people want to travel to this city.

Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.

It's guaranteed that each city can reach city 0 after reorder.




Example 1:


Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
Output: 3
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).


Example 2:


Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
Output: 2
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).





Example 3:

Input: n = 3, connections = [[1,0],[2,0]]
Output: 0


Constraints:

2 <= n <= 5 * 10^4
connections.length == n - 1
connections[i].length == 2
0 <= ai, bi <= n - 1
ai != bi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
int minReorder(int n, vector<vector<int>>& connections) {

vector<int>v(n,0);vector<vector<int>>adj(n);map<vector<int>,int>count;
for(int i=0;i<connections.size();i++){
adj[connections[i][0]].push_back(connections[i][1]);
adj[connections[i][1]].push_back(connections[i][0]);

count[{connections[i][0],connections[i][1]}]++;
}

queue<int>q;
q.push(0);
int ans=0;
while(!q.empty()){
int x=q.size();
while(x--){
//cout<<q.front()<<" ";
for(int i=0;i<adj[q.front()].size();i++){
if(count[{q.front(),adj[q.front()][i]}]&&v[adj[q.front()][i]]==0)ans++;
if(v[adj[q.front()][i]]==0)
q.push(adj[q.front()][i]);
}
v[q.front()]=1;
q.pop();
//cout<<ans<<endl;
}


}

return ans;

}
};