diff --git a/solutions/1501-1600/1550-three-consecutive-odds/README.md b/solutions/1501-1600/1550-three-consecutive-odds/README.md new file mode 100644 index 0000000..5b87c89 --- /dev/null +++ b/solutions/1501-1600/1550-three-consecutive-odds/README.md @@ -0,0 +1,11 @@ +# 1550. Three Consecutive Odds + +[LeetCode problem 1550](https://leetcode.com/problems/three-consecutive-odds/) + +- **Difficulty**: Easy +- **Tags**: array + +## Description + +Given an integer array `arr`, return `true` if there are three consecutive +odd numbers in it, `false` otherwise. diff --git a/solutions/1501-1600/1550-three-consecutive-odds/solution.cpp b/solutions/1501-1600/1550-three-consecutive-odds/solution.cpp new file mode 100644 index 0000000..aac38f3 --- /dev/null +++ b/solutions/1501-1600/1550-three-consecutive-odds/solution.cpp @@ -0,0 +1,22 @@ +#include "solution.hpp" + +namespace leetcode::p1550 { + +bool solve(const std::vector& arr) { + int consecutiveOdds = 0; + + for (int value : arr) { + if (value % 2 != 0) { + ++consecutiveOdds; + if (consecutiveOdds == 3) { + return true; + } + } else { + consecutiveOdds = 0; + } + } + + return false; +} + +} // namespace leetcode::p1550 diff --git a/solutions/1501-1600/1550-three-consecutive-odds/solution.hpp b/solutions/1501-1600/1550-three-consecutive-odds/solution.hpp new file mode 100644 index 0000000..bf4489b --- /dev/null +++ b/solutions/1501-1600/1550-three-consecutive-odds/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p1550 { + +// optimized: O(n) time, O(1) space — track a running count of consecutive +// odd values, reset it on any even one. +bool solve(const std::vector& arr); + +} // namespace leetcode::p1550 diff --git a/solutions/1501-1600/1550-three-consecutive-odds/test.cpp b/solutions/1501-1600/1550-three-consecutive-odds/test.cpp new file mode 100644 index 0000000..9c7eb15 --- /dev/null +++ b/solutions/1501-1600/1550-three-consecutive-odds/test.cpp @@ -0,0 +1,8 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 1550", "[p1550]") { + CHECK_FALSE(leetcode::p1550::solve({2, 6, 4, 1})); + CHECK(leetcode::p1550::solve({1, 2, 34, 3, 4, 5, 7, 23, 12})); +} diff --git a/swift/1550.swift b/swift/1550.swift deleted file mode 100644 index 21fd6ee..0000000 --- a/swift/1550.swift +++ /dev/null @@ -1,24 +0,0 @@ -class Solution { - func threeConsecutiveOdds(_ arr: [Int]) -> Bool { - var oddTimes = 0 - - for value in arr { - if isOdd(value) { - oddTimes += 1 - if oddTimes == 3 { - return true - } - } - - if !isOdd(value) { - oddTimes = 0 - } - } - - return false - } - - private func isOdd(_ value: Int) -> Bool { - return value % 2 != 0 - } -} \ No newline at end of file