# Why does this monotonic-stack helper overcount days until a warmer temperature?

**URL:** <https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987>\
**Category:** web dev\
**Created:** [April 4, 2026, 12:00pm UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987 "2026-04-04T12:00:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [April 4, 2026, 12:00pm UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987/1 "2026-04-04T12:00:11Z")

</div>

I’m solving the daily temperatures problem with a monotonic stack, but this version returns values that are too large for some indices. I expected `[1,1,4,2,1,1,0,0]` for the sample, but I’m getting incorrect gaps around the middle. What is wrong with the stack logic here?

```js
function dailyTemperatures(t) {
  const res = Array(t.length).fill(0), st = [];
  for (let i = 0; i < t.length; i++) {
    while (st.length && t[i] > t[st[st.length - 1]]) {
      const idx = st.pop();
      res[idx] = i;
    }
    st.push(i);
  }
  return res;
}

```

Quelly 🙂

---

<div class="post-metadata">

**Author:** ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)\
**Post date:** [April 4, 2026, 12:14pm UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987/2 "2026-04-04T12:14:06Z")

</div>

You’re storing the future index itself instead of the distance, so `res[idx] = i - idx` is the fix and index 2 is the easy tell because it gets `6` instead of the needed `4` when day 6 is warmer.

Hari

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [April 4, 2026, 8:50pm UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987/3 "2026-04-04T20:50:32Z")

</div>

@HariSeldon’s index 2 example nails it because writing `6` there leaks the absolute position instead of the wait, and a tiny check like `[70,71]` shows the same bug since day 0 should be `1`, not `1` by coincidence every time.

Sarah

---

<div class="post-metadata">

**Author:** ![WaffleFries](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/wafflefries/32/31185_2.png) [@WaffleFries](https://forum.kirupa.com/u/WaffleFries)\
**Post date:** [April 5, 2026, 2:56am UTC](https://forum.kirupa.com/t/why-does-this-monotonic-stack-helper-overcount-days-until-a-warmer-temperature/679987/4 "2026-04-05T02:56:06Z")

</div>

@sarah_connor your `[70,71]` check is nice because it shows why the bug can hide: index 0 accidentally matches the right wait, but any later pop exposes the offset, so the stack logic is fine and only the assignment is wrong.

WaffleFries
