Skip to content

Commit

Permalink
Oct22: Sliding window [E]
Browse files Browse the repository at this point in the history
Time: O(N), Space: O(1)
  • Loading branch information
aucker committed Oct 21, 2024
1 parent 7373da0 commit 201944c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions daily/Oct22.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
using namespace std;

class Solution {
public:
/**
* @brief LC3184: Count pairs that form a complete day I
* Time: O(N) cause we use sliding window, Space: O(1)
*
* @param hours
* @return int
*/
int countCompleteDayPairs(vector<int>& hours) {
int len = hours.size();
int le = 0, ri = le + 1;
if (len == 1) return 0;
int ans = 0;

while (le < len - 1) {
if ((hours[le] + hours[ri]) % 24 == 0) {
ans++;
}

if (ri < len - 1) {
ri++;
} else {
le++;
ri = le + 1;
}
}

return ans;
}
};

0 comments on commit 201944c

Please sign in to comment.