Skip to content

Commit 1056127

Browse files
committed
🚀 21-Aug-2020
1 parent b634bac commit 1056127

File tree

5 files changed

+374
-0
lines changed

5 files changed

+374
-0
lines changed

backtracking/rat_in_a_maze.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define N 4
5+
6+
7+
void printSolution(int sol[N][N]){
8+
for(int i = 0; i < N; i++) {
9+
for (int j = 0; j < N; j++)
10+
cout<<sol[i][j]<<" ";
11+
cout<<endl;
12+
}
13+
}
14+
15+
16+
bool isSafe(int maze[N][N], int x, int y){
17+
return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y]);
18+
}
19+
20+
21+
bool solveMaze(int maze[N][N], int x,int y, int sol[N][N]){
22+
// if we reached our destination
23+
if(x == N-1 && y == N-1 && maze[x][y] == 1){
24+
sol[x][y] = 1;
25+
return true;
26+
}
27+
28+
// Check if maze[x][y] is valid
29+
if (isSafe(maze, x, y) == true) {
30+
31+
sol[x][y] = 1; // mark x, y as part of solution path
32+
33+
// Move forward in x direction
34+
if (solveMaze(maze, x + 1, y, sol) == true)
35+
return true;
36+
37+
// If moving in x direction doesn't give solution then move down in y direction
38+
if (solveMaze(maze, x, y + 1, sol) == true)
39+
return true;
40+
41+
// If none of the above movements work then BACKTRACK: unmark x, y as part of solution path
42+
sol[x][y] = 0;
43+
return false;
44+
}
45+
46+
return false;
47+
}
48+
49+
50+
int main(){
51+
int maze[N][N] = { { 1, 0, 0, 0 },
52+
{ 1, 1, 0, 1 },
53+
{ 0, 1, 0, 0 },
54+
{ 1, 1, 1, 1 } };
55+
56+
int sol[N][N];
57+
memset(sol, 0, sizeof(sol));
58+
59+
if(solveMaze(maze, 0, 0, sol)== false){ // <maze, start x, start y, output matrix>
60+
cout<<"Solution doesn't exist";
61+
} else {
62+
printSolution(sol);
63+
}
64+
65+
return 0;
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
2+
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
3+
4+
You may not modify the values in the list's nodes, only nodes itself may be changed.
5+
6+
Example 1:
7+
8+
Given 1->2->3->4, reorder it to 1->4->2->3.
9+
Example 2:
10+
11+
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
/**
30+
* Definition for singly-linked list.
31+
* struct ListNode {
32+
* int val;
33+
* ListNode *next;
34+
* ListNode() : val(0), next(nullptr) {}
35+
* ListNode(int x) : val(x), next(nullptr) {}
36+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
42+
ListNode* getMiddle(ListNode* head){
43+
ListNode *slow=head, *fast=head;
44+
while(fast!=NULL && fast->next!=NULL){
45+
slow=slow->next;
46+
fast=fast->next->next;
47+
}
48+
return slow;
49+
}
50+
51+
ListNode* reverseList(ListNode* head){
52+
if(!head) return head;
53+
ListNode *prev=head, *curr=head, *ahead=head->next;
54+
prev->next=NULL;
55+
curr=ahead;
56+
while(ahead!=NULL){
57+
ahead=ahead->next;
58+
curr->next=prev;
59+
prev=curr;
60+
curr=ahead;
61+
}
62+
head=prev;
63+
return head;
64+
}
65+
66+
void reorderList(ListNode* head) {
67+
if(!head) return;
68+
if(head->next==NULL) return; // only one element
69+
70+
// find middle of the list
71+
ListNode *mid=getMiddle(head);
72+
73+
// Making middles previous equal to NULL
74+
ListNode *curr=head;
75+
while(curr->next!=mid){
76+
curr=curr->next;
77+
}
78+
curr->next=NULL;
79+
80+
// reverse the list after mid
81+
ListNode *revHead=reverseList(mid);
82+
83+
// connecting both the nodes in zigzag manner
84+
ListNode *h1=head, *h2=revHead, *ahead=NULL;
85+
while(h1!=NULL && h2!=NULL){
86+
ahead=h1->next;
87+
h1->next=h2;
88+
h1=h2;
89+
h2=ahead;
90+
}
91+
}
92+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
2+
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
3+
4+
You may not modify the values in the list's nodes, only nodes itself may be changed.
5+
6+
Example 1:
7+
8+
Given 1->2->3->4, reorder it to 1->4->2->3.
9+
Example 2:
10+
11+
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
12+
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
/**
30+
* Definition for singly-linked list.
31+
* struct ListNode {
32+
* int val;
33+
* ListNode *next;
34+
* ListNode() : val(0), next(nullptr) {}
35+
* ListNode(int x) : val(x), next(nullptr) {}
36+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
37+
* };
38+
*/
39+
class Solution {
40+
public:
41+
42+
ListNode* getMiddle(ListNode* head){
43+
ListNode *slow=head, *fast=head;
44+
while(fast!=NULL && fast->next!=NULL){
45+
slow=slow->next;
46+
fast=fast->next->next;
47+
}
48+
return slow;
49+
}
50+
51+
ListNode* reverseList(ListNode* head){
52+
if(!head) return head;
53+
ListNode *prev=head, *curr=head, *ahead=head->next;
54+
prev->next=NULL;
55+
curr=ahead;
56+
while(ahead!=NULL){
57+
ahead=ahead->next;
58+
curr->next=prev;
59+
prev=curr;
60+
curr=ahead;
61+
}
62+
head=prev;
63+
return head;
64+
}
65+
66+
void reorderList(ListNode* head) {
67+
if(!head) return;
68+
if(head->next==NULL) return; // only one element
69+
70+
// find middle of the list
71+
ListNode *mid=getMiddle(head);
72+
73+
// Making middles previous equal to NULL
74+
ListNode *curr=head;
75+
while(curr->next!=mid){
76+
curr=curr->next;
77+
}
78+
curr->next=NULL;
79+
80+
// reverse the list after mid
81+
ListNode *revHead=reverseList(mid);
82+
83+
// connecting both the nodes in zigzag manner
84+
ListNode *h1=head, *h2=revHead, *ahead=NULL;
85+
while(h1!=NULL && h2!=NULL){
86+
ahead=h1->next;
87+
h1->next=h2;
88+
h1=h2;
89+
h2=ahead;
90+
}
91+
}
92+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
2+
3+
You may return any answer array that satisfies this condition.
4+
5+
6+
7+
Example 1:
8+
9+
Input: [3,1,2,4]
10+
Output: [2,4,3,1]
11+
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
12+
13+
14+
Note:
15+
16+
1 <= A.length <= 5000
17+
0 <= A[i] <= 5000
18+
19+
20+
21+
22+
23+
24+
25+
// Without extra space
26+
class Solution {
27+
public:
28+
vector<int> sortArrayByParity(vector<int>& A) {
29+
int n=A.size();
30+
int j=0;
31+
for(int i=0;i<n;i++){
32+
if(A[i]%2==0){
33+
swap(A[i], A[j]);
34+
j++;
35+
}
36+
}
37+
return A;
38+
}
39+
};
40+
41+
42+
43+
44+
45+
46+
// Extra space
47+
class Solution {
48+
public:
49+
vector<int> sortArrayByParity(vector<int>& A) {
50+
int n=A.size();
51+
vector<int> res(n);
52+
int start=0, last=n-1;
53+
for(int i=0;i<n;i++){
54+
if(A[i]%2==0){
55+
res[start++]=A[i];
56+
} else {
57+
res[last--]=A[i];
58+
}
59+
}
60+
return res;
61+
}
62+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.
2+
3+
You may return any answer array that satisfies this condition.
4+
5+
6+
7+
Example 1:
8+
9+
Input: [3,1,2,4]
10+
Output: [2,4,3,1]
11+
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
12+
13+
14+
Note:
15+
16+
1 <= A.length <= 5000
17+
0 <= A[i] <= 5000
18+
19+
20+
21+
22+
23+
24+
25+
// Without extra space
26+
class Solution {
27+
public:
28+
vector<int> sortArrayByParity(vector<int>& A) {
29+
int n=A.size();
30+
int j=0;
31+
for(int i=0;i<n;i++){
32+
if(A[i]%2==0){
33+
swap(A[i], A[j]);
34+
j++;
35+
}
36+
}
37+
return A;
38+
}
39+
};
40+
41+
42+
43+
44+
45+
46+
// Extra space
47+
class Solution {
48+
public:
49+
vector<int> sortArrayByParity(vector<int>& A) {
50+
int n=A.size();
51+
vector<int> res(n);
52+
int start=0, last=n-1;
53+
for(int i=0;i<n;i++){
54+
if(A[i]%2==0){
55+
res[start++]=A[i];
56+
} else {
57+
res[last--]=A[i];
58+
}
59+
}
60+
return res;
61+
}
62+
};

0 commit comments

Comments
 (0)