Skip to content

Commit 03e0862

Browse files
committed
Solve 024
1 parent 56092da commit 03e0862

File tree

7 files changed

+314
-37
lines changed

7 files changed

+314
-37
lines changed

023/code.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ For example, given the following board:
2020
[f, f, f, f],
2121
[f, f, f, f]]
2222
```
23-
0 1 2 3
24-
4 5 6 7
25-
8 9 10 11
26-
12 13 14 15
2723
2824
and start = (3, 0) (bottom left) and end = (0, 0) (top left), the minimum number
2925
of steps required to reach the end is 7, since we would need to go through (1,

024/code.cpp

+141-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
/*
22
Daily Coding Challenge #024
33
---------------------------
4+
This problem was asked by Google.
45
6+
Implement locking in a binary tree. A binary tree node can be locked or unlocked
7+
only if all of its descendants or ancestors are not locked.
8+
9+
Design a binary tree node class with the following methods:
10+
11+
- `is_locked`, which returns whether the node is locked.
12+
- `lock`, which attempts to lock the node. If it cannot be locked, then it
13+
should return false. Otherwise, it should lock it and return true.
14+
- `unlock`, which unlocks the node. If it cannot be unlocked, then it should
15+
return false. Otherwise, it should unlock it and return true.
16+
17+
You may augment the node to add parent pointers or any other property you would
18+
like. You may assume the class is used in a single-threaded program, so there is
19+
no need for actual locks or mutexes. Each method should run in `O(h)`, where `h`
20+
is the height of the tree.
521
622
Run Using
723
---------
@@ -11,15 +27,132 @@ g++ 024/code.cpp -o bin/out && ./bin/out < 024/in.txt > 024/out.txt
1127
using namespace std;
1228
#define ll long long
1329

14-
void solve() {
15-
int n;
16-
return;
30+
struct Node {
31+
// val: value of node
32+
int val;
33+
34+
// stats for locked nodes
35+
bool state; // true for lock
36+
int leftLocks; // no. of left sub-tree nodes that are locked
37+
int rightLocks; // no. of right sub-tree nodes that are locked
38+
39+
Node *parent; // parent node
40+
Node *left, *right; // children nodes
41+
42+
Node(Node *par, int v) {
43+
parent = par;
44+
val = v;
45+
state = false;
46+
leftLocks = rightLocks = 0;
47+
left = right = NULL;
48+
}
49+
50+
bool is_locked() { return state; }
51+
52+
bool lock() {
53+
Node *tmp = this;
54+
bool already = (state+leftLocks + rightLocks > 0);
55+
while (tmp->parent && !already) {
56+
tmp = tmp->parent;
57+
already = tmp->state;
58+
}
59+
if (already) {
60+
cout << "(already locked due to: " << tmp->val
61+
<< " is_locked:" << (tmp->state ? "true" : "false")
62+
<< " leftLocks:" << tmp->leftLocks
63+
<< " rightLocks:" << tmp->rightLocks << ") ";
64+
return false;
65+
}
66+
// the node or its parent are not locked, means the children are also
67+
// not locked and the current node is free to be locked
68+
state = true; // lock node
69+
// set child lock for parents
70+
tmp = this;
71+
Node *par = tmp->parent;
72+
while (par) {
73+
if (par->left == tmp)
74+
par->leftLocks++;
75+
else
76+
par->rightLocks++;
77+
tmp = par;
78+
par = par->parent;
79+
}
80+
return true;
81+
}
82+
83+
bool unlock() {
84+
if (!state) return false;
85+
state = false; // unlock it
86+
// update parent stats
87+
Node *tmp = this, *par = tmp->parent;
88+
while (par) {
89+
if (par->left == tmp)
90+
par->leftLocks--;
91+
else
92+
par->rightLocks--;
93+
tmp = par;
94+
par = par->parent;
95+
}
96+
return true;
97+
}
98+
};
99+
100+
void PrintTree(Node *root, string prefix) {
101+
if (root->right != NULL) PrintTree(root->right, prefix + "\t");
102+
cout << prefix << root->val << endl;
103+
if (root->left != NULL) PrintTree(root->left, prefix + "\t");
17104
}
18105

19-
int main() {
20-
int t;
21-
cin >> t;
22-
while (t--) {
23-
solve();
106+
void ScanTree(int n, Node **list) {
107+
for (int i = 0; i < n; i++) {
108+
list[i] = NULL;
109+
}
110+
111+
n--;
112+
while (n--) {
113+
int x, y;
114+
cin >> x >> y; // x is parent of y
115+
if (list[x] == NULL) list[x] = new Node(NULL, x);
116+
if (list[y] == NULL) list[y] = new Node(list[x], y);
117+
list[y]->parent = list[x];
118+
if (list[x]->left == NULL)
119+
list[x]->left = list[y];
120+
else
121+
list[x]->right = list[y];
24122
}
25123
}
124+
125+
void solve() {
126+
int n; // nodes
127+
cin >> n;
128+
Node *root, *list[n];
129+
ScanTree(n, list);
130+
// considering 0 as root
131+
root = list[0];
132+
PrintTree(root, "");
133+
134+
int q; // queries
135+
cin >> q;
136+
137+
string op; // operation
138+
int on; // on: node index
139+
bool res; // response
140+
141+
while (q--) {
142+
cin >> op >> on;
143+
cout << op << " " << on << " ";
144+
if (op == "lock")
145+
res = list[on]->lock();
146+
else if (op == "unlock")
147+
res = list[on]->unlock();
148+
else
149+
res = list[on]->is_locked();
150+
if (res)
151+
cout << "true" << endl;
152+
else
153+
cout << "false" << endl;
154+
}
155+
return;
156+
}
157+
158+
int main() { solve(); }

024/code.py

-13
This file was deleted.

024/in.txt

+87-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,87 @@
1-
1+
21
2+
0 2
3+
0 3
4+
2 1
5+
2 4
6+
3 7
7+
3 8
8+
8 9
9+
8 10
10+
9 17
11+
4 6
12+
1 5
13+
5 11
14+
5 12
15+
11 18
16+
18 20
17+
6 13
18+
14 16
19+
16 19
20+
6 14
21+
13 15
22+
65
23+
lock 6
24+
is_locked 6
25+
lock 13
26+
is_locked 13
27+
unlock 13
28+
lock 16
29+
is_locked 16
30+
unlock 6
31+
lock 13
32+
lock 14
33+
is_locked 13
34+
is_locked 14
35+
lock 4
36+
is_locked 4
37+
lock 1
38+
is_locked 1
39+
lock 1
40+
is_locked 1
41+
unlock 14
42+
lock 15
43+
unlock 13
44+
lock 15
45+
lock 6
46+
unlock 6
47+
lock 0
48+
unlock 1
49+
lock 0
50+
unlock 15
51+
lock 0
52+
lock 2
53+
unlock 0
54+
lock 2
55+
lock 7
56+
is_locked 2
57+
is_locked 7
58+
lock 18
59+
lock 19
60+
unlock 2
61+
lock 19
62+
lock 18
63+
lock 20
64+
is_locked 20
65+
is_locked 18
66+
is_locked 19
67+
lock 13
68+
is_locked 13
69+
lock 9
70+
is_locked 9
71+
unlock 13
72+
lock 10
73+
is_locked 10
74+
unlock 9
75+
unlock 10
76+
unlock 7
77+
lock 3
78+
is_locked 3
79+
lock 0
80+
is_locked 0
81+
unlock 3
82+
lock 0
83+
unlock 19
84+
lock 0
85+
unlock 18
86+
lock 0
87+
is_locked 0

024/out.txt

+86-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,86 @@
1-
1+
10
2+
8
3+
9
4+
17
5+
3
6+
7
7+
0
8+
4
9+
14
10+
16
11+
19
12+
6
13+
13
14+
15
15+
2
16+
1
17+
12
18+
5
19+
11
20+
18
21+
20
22+
lock 6 true
23+
is_locked 6 true
24+
lock 13 (already locked due to: 6 is_locked:true leftLocks:0 rightLocks:0) false
25+
is_locked 13 false
26+
unlock 13 false
27+
lock 16 (already locked due to: 6 is_locked:true leftLocks:0 rightLocks:0) false
28+
is_locked 16 false
29+
unlock 6 true
30+
lock 13 true
31+
lock 14 true
32+
is_locked 13 true
33+
is_locked 14 true
34+
lock 4 (already locked due to: 4 is_locked:false leftLocks:2 rightLocks:0) false
35+
is_locked 4 false
36+
lock 1 true
37+
is_locked 1 true
38+
lock 1 (already locked due to: 1 is_locked:true leftLocks:0 rightLocks:0) false
39+
is_locked 1 true
40+
unlock 14 true
41+
lock 15 (already locked due to: 13 is_locked:true leftLocks:0 rightLocks:0) false
42+
unlock 13 true
43+
lock 15 true
44+
lock 6 (already locked due to: 6 is_locked:false leftLocks:1 rightLocks:0) false
45+
unlock 6 false
46+
lock 0 (already locked due to: 0 is_locked:false leftLocks:2 rightLocks:0) false
47+
unlock 1 true
48+
lock 0 (already locked due to: 0 is_locked:false leftLocks:1 rightLocks:0) false
49+
unlock 15 true
50+
lock 0 true
51+
lock 2 (already locked due to: 0 is_locked:true leftLocks:0 rightLocks:0) false
52+
unlock 0 true
53+
lock 2 true
54+
lock 7 true
55+
is_locked 2 true
56+
is_locked 7 true
57+
lock 18 (already locked due to: 2 is_locked:true leftLocks:0 rightLocks:0) false
58+
lock 19 (already locked due to: 2 is_locked:true leftLocks:0 rightLocks:0) false
59+
unlock 2 true
60+
lock 19 true
61+
lock 18 true
62+
lock 20 (already locked due to: 18 is_locked:true leftLocks:0 rightLocks:0) false
63+
is_locked 20 false
64+
is_locked 18 true
65+
is_locked 19 true
66+
lock 13 true
67+
is_locked 13 true
68+
lock 9 true
69+
is_locked 9 true
70+
unlock 13 true
71+
lock 10 true
72+
is_locked 10 true
73+
unlock 9 true
74+
unlock 10 true
75+
unlock 7 true
76+
lock 3 true
77+
is_locked 3 true
78+
lock 0 (already locked due to: 0 is_locked:false leftLocks:2 rightLocks:1) false
79+
is_locked 0 false
80+
unlock 3 true
81+
lock 0 (already locked due to: 0 is_locked:false leftLocks:2 rightLocks:0) false
82+
unlock 19 true
83+
lock 0 (already locked due to: 0 is_locked:false leftLocks:1 rightLocks:0) false
84+
unlock 18 true
85+
lock 0 true
86+
is_locked 0 true

go.mod

-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
module github.com/Shivam010/daily-coding-problem
22

33
go 1.14
4-
5-
require (
6-
github.com/gomarkdown/markdown v0.0.0-20200824053859-8c8b3816f167 // indirect
7-
github.com/gomarkdown/mdtohtml v0.0.0-20180202094705-7346712f31b4 // indirect
8-
)

go.sum

-5
This file was deleted.

0 commit comments

Comments
 (0)