2251. Number of Flowers in Full Bloom

  • 각 μ‹œκ°„ λ§ˆλ‹€ 꽃이 ν•€ 개수λ₯Ό 미리 계산해두면 맀번 ꡬ간 λ³„λ‘œ λΉ„κ΅ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€.
    • 맀번 ꡬ간 λ³„λ‘œ λΉ„κ΅ν•˜κ²Œ 되면 μ‹œκ°„ λ³΅μž‘λ„κ°€ κ°€ λ˜μ–΄μ„œ μ‹œκ°„ μ œν•œμ„ μ΄ˆκ³Όν•˜κ²Œ λœλ‹€.
  • 각 κ΅¬κ°„μ˜ μ‹œμž‘μ μ—λŠ” 꽃이 ν•€ κ°œμˆ˜κ°€ 1개 μ¦κ°€ν•˜κ³  끝점 λ‹€μŒ μ‹œκ°„μ—λŠ” 꽃이 ν•€ κ°œμˆ˜κ°€ 1개 κ°μ†Œν•œλ‹€. 이에 λ”°λΌμ„œ 꽃이 ν•€ κ°œμˆ˜κ°€ λ³€ν™”ν•˜λŠ” μ‹œμ  및 λ³€ν™”ν•˜λŠ” 개수λ₯Ό map delta에 κΈ°λ‘ν•œλ‹€.
  • λ³€ν™”ν•˜λŠ” 개수의 map delta둜 λΆ€ν„° λ³€ν™” μ‹œμ  별 꽃이 ν”Όμ–΄μžˆλŠ” 개수λ₯Ό array count에 κΈ°λ‘ν•œλ‹€.
    • delta에 μžˆλŠ” 값을 λˆ„μ ν•΄μ„œ λ”ν•˜λ©΄ ν•΄λ‹Ή μ‹œμ μ—μ„œ 꽃이 ν”Όμ–΄μžˆλŠ” 개수λ₯Ό ꡬ할 수 μžˆλ‹€.
  • 각 μ‚¬λžŒμ΄ λ„λ‹¬ν•œ μ‹œμ  time을 κΈ°μ€€μœΌλ‘œ countμ—μ„œ ν•΄λ‹Ήλ˜λŠ” λ³€ν™” μ‹œμ μ„ upper_boundλ₯Ό μ΄μš©ν•˜μ—¬ μ°ΎλŠ”λ‹€.
    • μ°Ύμ•„μ§„ μœ„μΉ˜κ°€ count array의 첫번째인 경우 λ„λ‹¬ν•œ μ‹œμ μ€ 첫번째 λ³€ν™” μ‹œμ  μ΄μ „μ΄λΌλŠ” μ˜λ―Έμ΄λ―€λ‘œ ν”Όμ–΄μžˆλŠ” 꽃이 ν•˜λ‚˜λ„ μ—†λŠ” 것이 λœλ‹€.
    • λ‚˜λ¨Έμ§€ κ²½μš°μ—λŠ” μ°Ύμ•„μ§„ μœ„μΉ˜ itr κ°€ 도달 μ‹œμ μ΄ν›„μ— 처음 κ½ƒμ˜ κ°œμˆ˜κ°€ λ³€ν™”ν•œ μ‹œμ μ΄λ―€λ‘œ ν•΄λ‹Ή μ‹œμ  μ•ž itr - 1의 값이 ν˜„μž¬ κ½ƒμ˜ κ°œμˆ˜κ°€ λœλ‹€.
  • μ΄λ ‡κ²Œ ν•˜λ©΄ μ‹œκ°„ λ³΅μž‘λ„λŠ” κ°€ λœλ‹€.
    • deltaλ₯Ό κ΅¬μ„±ν•œλ‹€:
    • countλ₯Ό κ΅¬μ„±ν•œλ‹€:
    • 각 μ‚¬λžŒλ³„λ‘œ countμ—μ„œ μ‹œμ μ„ μ°ΎλŠ”λ‹€:
class Solution { public: vector<int> fullBloomFlowers(vector<vector<int>>& flowers, vector<int>& people) { map<int, int> delta; for (auto& flower : flowers) { ++delta[flower[0]]; --delta[flower[1] + 1]; } vector<pair<int, int>> count; int prev = 0; for (auto& entry : delta) { int curr = entry.second + prev; count.emplace_back(entry.first, curr); prev = curr; } vector<int> result; auto compare = [](const pair<int, int>& a, const pair<int, int>& b) { return a.first < b.first; }; for (auto time : people) { auto itr = upper_bound(count.begin(), count.end(), make_pair(time, 0), compare); if (itr == count.begin()) result.push_back(0); else result.push_back((itr - 1)->second); } return result; } };