Skip to content

Commit ac030a6

Browse files
Solve daily problem and add explanation
1 parent b383000 commit ac030a6

File tree

5 files changed

+125
-60
lines changed

5 files changed

+125
-60
lines changed

.vscode/c_cpp_properties.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "windows-gcc-x86",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"compilerPath": "C:/MinGW64/bin/gcc.exe",
9+
"cStandard": "${default}",
10+
"cppStandard": "${default}",
11+
"intelliSenseMode": "windows-gcc-x86",
12+
"compilerArgs": [
13+
""
14+
]
15+
}
16+
],
17+
"version": 4
18+
}

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "C/C++ Runner: Debug Session",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"args": [],
9+
"stopAtEntry": false,
10+
"externalConsole": true,
11+
"cwd": "c:/Users/saksh/OneDrive/Desktop/github/Daily-leetcode-problem_with_detailed_solutions",
12+
"program": "c:/Users/saksh/OneDrive/Desktop/github/Daily-leetcode-problem_with_detailed_solutions/build/Debug/outDebug",
13+
"MIMode": "gdb",
14+
"miDebuggerPath": "gdb",
15+
"setupCommands": [
16+
{
17+
"description": "Enable pretty-printing for gdb",
18+
"text": "-enable-pretty-printing",
19+
"ignoreFailures": true
20+
}
21+
]
22+
}
23+
]
24+
}

.vscode/settings.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"C_Cpp_Runner.cCompilerPath": "gcc",
3+
"C_Cpp_Runner.cppCompilerPath": "g++",
4+
"C_Cpp_Runner.debuggerPath": "gdb",
5+
"C_Cpp_Runner.cStandard": "",
6+
"C_Cpp_Runner.cppStandard": "",
7+
"C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
8+
"C_Cpp_Runner.useMsvc": false,
9+
"C_Cpp_Runner.warnings": [
10+
"-Wall",
11+
"-Wextra",
12+
"-Wpedantic",
13+
"-Wshadow",
14+
"-Wformat=2",
15+
"-Wcast-align",
16+
"-Wconversion",
17+
"-Wsign-conversion",
18+
"-Wnull-dereference"
19+
],
20+
"C_Cpp_Runner.msvcWarnings": [
21+
"/W4",
22+
"/permissive-",
23+
"/w14242",
24+
"/w14287",
25+
"/w14296",
26+
"/w14311",
27+
"/w14826",
28+
"/w44062",
29+
"/w44242",
30+
"/w14905",
31+
"/w14906",
32+
"/w14263",
33+
"/w44265",
34+
"/w14928"
35+
],
36+
"C_Cpp_Runner.enableWarnings": true,
37+
"C_Cpp_Runner.warningsAsError": false,
38+
"C_Cpp_Runner.compilerArgs": [],
39+
"C_Cpp_Runner.linkerArgs": [],
40+
"C_Cpp_Runner.includePaths": [],
41+
"C_Cpp_Runner.includeSearch": [
42+
"*",
43+
"**/*"
44+
],
45+
"C_Cpp_Runner.excludeSearch": [
46+
"**/build",
47+
"**/build/**",
48+
"**/.*",
49+
"**/.*/**",
50+
"**/.vscode",
51+
"**/.vscode/**"
52+
],
53+
"C_Cpp_Runner.useAddressSanitizer": false,
54+
"C_Cpp_Runner.useUndefinedSanitizer": false,
55+
"C_Cpp_Runner.useLeakSanitizer": false,
56+
"C_Cpp_Runner.showCompilationTime": false,
57+
"C_Cpp_Runner.useLinkTimeOptimization": false,
58+
"C_Cpp_Runner.msvcSecureNoWarnings": false
59+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
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.
1+
Approach
2+
Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
3+
4+
Use len to count the the length for consecutive odd numbers
5+
If x in nums is even reset len=0, otherwise len++
6+
if len==3 return true
7+
When the loop is through, there are no three consecutive odds, return false
8+
2nd C++ uses std::find_if with lambda which return the index if the first odd from index=i on is found in arr
9+
i=find_if(arr.begin()+i, arr.end(),
10+
[](int x){return (x&1);})-arr.begin();
11+
Complexity
12+
Time complexity:
13+
O(n)
14+
15+
Space complexity:
16+
O(1)

183-1st_July-Three_consecutive_odds/code.cpp

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,20 @@
11
#include <iostream>
2-
#include <queue>
3-
#include <climits>
2+
#include <vector>
43
using namespace std;
54

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")
185
class Solution
196
{
207
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)
8+
static bool threeConsecutiveOdds(vector<int> &arr)
439
{
44-
int level = -1;
45-
deque<TreeNode *> q;
46-
q.push_back(root);
47-
while (!q.empty())
10+
int len = 0;
11+
for (int x : arr)
4812
{
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-
}
13+
len = (x & 1) ? len + 1 : 0;
14+
if (len == 3)
15+
return 1;
6616
}
67-
return 1;
17+
return 0;
6818
}
6919
};
7020

0 commit comments

Comments
 (0)