-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoodDays.js
More file actions
31 lines (27 loc) · 1.35 KB
/
goodDays.js
File metadata and controls
31 lines (27 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on duty on the ith day. The days are numbered starting from 0. You are also given an integer time.
The ith day is a good day to rob the bank if:
There are at least time days before and after the ith day,
The number of guards at the bank for the time days before i are non-increasing, and
The number of guards at the bank for the time days after i are non-decreasing.
More formally, this means day i is a good day to rob the bank if and only if security[i - time] >= security[i - time + 1] >= ... >= security[i] <= ... <= security[i + time - 1] <= security[i + time].
Return a list of all days (0-indexed) that are good days to rob the bank. The order that the days are returned in does not matter.*/
const goodDaysToRobBank = function(security, time) {
let result = [];
for (let i = time; i + time < security.length; i++) {
let bad = 0
for (let j = i - time + 1; j <= i; j++)
if (security[j - 1] < security[j]) {
bad = 1; // increasing
break
}
for (let j = i + 1; j <= i + time; j++)
if (security[j - 1] > security[j]) {
bad = 1; // decreasing
break
}
if (!bad) {
result.push(i)
}
}
return result;
};