解析
解析:
- 家裡一個傘架,公司一個傘架
- 需要傘的條件 ( weather[i] == 'rainy' || weather[i] == 'thunderstorms' )
- 需要傘時,沒有就買--->下一個場所多一把傘,如果當下有傘,當下的傘架-1,下一個場傘架+1
function minUmbrellas(weather) {
let sum = 0
let homeBox = 0
let workBox = 0
for(let i=0; i<weather.length; i++ ){
if(weather[i] == 'rainy' || weather[i] == 'thunderstorms'){
if(i%2===0 && homeBox===0){
sum++
workBox++
}else if(i%2===0 && homeBox>0){
homeBox--
workBox++
}else if(i%2===1 && workBox===0){
sum++
homeBox++
}else{
workBox--
homeBox++
}
}
}
return sum
}