Skip to content

Commit 040ecda

Browse files
Commit daily problem's solution
1 parent dbd1033 commit 040ecda

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Intuition
2+
The intuition for the threeConsecutiveOdds function involves iterating through the array and checking groups of three consecutive elements at each step. The function uses a for loop to traverse the array, and at each iteration, it applies a bitwise AND operation (& 1) to determine if the current element and the next two elements are all odd. If a triplet of consecutive odd numbers is found, it returns true; otherwise, it continues the search until the end of the array and returns false if no such triplet is found.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <iostream>
2+
#include <queue>
3+
#include <climits>
4+
using namespace std;
5+
6+
class TreeNode
7+
{
8+
public:
9+
int val;
10+
TreeNode *left;
11+
TreeNode *right;
12+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
13+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
14+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
15+
};
16+
17+
#pragma GCC optimize("O3", "unroll-loops")
18+
class Solution
19+
{
20+
public:
21+
using info = pair<TreeNode *, int>;
22+
void print(auto &c)
23+
{
24+
for (auto node : c)
25+
cout << node->val << ", ";
26+
cout << endl;
27+
}
28+
bool isLess(auto &q, int qz)
29+
{
30+
for (int i = 1; i < qz; i++)
31+
if (q[i - 1]->val >= q[i]->val)
32+
return 0;
33+
return 1;
34+
}
35+
bool isGreater(auto &q, int qz)
36+
{
37+
for (int i = 1; i < qz; i++)
38+
if (q[i - 1]->val <= q[i]->val)
39+
return 0;
40+
return 1;
41+
}
42+
bool isEvenOddTree(TreeNode *root)
43+
{
44+
int level = -1;
45+
deque<TreeNode *> q;
46+
q.push_back(root);
47+
while (!q.empty())
48+
{
49+
level++;
50+
int qz = q.size();
51+
bool isOdd = level & 1;
52+
// print(q);
53+
if ((isOdd && !isGreater(q, qz)) || (!isOdd && !isLess(q, qz)))
54+
return 0;
55+
for (int i = 0; i < qz; i++)
56+
{
57+
auto node = q.front();
58+
q.pop_front();
59+
if ((node->val & 1) == isOdd)
60+
return 0;
61+
if (node->left)
62+
q.push_back(node->left);
63+
if (node->right)
64+
q.push_back(node->right);
65+
}
66+
}
67+
return 1;
68+
}
69+
};
70+
71+
auto init = []()
72+
{
73+
ios::sync_with_stdio(0);
74+
cin.tie(0);
75+
cout.tie(0);
76+
return 'c';
77+
}();

0 commit comments

Comments
 (0)