id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "\n#ifdef LOCAL_RUN\n#include \"debug_leet.h\"\n #else\n #define trace(...) ;\n #define dbg(...) ;\n #define dbgc(...) ;\n #define debug(x) ;\n #define debuga(a, n) ;\n #define debug2(x, y) ;\n #define debug3(x, y, z) ;\n #define debug4(x, y, z, w) ;\n #define debug5(a,b,c,d,e) ;\n #define lassert(x) ;\n #define dassert(x, ...) ;\n int recur_depth = 0; bool rec_indent = true;\n const bool isLocal = false;\n template <typename Arg, typename... Args>\n void display(TreeNode* root) {}\n #endif\n\n #define pb push_back\n #define eb emplace_back\n #define popb pop_back\n #define all(v) begin(v), end(v)\n #define rall(v) (v).rbegin(),(v).rend()\n #define make_unique(v) (v).erase(unique(all(v)), (v).end())\n #define sz(c) ((int) c.size())\n #define forn(i,n) for(int i=0;i<(int)(n);i++)\n #define fornn(i,s,n) for(int i=s;i<(int)(n);i++)\n #define forb(i,n) for(int i=n-1;i>=0;i--)\n #define forbn(i,s,n) for(int i=n-1;i>=(int)(s);i--)\n #define forit(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)\n #define mem(a,b) memset(a,b,sizeof(a))\n #define abs(x) (((x) < 0) ? -(x) : (x))\n #define sqr(x) ((x) * (x))\n #define sqrt(x) sqrt(abs(x))\n #define has(c,x) (c.find(x) != c.end())\n #define pw(x) (1LL << (x))\n #define ibit(x,i) (((x) >> (i)) & 1)\n #define data(v) v.data(), sz(v) // vi -> vai\n #define gtime() ((double(clock()) - 0)/CLOCKS_PER_SEC)\n\n typedef stringstream sstr;\n typedef long long ll;\n typedef long double ld;\n typedef pair<int, int> pii;\n typedef pair<ll,ll> pll;\n typedef pair<ld,ld> pdd;\n typedef vector<int> vi;\n typedef vector<ll> vll;\n typedef vector<pii> vpii;\n typedef vector<vi> vvi;\n typedef vector<vll> vvll;\n typedef valarray<int> vai;\n template <class T>\n using min_pq = priority_queue<T, vector<T>, greater<T>>;\n template <class T>\n using vc = vector<T>;\n template <class T>\n using vvc = vector<vc<T>>;\n template <class T>\n using vvvc = vector<vvc<T>>;\n template <class T>\n using vvvvc = vector<vvvc<T>>;\n template <class T>\n using vvvvvc = vector<vvvvc<T>>;\n\n template<class F>\n struct y_comb{\n F f;\n template<class T> explicit y_comb(T &&f_in): f(forward<T>(f_in)){ }\n template<class ...Args> decltype(auto) operator()(Args &&...args){ return f(ref(*this), forward<Args>(args)...); }\n };\n template<class F>\n decltype(auto) yf(F &&f){\n return y_comb<decay_t<F>>(forward<F>(f));\n }\n\n inline int ni(){ int x; cin >> x; return x; }\n inline ll nl() { ll x; cin >> x; return x; }\n\n template <class T> void mmin(T& a, const T& b) {\n a = (a) < (b) ? (a) : (b);\n }\n template <class T> void mmax(T& a, const T& b) {\n a = (a) > (b) ? (a) : (b);\n }\n template <class T> auto vv(int d1, T x){\n return vc<T>(d1, x);\n }\n template <class T> auto vv(int d1, int d2, T x){\n return vc<vc<T>>(d1, vc<T>(d2, x));\n }\n template <class T> auto vv(int d1, int d2, int d3, T x){\n return vc<vc<vc<T>>>(d1, vv(d2, d3, x));\n }\n template <class T> auto vv(int d1, int d2, int d3, int d4, T x){\n return vc<vc<vc<vc<T>>>>(d1, vv(d2, d3, d4, x));\n }\n void outv(auto &v){\n for(auto &x: v) {cout<< x <<\" \";} cout<<endl;\n }\n void rvec(int &n, auto &v){\n cin >> n; v.resize(n); for(auto &x: v) cin >> (x);\n }\n template <typename Arg, typename... Args>\n void read(Arg&& arg, Args&&... args){\n cin >> std::forward<Arg>(arg); using expander = int[];\n (void)expander{0, (void(cin >> std::forward<Args>(args)),0)...};\n }\n template <typename Arg, typename... Args>\n void out(Arg&& arg, Args&&... args){\n cout << std::forward<Arg>(arg); using expander = int[];\n (void)expander{0, (void(cout << \" \" << std::forward<Args>(args)),0)...};\n cout << endl;\n }\n template <class Integer, class F>\n Integer find_first_false(Integer l, Integer r, F&& f) {\n --l; // ++r;\n while (r - l > 1) {\n Integer m = midpoint(l, r);\n if (f(m)) l = m;\n else r = m;\n }\n return r;\n }\n template <class Integer, class F>\n Integer find_last_true(Integer l, Integer r, F &&f) {\n return find_first_false(l, r, [&f](Integer i) { return f(i); }) - 1;\n }\n template <class Integer, class F>\n Integer find_first_true(Integer l, Integer r, F &&f) {\n return find_first_false(l, r, [&f](Integer i) { return !f(i); });\n }\n auto init = []() {\n ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); return 'c';\n }();\n\n ll pwr(ll base, ll p, ll mod){\n ll ans=1; while(p) {if(p&1) ans=(ans*base)%mod;\n base=(base*base)%mod; p/=2;}\n return ans;\n }\n ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b,a%b); }\n ll lcm(ll a, ll b) { return a*(b/gcd(a,b)); }\n\n const long double PI = (long double)(3.1415926535897932384626433832795);\n const ll mx_ll = numeric_limits<ll> :: max();\n const int mx_int = numeric_limits<int> :: max();\n\n const int oo = 0x3f3f3f3f;\n const ll OO = 0x3f3f3f3f3f3f3f3fll;\n const double eps = 1e-9;\n const int DX[8]={0,1, 0,-1,-1,1,-1, 1};\n const int DY[8]={1,0,-1, 0,-1,1, 1,-1};\n\nconst int maxn = 1e5 + 3;\nconst int mod = 1e9+7;\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& g) {\n int n = sz(g), m = sz(g[0]);\n vll a(m);\n stack<pii> q;\n forn(i, n){\n forn(j, m) q.emplace(a[j], j);\n while(sz(q)){\n auto [x, j] = q.top(); q.pop();\n if(x != a[j]) continue;\n if(j and a[j-1] < x - 1) {\n a[j-1] = x - 1;\n q.emplace(a[j-1], j-1);\n }\n if(j+1 < m and a[j+1] < x - 1) {\n a[j+1] = x - 1;\n q.emplace(a[j+1], j+1);\n }\n }\n forn(j, m) a[j] += g[i][j];\n }\n return *max_element(all(a));\n }\n};",
"memory": "109293"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n const int n = points.size();\n const int m = points[0].size();\n\n vector<long long> memo(points[0].cbegin(), points[0].cend());\n for(int i = 1; i < n; ++i){\n vector<int> best_choices(m);\n best_choices[0] = 0;\n\n int cur_best = 0;\n for(int j = 1; j < m; ++j){\n if(memo[cur_best] + cur_best - j <= memo[j]) cur_best = j;\n best_choices[j] = cur_best;\n }\n\n cur_best = m-1;\n for(int j = m-2; j >= 0; --j){\n if(memo[cur_best] - cur_best + j <= memo[j]) cur_best = j;\n if(memo[best_choices[j]] + best_choices[j] - j < memo[cur_best] - cur_best + j){\n best_choices[j] = cur_best;\n }\n }\n\n\n vector<long long> buffer(m);\n for(int j = 0; j < m; ++j){\n buffer[j] = points[i][j] + memo[best_choices[j]] - abs(best_choices[j] - j);\n }\n memo = buffer;\n }\n return *max_element(memo.cbegin(), memo.cend());\n }\n};",
"memory": "110581"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(const vector<vector<int>>& points) {\n const int n = points.size();\n const int m = points[0].size();\n\n vector<long long> memo(points[0].cbegin(), points[0].cend());\n for(int i = 1; i < n; ++i){\n vector<int> best_choices(m);\n best_choices[0] = 0;\n\n int cur_best = 0;\n for(int j = 1; j < m; ++j){\n if(memo[cur_best] + cur_best - j <= memo[j]) cur_best = j;\n best_choices[j] = cur_best;\n }\n\n cur_best = m-1;\n for(int j = m-2; j >= 0; --j){\n if(memo[cur_best] - cur_best + j <= memo[j]) cur_best = j;\n if(memo[best_choices[j]] + best_choices[j] - j < memo[cur_best] - cur_best + j){\n best_choices[j] = cur_best;\n }\n else{\n cur_best = best_choices[j];\n j = best_choices[j];\n }\n }\n\n\n vector<long long> buffer(m);\n for(int j = 0; j < m; ++j){\n buffer[j] = points[i][j] + memo[best_choices[j]] - abs(best_choices[j] - j);\n }\n memo.clear();\n memo = buffer;\n buffer.clear();\n }\n return *max_element(memo.cbegin(), memo.cend());\n }\n};",
"memory": "110581"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\n typedef long long ll;\n typedef pair<ll, ll> prll;\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n \n vector<ll> prev(m), curr(m);\n for(int i=0; i<m; i++) prev[i] = points[n-1][i];\n\n for(int i=n-2; i>=0; i--) {\n vector<prll> prefix(m);\n prll max_so_far = {0, 0};\n for(int j=0; j<m-1; j++) {\n if(max_so_far.first < j + prev[j]) {\n max_so_far = {prev[j] + j, prev[j]};\n }\n prefix[j + 1] = max_so_far;\n }\n\n max_so_far = {0, 0};\n for(int j=m-1; j>=0; j--) {\n curr[j] = max({\n prev[j], \n prefix[j].first - j, \n max_so_far.first - m + 1 + j\n }) + points[i][j];\n\n if(prev[j] + m-1-j > max_so_far.first) {\n max_so_far = {prev[j] + m-1-j, prev[j]};\n }\n }\n\n prev = curr;\n }\n\n return *max_element(prev.begin(), prev.end());\n }\n};",
"memory": "111868"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "#define ll long long\n#define li pair<long long, int>\n#define mp make_pair\nclass Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n uint m = points.size();\n uint n = points[0].size();\n vector<li> pp(n);\n for (int i = 0; i < n; i++) {\n pp[i].first = points[0][i];\n pp[i].second = i;\n } \n for (int i = 1; i < m; i++) {\n sort(pp.begin(), pp.end());\n vector<li> nxt(n);\n int rowmax = *max_element(points[i].begin(), points[i].end());\n for (int j = 0; j < n; j++) {\n nxt[j] = mp(0,j);\n if (points[i][j] + 2*n > rowmax) {\n for (int k = n-1; k >= 0 && pp[k].first > nxt[j].first; k--) {\n nxt[j].first = max(nxt[j].first, pp[k].first - abs(j - pp[k].second));\n }\n nxt[j].first += points[i][j];\n }\n }\n pp = nxt;\n }\n ll ans = pp[0].first;\n for (int i = 0; i < pp.size(); i++) {\n ans = max(pp[i].first, ans);\n }\n return ans;\n }\n};",
"memory": "111868"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n =points.size(), m = points[0].size();\n if(n == 1){\n long long ans = 0;\n for(long long tt:points[0])ans = max(ans,tt);\n return ans;\n }\n vector<vector<pair<long long,long long>>>v(n-1,vector<pair<long long,long long>>(m));\n vector<long long>a(m);\n for(int i=0;i<1;i++){\n v[i][m-1] = {points[i][m-1] - m + 1,m-1};\n for(int j=m-2;j>=0;j--){\n if(points[i][j] - j >= v[i][j+1].first){\n v[i][j] = {points[i][j] - j,j};\n }\n else{\n v[i][j] = v[i][j+1];\n }\n }\n }\n // b = points[0];\n long long idx =0, val =0;\n for(int i=1;i<n;i++){\n for(int j = 0;j<m;j++){\n if(j == 0){\n a[j] = points[i][j] + v[i-1][j].first + j;\n val = v[i-1][j].first + j;\n idx = v[i-1][j].second;\n }\n else{\n if(idx >= j){\n val++;\n a[j] = points[i][j] + val;\n }\n else{\n if(val - 1 > v[i-1][j].first + j){\n a[j] = points[i][j] + val -1;\n val--;\n }\n else{\n a[j] = points[i][j] + v[i-1][j].first + j;\n val = v[i-1][j].first + j;\n idx = v[i-1][j].second;\n }\n }\n }\n }\n if(i == n-1)break;\n v[i][m-1] = {a[m-1] - m + 1,m-1};\n for(int j=m-2;j>=0;j--){\n if(a[j] - j >= v[i][j+1].first){\n v[i][j] = {a[j] - j,j};\n }\n else{\n v[i][j] = v[i][j+1];\n }\n }\n // for(int tt:a){\n // cout << tt << \" \";\n // }\n // cout << endl;\n }\n // for(int tt:a){\n // cout << tt << \" \";\n // }\n // cout << endl;\n long long ans = 0;\n for(long long tt:a)ans = max(ans,tt);\n return ans;\n }\n};",
"memory": "113156"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n // seems like a dp\n vector<vector<long long>> dp(points.size(), vector<long long>(points[0].size(), 0));\n\n for (int j = 0; j < points[0].size(); j++) {\n dp[0][j] = points[0][j];\n }\n\n for (int i = 1; i < points.size(); i++) {\n vector<int> candidates;\n candidates.push_back(points[0].size() - 1);\n int last = points[0].size() - 1;\n // pre-process a bit\n for (int j = points[0].size() - 2; j >= 0; --j) {\n if (dp[i-1][last] - last < dp[i-1][j] - j) {\n candidates.push_back(j);\n last = j;\n }\n }\n reverse(candidates.begin(), candidates.end());\n\n // cout << i << \": \";\n // for (int a = 0; a < candidates.size(); ++a) {\n // cout << candidates[a] << ' ';\n // }\n // cout << '\\n';\n\n int k = 0;\n int bestLeft = 0;\n int bestRight = candidates[k];\n for (int j = 0; j < points[0].size(); j++) {\n dp[i][j] = max(dp[i-1][bestLeft] + bestLeft - j, dp[i-1][bestRight] + j - bestRight) + points[i][j];\n\n if (j == bestRight && j != points[0].size() - 1) {\n if (dp[i-1][bestRight] + j - bestRight > dp[i-1][bestLeft] + bestLeft - j) {\n bestLeft = bestRight;\n }\n k++;\n bestRight = candidates[k];\n }\n }\n }\n\n for (int i = 0; i < points.size(); i++) {\n for (int j = 0; j < points[0].size(); j++) {\n cout << dp[i][j] << ' ';\n }\n cout << '\\n';\n }\n\n long long ans = 0;\n for (int j = 0; j < points[0].size(); j++) {\n ans = max(ans, dp[points.size() - 1][j]);\n }\n return ans;\n }\n};",
"memory": "113156"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "using ll = long long;\nclass Solution {\npublic:\n vector<vector<int>> v;\n // vector<vector<ll>> dp;\n // ll fun(ll r, ll c) {\n // if (r == n)\n // return 0;\n // ll& ret = dp[r][c];\n // if (ret!=-1)\n // return ret;\n // for (int i = 0; i < m; i++) {\n // ll ans = v[r][i] - ((r != 0) ? abs(i - c) : 0);\n // ret = max(ret, fun(r + 1, i) + ans);\n // }\n // return ret;\n // }\n long long maxPoints(vector<vector<int>>& ar) {\n ll n = ar.size();\n ll m = ar[0].size();\n v = ar;\n vector<ll> dp(m);\n for (int i = 0; i < m; i++)\n dp[i] = v[0][i];\n for (int i = 1; i < n; i++) {\n vector<ll> ndp(m, 0);\n ll mxl = -1e9, mxr = -1e9;\n for (int j = 0; j < m; j++) {\n mxl = max(mxl, dp[j] + j);\n ndp[j] = v[i][j] + mxl - j;\n }\n for (int j = m - 1; j >= 0; j--) {\n mxr = max(mxr, dp[j] - j);\n ndp[j] = max(ndp[j], v[i][j] + mxr + j);\n }\n dp = ndp;\n }\n ll ans = 0;\n for (int i = 0; i < m; i++)\n ans = max(ans, dp[i]);\n return ans;\n }\n};",
"memory": "114443"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n \n if (n == 1) {\n return *max_element(points[0].begin(), points[0].end());\n }\n \n vector<long long> dp(m);\n for (int i = 0; i < m; i++) {\n dp[i] = points[0][i];\n }\n \n for (int i = 1; i < n; i++) {\n vector<long long> left(m), right(m);\n \n // Calculate left max\n left[0] = dp[0];\n for (int j = 1; j < m; j++) {\n left[j] = max(left[j-1] - 1, dp[j]);\n }\n \n // Calculate right max\n right[m-1] = dp[m-1];\n for (int j = m-2; j >= 0; j--) {\n right[j] = max(right[j+1] - 1, dp[j]);\n }\n \n // Update dp\n for (int j = 0; j < m; j++) {\n dp[j] = points[i][j] + max(left[j], right[j]);\n }\n }\n \n return *max_element(dp.begin(), dp.end());\n }\n};",
"memory": "114443"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n #define ll long long\n long long maxPoints(vector<vector<int>>& points) {\n ll n = points.size();\n ll m = points[0].size();\n\n vector<ll> curr(points[0].begin(), points[0].end());\n\n if (n == 1) {\n return *max_element(curr.begin(), curr.end());\n }\n\n vector<ll> next(m, 0);\n for (int i = 1; i < n; i++) {\n vector<ll> leftMax(m), rightMax(m);\n \n leftMax[0] = curr[0];\n for (int j = 1; j < m; j++) {\n leftMax[j] = max(leftMax[j-1], curr[j] + j);\n }\n \n rightMax[m-1] = curr[m-1] - (m-1);\n for (int j = m-2; j >= 0; j--) {\n rightMax[j] = max(rightMax[j+1], curr[j] - j);\n }\n \n for (int j = 0; j < m; j++) {\n next[j] = points[i][j] + max(leftMax[j] - j, rightMax[j] + j);\n }\n \n curr = next;\n }\n\n return *max_element(curr.begin(), curr.end());\n }\n};\n",
"memory": "115731"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxi(long long i, long long j){\n if(i>j) return i;\n return j;\n }\n // long long dp[1001][1001];\n long long fun(vector<vector<int>> &points, vector<vector<long long>> &dp){\n long long result = 0;\n for(int i = 0; i < points.size(); i++){\n long long lhs = 0;\n for(int j = 0; j<points[i].size(); j++){\n // int ans = 0;\n if(i != 0 && j != 0) lhs = maxi(lhs, dp[i-1][j-1]);\n if(i != 0) lhs = maxi(--lhs, dp[i-1][j]);\n dp[i][j] = lhs + points[i][j];\n \n }\n long long rhs = 0;\n for(int j = points[0].size()-1; j>=0; j--){\n // int ans = 0;\n if(points.size()-1 != j && 0 != i) rhs = maxi(rhs, dp[i-1][j+1]);\n if(i != 0) rhs = maxi(--rhs, dp[i-1][j]);\n dp[i][j] = maxi(dp[i][j], rhs+points[i][j]);\n if(i == points.size()-1) result = maxi(dp[i][j], result);\n }\n // for(int j = 0; j<points[0].size(); j++){\n // cout<<dp[i][j]<<\" \";\n // }\n // cout<<endl;\n }\n return result;\n }\n long long maxPoints(vector<vector<int>>& points) {\n // memset(dp, 0, sizeof dp);\n vector<vector<long long>> dp(points.size()+5, vector<long long>(points[0].size()+5, 0));\n return fun(points, dp);\n }\n};",
"memory": "115731"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int row = points.size();\n int col = points[0].size();\n vector<long long> max_points;\n for(int i = 0; i < col; i++){\n max_points.push_back((long long) points[0][i]);\n //cout<<max_points[i]<<' ';\n }\n for(int i = 1; i < row; i++){\n vector<long long> left(col, 0), right(col, 0);\n left[0] = max_points[0];\n right[col - 1] = max_points[col - 1];\n for(int j = 1; j < col; j++){\n left[j] = max(left[j - 1] - 1, max_points[j]);\n right[col - j - 1] = max(right[col - j] - 1, max_points[col - j - 1]);\n }\n for(int j = 0; j < col; j++){\n max_points[j] = max(left[j], right[j]) + points[i][j];\n }\n }\n return (*max_element(max_points.begin(), max_points.end()));\n }\n};",
"memory": "117018"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size(), m = points[0].size();\n vector<long long> ans;\n for (int p : points[0]) ans.push_back(p);\n for (int i = 0; i < n - 1; ++i) {\n vector<long long> left(m), right(m);\n left[0] = ans[0];\n right[m - 1] = ans[m - 1];\n for (int j = 1; j < m; ++j)\n left[j] = max(left[j - 1] - 1, ans[j]);\n for (int j = m - 2; j >= 0; --j)\n right[j] = max(right[j + 1] - 1, ans[j]);\n for (int j = 0; j < m; ++j)\n ans[j] = points[i + 1][j] + max(left[j], right[j]);\n }\n return *max_element(ans.begin(), ans.end());\n }\n};",
"memory": "117018"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<long long> d1;\n for (int p: points[0]) d1.push_back(p);\n vector<long long> d2(n);\n vector<long long> &cur = d2, &prev = d1;\n for (int i = 1; i < m; i++) {\n vector<long long> left_max(n), right_max(n);\n left_max[0] = prev[0];\n for (int j = 1; j < n; j++) left_max[j] = max(left_max[j-1] - 1, prev[j]);\n right_max.back() = prev.back();\n for (int j = n-2; j >= 0; j--) right_max[j] = max(right_max[j+1] - 1, prev[j]);\n for (int j = 0; j < n; j++) {\n cur[j] = points[i][j] + max(left_max[j], right_max[j]);\n }\n swap(prev, cur);\n }\n return *max_element(prev.begin(), prev.end());\n }\n};",
"memory": "118306"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n vector<long long> prev, curr(n, 0);\n for(auto i : points[0]) prev.push_back(i);\n\n for(int i = 1; i < m; i++){\n vector<long long> left(n, 0), right(n, 0);\n left[0] = prev[0];\n for(int j = 1; j < n; j++){\n left[j] = max(prev[j], left[j - 1] - 1);\n }\n\n right[n - 1] = prev[n - 1];\n for(int j = n - 2; j >= 0; j--){\n right[j] = max(prev[j], right[j + 1] - 1);\n }\n\n for(int j = 0; j < n; j++){\n curr[j] = points[i][j] + max(left[j], right[j]);\n }\n prev = curr;\n }\n return *max_element(prev.begin(), prev.end());\n\n }\n};",
"memory": "118306"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n vector<vector<long long>> dp(m, vector<long long>(n, 0));\n\n for (int i = 0; i < m; i++) {\n if (i == 0) {\n for (int j = 0; j < n; j++) dp[i][j] = points[i][j];\n } else {\n vector<long long> left(n);\n\n left[0] = dp[i-1][0];\n for (int j = 1; j < n; j++) left[j] = max(left[j-1] - 1, dp[i-1][j]);\n\n for (int j = n-2; j >= 0; j--) left[j] = max(left[j], left[j+1] - 1);\n\n for (int j = 0; j < n; j++) dp[i][j] = points[i][j] + left[j];\n }\n }\n\n return *max_element(begin(dp[m-1]), end(dp[m-1]));\n }\n};\n",
"memory": "119593"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n vector<vector<long long>> dp(m, vector<long long>(n));\n for(int i = 0; i < n; i++)\n dp[0][i] = points[0][i];\n\n for(int i = 1; i < m; i++) {\n vector<long long> suffixMax(n + 1, -100000);\n for(int j = n - 1; j >= 0; j--)\n suffixMax[j] = max(suffixMax[j + 1], dp[i - 1][j] - j);\n \n long long prefixMax = -2;\n for(int j = 0; j < n; j++) {\n dp[i][j] = max(prefixMax, suffixMax[j] + j) + points[i][j];\n prefixMax = max(prefixMax, dp[i - 1][j]) - 1;\n }\n }\n\n long long ans = -1;\n for(int i = 0; i < n; i++)\n ans = max(ans, dp[m - 1][i]);\n return ans;\n }\n};\n\n/*\ndp[i - 1][0] - 0, dp[i - 1][1] - 1, dp[i - 1][2] - 2\ndp[i - 1][0] - 1, dp[i - 1][1] - 0, dp[i - 1][2] - 1\ndp[i - 1][0] - 2, dp[i - 1][1] - 1, dp[i - 1][2] - 0\nrange update + range query\n*/",
"memory": "119593"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n long long mx = -1;\n int n = points.size(), m = points[0].size();\n cout << n << \" \" << m << endl;\n if (n == 1)\n return *max_element(points[0].begin(), points[0].end());\n vector<long long> fwd(m, 0);\n for (int i = 0; i < m; i++)\n {\n fwd[i] = points[n-1][i];\n }\n for (int i = n-2; i >= 0; i--)\n {\n vector<long long> curr(m, 0);\n vector<long long> left(m,0), right(m, 0);\n left[0] = fwd[0];\n for (int k = 1; k < m; k++)\n {\n left[k] = max(fwd[k], (left[k-1]-1));\n }\n right[m-1] = fwd[m-1];\n curr[m-1] = points[i][m-1] + max(left[m-1], right[m-1]);\n for (int k = m-2; k >= 0; k--)\n {\n right[k] = max(fwd[k], right[k+1]-1);\n curr[k] = points[i][k] + max(left[k], right[k]);\n }\n //mx = max(mx, curr[j]);\n fwd = curr;\n }\n return *max_element(fwd.begin(), fwd.end());;\n }\n};",
"memory": "128606"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long maxPoints(std::vector<std::vector<int>>& points) {\n int m = points.size();\n int n = points[0].size();\n\n if (m == 1) {\n return *std::max_element(points[0].begin(), points[0].end());\n }\n\n // Initialize dp with values from the last row\n std::vector<long long> dp(n);\n for (int i = 0; i < n; ++i) {\n dp[i] = points[m - 1][i];\n }\n\n // Process rows from bottom to top\n for (int i = m - 2; i >= 0; --i) {\n std::vector<long long> new_dp(n, LLONG_MIN);\n\n // Compute max points for each column considering penalties\n long long left_max = LLONG_MIN;\n std::vector<long long> left(n, LLONG_MIN);\n\n for (int j = 0; j < n; ++j) {\n left_max = std::max(left_max, dp[j] + j);\n left[j] = left_max;\n }\n\n long long right_max = LLONG_MIN;\n std::vector<long long> right(n, LLONG_MIN);\n\n for (int j = n - 1; j >= 0; --j) {\n right_max = std::max(right_max, dp[j] - j);\n right[j] = right_max;\n }\n\n for (int j = 0; j < n; ++j) {\n new_dp[j] = points[i][j] + std::max(left[j] - j, right[j] + j);\n }\n\n dp = new_dp;\n }\n\n // The result is the maximum value in the dp array\n return *std::max_element(dp.begin(), dp.end());\n }\n};",
"memory": "128606"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int m = points.size(), n = points[0].size();\n\n std::vector<long long> score(points[0].begin(), points[0].end());\n\n for (int i = 1; i < m; i++) {\n std::vector<int> &row = points[i];\n std::vector<long long> currScore(n);\n\n std::vector<long long> leftContribution = {score[0]};\n leftContribution.reserve(n);\n \n for (int j = 1; j < n; j++) {\n leftContribution.push_back(\n std::max(leftContribution.back() - 1, score[j])\n );\n }\n\n std::vector<long long> rightContribution(n);\n rightContribution.back() = score.back();\n for (int j = n - 2; j >= 0; j--) {\n rightContribution[j] = std::max(rightContribution[j + 1] - 1, score[j]);\n }\n\n for (int j = 0; j < n; j++) {\n currScore[j] = row[j] + std::max(leftContribution[j], rightContribution[j]);\n }\n\n score = std::move(currScore);\n }\n\n return *std::max_element(score.begin(), score.end());\n }\n};",
"memory": "129893"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvoid check(vector<vector<long long>>&dp,int curr_row,int row,int col,vector<vector<int>>&points){\n\n if(curr_row>=row){\n return ;\n }\n \n\n if(curr_row==row-1){\n for(int i=0;i<col;i++){\n dp[curr_row][i]=points[curr_row][i];\n }\n return;\n }\n if(dp[curr_row][0]==INT_MIN){\n check(dp,curr_row+1,row,col,points);\n }\n \nlong long left[col];\nlong long right[col];\n//calculate(dp,curr_row+1,0,row,col,points);\nleft[0]=dp[curr_row+1][0];\nfor(int i=1;i<col;i++){\n left[i]=max(dp[curr_row+1][i],left[i-1]-1);\n}\n//calculate(dp,curr_row+1,col-1,row,col,points);\nright[col-1]=dp[curr_row+1][col-1];\n//cout<<right[col-1]<<endl;\nfor(int i=col-2;i>-1;i--){\n right[i]=max(dp[curr_row+1][i],right[i+1]-1);\n}\n\nfor(int i=0;i<col;i++){\n dp[curr_row][i]=points[curr_row][i]+max(left[i],right[i]);\n}\nreturn;\n// for(int i=0;i<col;i++){\n \n// dp[curr_row][curr_col]=max(dp[curr_row][curr_col],calculate(dp,curr_row+1,i,row,col,points)-abs(curr_col-i)+points[curr_row][curr_c);\n \n \n// }\n\n//dp[curr_row][curr_col]=points[curr_row][curr_col]+max(left[curr_col],right[curr_col]);\nreturn;\n}\n long long maxPoints(vector<vector<int>>& points) {\n int row=points.size();\n int col=points[0].size();\n vector<vector<long long>>dp(row,vector<long long>(col,INT_MIN));\nint ans=0;\n//for(int i=0;i<col;i++){\n check(dp,0,row,col,points);\n // }\n return *max_element(dp[0].begin(),dp[0].end());}\n};",
"memory": "129893"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n vector<long long> previousRow;\n for(int i=0 ; i < m ; i++){\n previousRow.push_back(points[0][i]);\n }\n\n for(int row =0 ; row < n-1 ; row++){\n vector<long long> leftMax(m), rightMax(m), currRow(m);\n\n leftMax[0] = previousRow[0];\n for(int col=1 ; col < m ; col++){\n leftMax[col] = max(leftMax[col-1]-1, previousRow[col]);\n }\n rightMax[m-1] = previousRow[m-1];\n for(int col=m-2 ; col >=0 ; col--){\n rightMax[col] = max(rightMax[col+1]-1, previousRow[col]);\n }\n\n for(int col =0 ; col < m ; col++) {\n currRow[col] = points[row+1][col] + max(leftMax[col], rightMax[col]);\n }\n previousRow = currRow;\n }\n return *max_element(previousRow.begin(), previousRow.end());\n }\n};",
"memory": "131181"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // dp solution\n // int f(vector<vector<int>>& dp,vector<vector<int>>& points, int i, int prev_col){\n // if(i>=points.size()){\n // return 0;\n // }\n // if(dp[i][prev_col] != -1) return dp[i][prev_col];\n // int a = INT_MIN;\n // for(int j=0;j<points[i].size();j++){\n // a = max(a,points[i][j] - abs(j-prev_col) + f(dp,points,i+1,j));\n // }\n // return dp[i][prev_col] = a;\n // }\n // long long maxPoints(vector<vector<int>>& points) {\n // int n = points.size();\n // int m = points[0].size();\n // int ans = INT_MIN;\n // vector<vector<int>> dp(n+1,vector<int>(m+1,-1));\n // for(int i=0;i<points[0].size();i++)\n // ans = max(ans,points[0][i] + f(dp,points,1,i));\n // return ans;\n // }\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n vector<long long> x;\n for(int i=0;i<m;i++){\n x.push_back(points[0][i]);\n }\n for(int i=1;i<n;i++){\n \n vector<long long> leftswipe(m,0);\n vector<long long> rightswipe(m,0);\n vector<long long> current(m,0);\n\n // left to right swipe\n leftswipe[0] = x[0];\n for(int j = 1;j < m;j++){\n leftswipe[j] = max(leftswipe[j-1]-1,x[j]);\n }\n // right to left swipe\n rightswipe[m-1] = x[m-1];\n for(int j = m-2;j >= 0;j--){\n rightswipe[j] = max(rightswipe[j+1]-1,x[j]);\n } \n for(int j = 0; j < m ; j++){\n current[j] = points[i][j] + max(leftswipe[j],rightswipe[j]);\n }\n swap(x,current);\n\n }\n long long ans = INT_MIN;\n for(int i = 0;i < m ; i++){\n ans = max(ans,x[i]);\n }\n return ans;\n }\n};",
"memory": "131181"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n vector<long long> prev(m, 0);\n \n for (int j = 0; j < m; ++j) {\n prev[j] = points[0][j];\n }\n for (int i = 1; i < n; ++i) {\n vector<long long> left(m, 0), right(m, 0), curr(m, 0);\n left[0] = prev[0];\n for (int j = 1; j < m; ++j) {\n left[j] = max(left[j - 1] - 1, prev[j]);\n }\n right[m - 1] = prev[m - 1];\n for (int j = m - 2; j >= 0; --j) {\n right[j] = max(right[j + 1] - 1, prev[j]);\n }\n for (int j = 0; j < m; ++j) {\n curr[j] = points[i][j] + max(left[j], right[j]);\n }\n prev= curr;\n }\n sort(prev.rbegin(), prev.rend());\n return prev[0];\n }\n};\n",
"memory": "132468"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& points) {\n int n = points.size();\n int m = points[0].size();\n vector<long long> dp(m, 0);\n\n for (int j = 0; j < n; ++j) {\n vector<long long> new_dp(m, 0);\n vector<long long> leftMax(m, numeric_limits<long long>::min()), rightMax(m, numeric_limits<long long>::min());\n\n // Compute leftMax\n leftMax[0] = dp[0];\n for (int i = 1; i < m; ++i) {\n leftMax[i] = max(leftMax[i - 1] - 1, dp[i]);\n }\n\n // Compute rightMax\n rightMax[m - 1] = dp[m - 1];\n for (int i = m - 2; i >= 0; --i) {\n rightMax[i] = max(rightMax[i + 1] - 1, dp[i]);\n }\n\n // Update new_dp based on leftMax and rightMax\n for (int i = 0; i < m; ++i) {\n new_dp[i] = max(leftMax[i], rightMax[i]) + points[j][i];\n }\n\n dp = new_dp;\n }\n\n return *max_element(dp.begin(), dp.end());\n }\n};",
"memory": "132468"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long util (vector<vector<int>> &p, int row, int lastCol, vector<vector<int>> &dp) {\n if (row == p.size()) return 0;\n\n if(dp[row][lastCol+1]!=-1) return dp[row][lastCol+1];\n long long ans=0;\n for (int i=0; i<p[0].size(); i++) {\n long long curr = 0;\n if (lastCol == -1) {\n curr = p[row][i] + util(p, row+1, i, dp);\n }else{\n curr = p[row][i]-abs(i-lastCol)+util(p, row+1, i, dp);\n }\n ans = max(ans, curr);\n }\n dp[row][lastCol+1] = ans;\n return dp[row][lastCol+1];\n }\n long long maxPoints (vector<vector<int>>& p) {\n int rows = p.size();\n int cols = p[0].size();\n\n // dp[row][col] represents the max points you can get from row 'row' and column 'col'\n vector<vector<long long>> dp(rows, vector<long long>(cols, 0));\n\n // Base case: For the last row, the points are just the values in the last row\n for (int col = 0; col < cols; col++) {\n dp[rows-1][col] = p[rows-1][col];\n }\n\n // Fill the dp table in bottom-up fashion\n for (int row = rows - 2; row >= 0; row--) {\n // We need two arrays to track max from left to right and right to left\n vector<long long> left(cols, 0), right(cols, 0);\n \n // left[i] will store the max points if we come from the left side to i\n left[0] = dp[row+1][0];\n for (int i = 1; i < cols; i++) {\n left[i] = max(left[i-1] - 1, dp[row+1][i]);\n }\n\n // right[i] will store the max points if we come from the right side to i\n right[cols-1] = dp[row+1][cols-1];\n for (int i = cols - 2; i >= 0; i--) {\n right[i] = max(right[i+1] - 1, dp[row+1][i]);\n }\n\n // Now fill the dp[row][col] based on left and right arrays\n for (int col = 0; col < cols; col++) {\n dp[row][col] = p[row][col] + max(left[col], right[col]);\n }\n }\n\n // The answer will be the max value in the first row\n long long ans = 0;\n for (int col = 0; col < cols; col++) {\n ans = max(ans, dp[0][col]);\n }\n return ans;\n }\n};",
"memory": "133756"
} |
2,067 | <p>You are given an <code>m x n</code> integer matrix <code>points</code> (<strong>0-indexed</strong>). Starting with <code>0</code> points, you want to <strong>maximize</strong> the number of points you can get from the matrix.</p>
<p>To gain points, you must pick one cell in <strong>each row</strong>. Picking the cell at coordinates <code>(r, c)</code> will <strong>add</strong> <code>points[r][c]</code> to your score.</p>
<p>However, you will lose points if you pick a cell too far from the cell that you picked in the previous row. For every two adjacent rows <code>r</code> and <code>r + 1</code> (where <code>0 <= r < m - 1</code>), picking cells at coordinates <code>(r, c<sub>1</sub>)</code> and <code>(r + 1, c<sub>2</sub>)</code> will <strong>subtract</strong> <code>abs(c<sub>1</sub> - c<sub>2</sub>)</code> from your score.</p>
<p>Return <em>the <strong>maximum</strong> number of points you can achieve</em>.</p>
<p><code>abs(x)</code> is defined as:</p>
<ul>
<li><code>x</code> for <code>x >= 0</code>.</li>
<li><code>-x</code> for <code>x < 0</code>.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong><strong> </strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-40-26-diagram-drawio-diagrams-net.png" style="width: 300px; height: 300px;" />
<pre>
<strong>Input:</strong> points = [[1,2,3],[1,5,1],[3,1,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 2), (1, 1), and (2, 0).
You add 3 + 5 + 3 = 11 to your score.
However, you must subtract abs(2 - 1) + abs(1 - 0) = 2 from your score.
Your final score is 11 - 2 = 9.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/12/screenshot-2021-07-12-at-13-42-14-diagram-drawio-diagrams-net.png" style="width: 200px; height: 299px;" />
<pre>
<strong>Input:</strong> points = [[1,5],[2,3],[4,2]]
<strong>Output:</strong> 11
<strong>Explanation:</strong>
The blue cells denote the optimal cells to pick, which have coordinates (0, 1), (1, 1), and (2, 0).
You add 5 + 3 + 4 = 12 to your score.
However, you must subtract abs(1 - 1) + abs(1 - 0) = 1 from your score.
Your final score is 12 - 1 = 11.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>m == points.length</code></li>
<li><code>n == points[r].length</code></li>
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
<li><code>0 <= points[r][c] <= 10<sup>5</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long maxPoints(vector<vector<int>>& p) {\n int r = p.size();\n int c = p[0].size();\n \n // Create a dp array to store maximum points for each cell\n vector<vector<long long>> dp(r, vector<long long>(c, 0));\n \n // Initialize the first row with the same values as p\n for (int j = 0; j < c; ++j) {\n dp[0][j] = p[0][j];\n }\n\n // Iterate over each row, starting from the second one\n for (int i = 1; i < r; ++i) {\n vector<long long> left(c, 0), right(c, 0);\n\n // Compute left array\n left[0] = dp[i-1][0];\n for (int j = 1; j < c; ++j) {\n left[j] = max(left[j-1] - 1, dp[i-1][j]);\n }\n\n // Compute right array\n right[c-1] = dp[i-1][c-1];\n for (int j = c-2; j >= 0; --j) {\n right[j] = max(right[j+1] - 1, dp[i-1][j]);\n }\n\n // Update the dp values for the current row\n for (int j = 0; j < c; ++j) {\n dp[i][j] = p[i][j] + max(left[j], right[j]);\n }\n }\n\n // Find the maximum value in the last row\n long long result = 0;\n for (int j = 0; j < c; ++j) {\n result = max(result, dp[r-1][j]);\n }\n\n return result;\n }\n};",
"memory": "133756"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long mostBooked(long long n, vector<vector<int>>& meetings) {\n if(meetings.size() == 1) return 0;\n vector<long long> endtime(n,0); \n vector<long long> count(n,0);\n sort(meetings.begin() , meetings.end());\n for(long long i = 0 ; i < meetings.size() ; i++){\n long long start = meetings[i][0];\n long long end = meetings[i][1];\n bool flag = false;\n long long minn = LONG_MAX;\n long long minindex = -1;\n for(long long j = 0 ; j < n ; j++){\n if(endtime[j] < minn){\n minn = endtime[j];\n minindex = j;\n }\n if(endtime[j] <= start){\n count[j]++;\n endtime[j] = end;\n flag = true;\n break;\n }\n }\n if(!flag){\n count[minindex]++;\n endtime[minindex]+=end - start;\n }\n }\n long long maxx = INT_MIN;\n long long mi = -1;\n for(long long i = 0 ; i < n ; i++){\n if(count[i] > maxx){\n maxx = count[i];\n mi = i;\n }\n }\n return mi;\n }\n};",
"memory": "97631"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long mostBooked(int n, vector<vector<int>>& meetings) {\n if(meetings.size()==1){return 0;}\n vector<long long>endtime(n,0);//a vector to store the info about rroms\n vector<long long> count(n,0);//vector to store how many time rooms being used\n sort(meetings.begin(), meetings.end());\n\n for(long long i=0;i<meetings.size();i++){\n long long start=meetings[i][0];//placing start pointer \n long long end=meetings[i][1];//lly \n bool flag=false;\n long long min=LONG_MAX;//making a min variable to gget the room wich will be free first\n long long minindex=-1;//to get index of thst room\n\n for(long long j=0;j<n;j++){//this loop works for rrrom basically in thrre parts\n\n //part 1\n //gets the min and min index of rooms\n if(endtime[j] < min){\n min=endtime[j];\n minindex=j;\n }\n //checks if room is free if yes increase the count \n if(endtime[j]<=start){\n \n count[j]++;\n endtime[j]=end;\n flag=true;\n break;\n }\n \n }\n //if no room free innc count of the room thst will be free the first\n //and updsate its ending time as +=end-start\n //eg(ending timme of room is 9,meeting come s(8,12),new rime will be 12-8+9=13)\n if(!flag){\n count[minindex]++;\n endtime[minindex]+=end-start;\n }\n\n }\n long long max=LONG_MIN;\n long long ans=-1;\n//just get the room with highest\n for(long long i=0;i<n;i++){\n if(count[i]>max){\n max=count[i];\n ans=i;\n }\n }\n return ans;\n \n }\n};",
"memory": "97631"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "typedef pair<long long,int>pi; //first num will be end time, second num will be room num\ntypedef vector<pi> vpi;\n // bool compare(pi a, pi b) \n // {\n // if(a.first>b.first)\n // return true;\n // if(a.first==b.first)\n // return a.second>b.second;\n // return false;\n // }\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<pi,vpi,greater<pi>>pq;\n priority_queue<int,vector<int>,greater<int>>unused;\n vector<int>vec(meetings.size());\n sort(meetings.begin(),meetings.end());\n for(int i=0;i<n;++i)\n {\n unused.push(i);\n }\n for(int i=0;i<meetings.size();++i)\n {\n while(!pq.empty() && meetings[i][0]>=pq.top().first)\n {\n unused.push(pq.top().second);\n pq.pop();\n }\n if(!unused.empty())\n {\n int roomnum=unused.top();\n pq.push(make_pair(meetings[i][1],roomnum));\n unused.pop();\n ++vec[roomnum];\n }\n else\n {\n pi a = pq.top();\n pq.pop();\n pq.push(make_pair(a.first+(meetings[i][1]-meetings[i][0]), a.second));\n ++vec[a.second];\n }\n }\n int maxm=-1,maxi=0;\n for(int i=0;i<vec.size();++i)\n {\n if(vec[i]>maxm)\n {\n maxm=vec[i];\n maxi=i;\n }\n }\n return maxi;\n }\n};\n\n/*\n3\n[[1,20],[2,10],[3,5],[4,9],[6,8]]\n3\n[[1,4],[3,12],[4,13],[2,10],[0,4],[5,6],[9,12],[6,7],[8,9],[10,11]]\n3\n[[1,2],[2,3]]\n2\n[[0,10],[1,2],[12,14],[13,15]]\n4\n[[18,19],[3,12],[17,19],[2,13],[7,10]]\n2\n[[0,10],[1,2],[12,14],[13,15]]\n*/",
"memory": "98293"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);;\n std::vector<int> temp(n);\n for (int i = 0; i < n; ++i) {\n temp[i] = i;\n }\n std::priority_queue<int, std::vector<int>, std::greater<int>>\n smallest_room(temp.begin(), temp.end());\n\n std::sort(meetings.begin(), meetings.end(),\n [](const std::vector<int>& m1, const std::vector<int>& m2) {\n return m1[0] < m2[0];\n });\n\n std::vector<int> num_meetings(n, 0);\n std::priority_queue<std::pair<long long int, long long int>,\n std::vector<std::pair<long long int, long long int>>,\n std::greater<std::pair<long long int, long long int>>>\n ends;\n int i = 0;\n while (i < meetings.size()) {\n if (!ends.empty() && ends.top().first <= meetings[i][0]) {\n smallest_room.push(ends.top().second);\n ends.pop();\n } else if (!smallest_room.empty()) {\n int room = smallest_room.top();\n smallest_room.pop();\n ++num_meetings[room];\n ends.push({meetings[i][1], room});\n ++i;\n } else {\n const auto [prev_end, room] = ends.top();\n ends.pop();\n long long int new_end_time = prev_end + meetings[i][1] - meetings[i][0];\n ++num_meetings[room];\n ends.push({new_end_time, room});\n ++i;\n }\n }\n\n int most_booked_room = 0;\n for (int room = 1; room < n; ++room) {\n if (num_meetings[room] > num_meetings[most_booked_room]) {\n most_booked_room = room;\n }\n }\n return most_booked_room;\n }\n};",
"memory": "98956"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n int m = meetings.size();\n vector <int> id(m);\n iota(begin(id), end(id), 0);\n sort(begin(id), end(id), [&](auto & x, auto & y) {\n return meetings[x][0] < meetings[y][0] || (meetings[x][0] == meetings[y][0] && x < y);\n });\n\n priority_queue <pair<long,int>, vector<pair<long,int>>, greater<>> used;\n priority_queue <int, vector<int>, greater<>> free;\n for (int i = 0; i < n; i++) free.push(i);\n\n long t = 0;\n vector <int> rooms(m);\n int cnt = 0, cur = 0;\n\n for (int x: id) {\n auto & meeting = meetings[x];\n t = max(t, meeting[0] + 0L);\n if (free.empty()) {\n t = max(t, 0L + used.top().first);\n }\n while (!used.empty() && used.top().first <= t) {\n auto [t_, id] = used.top(); used.pop();\n free.push(id);\n }\n auto u = free.top(); free.pop();\n used.emplace(t + meeting[1] - meeting[0], u);\n if (++rooms[u] > cnt || rooms[u] == cnt && cur > u) {\n cnt = rooms[u];\n cur = u;\n }\n }\n\n return cur;\n }\n};",
"memory": "98956"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<tuple<int, int>> sort_meetings(meetings.size());\n for (int i = 0; i < meetings.size(); ++i) {\n sort_meetings[i] = {meetings[i][0], meetings[i][1]};\n }\n sort(sort_meetings.begin(), sort_meetings.end());\n\n vector<int> counter(n, 0);\n\n priority_queue<int, vector<int>, greater<int>> available_room;\n for (int i = 0; i < n; ++i) {\n available_room.push(i);\n }\n\n // end time, room number\n priority_queue<tuple<long, int>, vector<tuple<long, int>>,\n greater<tuple<long, int>>>\n min_pq;\n for (auto& [current_start_time, current_end_time] : sort_meetings) {\n // cout << current_start_time << \",\";\n if (min_pq.empty()) {\n counter[available_room.top()]++;\n min_pq.push({current_end_time, available_room.top()});\n available_room.pop();\n continue;\n }\n\n while (!min_pq.empty() &&\n current_start_time >= get<0>(min_pq.top())) {\n available_room.push(get<1>(min_pq.top()));\n min_pq.pop();\n }\n\n if (!available_room.empty()) {\n counter[available_room.top()]++;\n min_pq.push({current_end_time, available_room.top()});\n available_room.pop();\n continue;\n }\n\n // No room available.\n // Free a room for use.\n long end_time = get<0>(min_pq.top());\n int room_number = get<1>(min_pq.top());\n min_pq.pop();\n // cout << end_time << \" and \" << room_number << endl;\n counter[room_number]++;\n\n // int next_end_time =\n // (current_start_time < end_time)\n // ? (end_time + (current_end_time - current_start_time))\n // : current_end_time;\n long next_end_time =\n end_time + (current_end_time - current_start_time);\n\n min_pq.push({next_end_time, room_number});\n }\n\n // [2,13]\n // [3,12]\n // [7,10]\n // [17,19]\n // [[18,19]]\n\n // for (int j = 0; j < n; ++j) {\n // cout << counter[j] << \",\";\n // }\n // cout << endl;\n\n int max_counter = counter[0];\n int max_i = 0;\n for (int j = 1; j < n; ++j) {\n if (counter[j] > max_counter) {\n max_counter = counter[j];\n max_i = j;\n }\n }\n return max_i;\n }\n};",
"memory": "99618"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;\n sort(meetings.begin(), meetings.end());\n vector<int> ans(n, 0);\n ans[0]++;\n pq.push({meetings[0][1],0});\n vector<int>temp;\n for(int i=n-1;i>=1;i--) temp.push_back(i);\n long long t=0;\n for(int i=1;i<meetings.size();i++)\n {\n // for(int i:temp) cout<<i<<\" \";\n // cout<<endl;\n bool flag=true;\n while(!pq.empty() && pq.top().first<=meetings[i][0])\n {\n pair<long long,int> p;\n p=pq.top();\n pq.pop();\n temp.push_back(p.second);\n t=0;\n flag=false;\n }\n if(flag==false) sort(temp.rbegin(),temp.rend());\n\n // for(int i:temp) cout<<i<<\" \";\n // cout<<endl;\n\n if(temp.empty())\n {\n pair<long long,int> x;\n x=pq.top();\n pq.pop();\n t=x.first;\n temp.push_back(x.second);\n while(!pq.empty() && pq.top().first==t)\n {\n pair<long long,int> p;\n p=pq.top();\n pq.pop();\n temp.push_back(p.second);\n }\n sort(temp.rbegin(),temp.rend());\n }\n // for(int i:temp) cout<<i<<\" \";\n // cout<<endl;\n long long h=t;\n h+=meetings[i][1];\n h-=meetings[i][0];\n pq.push({max(h,(long long)meetings[i][1]),temp.back()});\n ans[temp.back()]++;\n temp.pop_back();\n }\n int a=-1,b=-1;\n for(int i=0;i<n;i++)\n {\n // cout<<ans[i]<<\" \";\n if(ans[i]>b)\n {\n b=ans[i];\n a=i;\n }\n }\n return a;\n }\n};",
"memory": "100281"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "#define ll long long\nclass Solution {\npublic:\n struct Compare{\n bool operator()(pair<ll,int>&x, pair<ll,int>&y){\n return x.second > y.second;\n }\n };\n int mostBooked(int n, vector<vector<int>>& meetings) {\n if(n==1){\n return 0;\n }\n sort(meetings.begin(), meetings.end());\n priority_queue< pair<ll,int>, vector< pair<ll,int> >, greater< pair<ll,int> > >pq;\n int idx = 0, sz=meetings.size();\n vector<int>count(n);\n count[0]=1;\n pq.push( pair<ll,int>(meetings[0][1],idx) );\n idx++;\n for(int i=1;i<sz;i++){\n pair<ll,int> xx = pq.top();\n if(xx.first<=meetings[i][0]){\n cout<<\"start\"<<endl;\n \n priority_queue< pair<ll,int>, vector< pair<ll,int> >, Compare >holder;\n while(!pq.empty() && pq.top().first<=meetings[i][0]){\n holder.push(pq.top());\n pq.pop();\n }\n xx = holder.top();\n xx.first = meetings[i][1];\n count[xx.second]++;\n holder.pop();\n pq.push(xx);\n cout<<xx.first<<\" \"<<xx.second<<endl;\n\n while(!holder.empty()){\n pq.push(holder.top());\n cout<<holder.top().first<<\" \"<<holder.top().second<<endl;\n holder.pop();\n }\n cout<<endl;\n }else{\n if(idx<n){\n pq.push( pair<ll,int>(meetings[i][1],idx) );\n count[idx]++;\n idx++;\n }else{\n xx.first = xx.first + meetings[i][1]-meetings[i][0];\n count[xx.second]++;\n pq.pop();\n pq.push(xx);\n }\n }\n }\n int Max = count[0];\n idx=0;\n for(int i=0; i<n; i++){\n cout<<count[i]<<\" \";\n }\n cout<<endl;\n for(int i=1; i<n; i++){\n if( count[i] > Max ){\n Max = count[i];\n idx = i;\n }\n }\n return idx;\n }\n};",
"memory": "100943"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<int> c(105, 0);\n sort(meetings.begin(), meetings.end());\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q;\n for(int i=0;i<n;++i) q.push({0, i});\n for(int i=0;i<meetings.size();++i) {\n vector<int> rooms;\n while(q.size() && q.top().first <= meetings[i][0]) {\n rooms.push_back(q.top().second);\n q.pop();\n }\n for(auto l : rooms) q.push({0, l});\n auto curr = q.top();\n q.pop();\n if(meetings[i][0] < curr.first) {\n q.push({curr.first + (meetings[i][1] - meetings[i][0]), curr.second});\n } else {\n q.push({meetings[i][1], curr.second});\n }\n c[curr.second]++;\n }\n int ans = -1, target;\n for(int i=0;i<n;++i) {\n if(c[i] > ans) {\n ans = c[i];\n target = i;\n }\n }\n return target;\n }\n};",
"memory": "101606"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<pair<long long, long long>> meets(meetings.size());\n for (int i = 0; i < meetings.size(); i++) {\n meets[i] = { meetings[i][0], meetings[i][1] };\n }\n sort(meets.begin(), meets.end());\n\n priority_queue<int, vector<int>, greater<int>> emptyRooms;\n for (int i = 0; i < n; i++) {\n emptyRooms.push(i);\n }\n\n vector<int> roomUsageCnt(n, 0);\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> endingTimes;\n long long currTime = 0;\n\n for (int i = 0; i < meets.size(); i++) {\n if (emptyRooms.size() == 0) {\n currTime = max(currTime, endingTimes.top().first);\n }\n\n while (endingTimes.size() > 0 && endingTimes.top().first <= currTime) {\n int freedRoom = endingTimes.top().second;\n emptyRooms.push(freedRoom);\n endingTimes.pop();\n }\n\n if (meets[i].first <= currTime) {\n int room = emptyRooms.top();\n emptyRooms.pop();\n roomUsageCnt[room]++;\n endingTimes.push({ (currTime - meets[i].first) + meets[i].second, room });\n } else {\n currTime = meets[i].first;\n i--;\n }\n }\n\n int highCnt = -1;\n for (int i = 0; i < roomUsageCnt.size(); i++) {\n highCnt = max(highCnt, roomUsageCnt[i]);\n }\n\n for (int i = 0; i < roomUsageCnt.size(); i++) {\n if (highCnt == roomUsageCnt[i]) {\n return i;\n }\n }\n\n return -1;\n }\n};",
"memory": "102268"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n struct Comp {\n bool operator()(pair<long long, long long>&a, pair<long long, long long>&b) {\n if(a.second != b.second) {\n return a.second > b.second;\n }\n return a.first > b.first;\n }\n };\n\n // void print(priority_queue<pair<int, int>, vector<pair<int, int>>, Comp> pq) {\n // cout<<\"printing .... \"<<endl;\n // while(!pq.empty()) {\n // cout<<pq.top().first<<\" \"<<pq.top().second<<endl;\n // pq.pop();\n // }\n // cout<<\"done .... \"<<endl;\n // }\n\n static bool custom(vector<int>&a, vector<int>&b) {\n if(a[0] != b[0]) {\n return a[0] < b[0];\n }\n return a[1] < b[1];\n }\n\n int mostBooked(int n, vector<vector<int>>& meetings) {\n\n sort(meetings.begin(), meetings.end(), custom);\n vector<long long> counts(n, 0);\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, Comp> pq;\n for(int i = 0; i < n; i++) {\n pq.push({i, 0});\n }\n // print(pq);\n for(int i = 0; i < meetings.size(); i++) {\n long long start = meetings[i][0];\n long long end = meetings[i][1];\n long long interval = end-start;\n // cout<<start<<\" \"<<end<<\" \"<<interval<<endl;\n if(start <= pq.top().second) {\n long long index = pq.top().first;\n long long time = pq.top().second;\n counts[index]++;\n pq.pop();\n pq.push({index, time+interval});\n // cout<<\"case1 \"<<index<<\" \"<<time<<endl;\n } else if(start > pq.top().second) {\n vector<long long> indicesPopped;\n while(!pq.empty() && start >= pq.top().second) {\n long long index = pq.top().first;\n long long time = pq.top().second; \n pq.pop();\n indicesPopped.push_back(index);\n // pq.push({index, 0});\n // cout<<\"case2.1 \"<<index<<\" \"<<time<<endl;\n }\n for(int j = 0; j < indicesPopped.size(); j++) {\n pq.push({indicesPopped[j], 0});\n }\n long long index = pq.top().first;\n counts[index]++;\n pq.pop();\n pq.push({index, end});\n // cout<<\"case2.2 \"<<index<<\" \"<<end<<endl;\n }\n // print(pq);\n }\n long long result = 0;\n long long maxVal = 0;\n for(int i = 0; i < n; i++) {\n cout<<i<<\" \"<<counts[i]<<endl;\n if(counts[i] > maxVal) {\n maxVal = counts[i];\n result = i;\n }\n }\n return result;\n \n }\n};",
"memory": "102931"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n struct Comp {\n bool operator()(pair<long long, long long>&a, pair<long long, long long>&b) {\n if(a.second != b.second) {\n return a.second > b.second;\n }\n return a.first > b.first;\n }\n };\n\n static bool custom(vector<int>&a, vector<int>&b) {\n if(a[0] != b[0]) {\n return a[0] < b[0];\n }\n return a[1] < b[1];\n }\n\n int mostBooked(int n, vector<vector<int>>& meetings) {\n\n sort(meetings.begin(), meetings.end(), custom);\n vector<long long> counts(n, 0);\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, Comp> pq;\n for(int i = 0; i < n; i++) {\n pq.push({i, 0});\n }\n for(int i = 0; i < meetings.size(); i++) {\n long long start = meetings[i][0];\n long long end = meetings[i][1];\n long long interval = end-start;\n if(start <= pq.top().second) {\n long long index = pq.top().first;\n long long time = pq.top().second;\n counts[index]++;\n pq.pop();\n pq.push({index, time+interval});\n } else if(start > pq.top().second) {\n vector<long long> indicesPopped;\n while(!pq.empty() && start >= pq.top().second) {\n long long index = pq.top().first;\n long long time = pq.top().second; \n pq.pop();\n indicesPopped.push_back(index);\n }\n for(int j = 0; j < indicesPopped.size(); j++) {\n pq.push({indicesPopped[j], 0});\n }\n long long index = pq.top().first;\n counts[index]++;\n pq.pop();\n pq.push({index, end});\n }\n }\n long long result = 0;\n long long maxVal = 0;\n for(int i = 0; i < n; i++) {\n cout<<i<<\" \"<<counts[i]<<endl;\n if(counts[i] > maxVal) {\n maxVal = counts[i];\n result = i;\n }\n }\n return result;\n \n }\n};",
"memory": "103593"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct Meeting {\n int64_t start;\n int64_t end;\n\n inline int64_t duration() const {\n return end - start;\n }\n\n inline bool operator<(const Meeting& other) const {\n return start > other.start;\n }\n };\n\n struct Event {\n int64_t time;\n int room_id;\n int meeting_id;\n\n inline bool operator<(const Event& other) const {\n return time > other.time || (time == other.time && room_id > other.room_id);\n }\n };\n\n int mostBooked(int n, vector<vector<int>>& in_meetings) {\n std::vector<int> count(n, 0);\n std::vector<Meeting> meetings;\n meetings.reserve(in_meetings.size());\n for (const auto& in_meeting : in_meetings) {\n meetings.push_back({in_meeting[0], in_meeting[1]});\n }\n\n std::sort(meetings.begin(), meetings.end(), [](const auto& a, const auto& b) {\n return a.start < b.start;\n });\n\n std::priority_queue<int, std::vector<int>, std::greater<int>> avail_rooms;\n for (int i = 0; i < n; ++i) {\n avail_rooms.push(i);\n }\n\n std::priority_queue<Event> meeting_ends;\n\n\n for (int i = 0; i < meetings.size(); ++i) {\n const auto& meeting = meetings[i];\n\n while (!meeting_ends.empty() && meeting_ends.top().time <= meeting.start) {\n avail_rooms.push(meeting_ends.top().room_id);\n meeting_ends.pop();\n }\n\n if (!avail_rooms.empty()) {\n const auto top = avail_rooms.top();\n // std::cout << \"Delayed meeting started at \" << meeting.start << \" in room \" << top << std::endl;\n avail_rooms.pop();\n ++count[top];\n meeting_ends.push({\n meeting.start + meeting.duration(),\n top,\n i,\n });\n } else if (!meeting_ends.empty()) {\n const auto top = meeting_ends.top();\n meeting_ends.pop();\n // std::cout << \"Delayed meeting started at \" << top.time << \"in room \" << top.room_id << std::endl;\n ++count[top.room_id];\n meeting_ends.push({\n top.time + meeting.duration(),\n top.room_id,\n i\n });\n } else {\n std::cout << \"WWW\" << std::endl;\n }\n }\n\n int max_count = 0;\n int ans = 0;\n for (int i = 0; i < n; ++i) {\n if (count[i] > max_count) {\n max_count = count[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "103593"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct Meeting {\n int64_t start;\n int64_t end;\n\n inline int duration() const {\n return end - start;\n }\n\n inline bool operator<(const Meeting& other) const {\n return start > other.start;\n }\n };\n\n struct Event {\n int64_t time;\n int room_id;\n int meeting_id;\n\n inline bool operator<(const Event& other) const {\n return time > other.time || (time == other.time && room_id > other.room_id);\n }\n };\n\n int mostBooked(int n, vector<vector<int>>& in_meetings) {\n std::vector<int> count(n, 0);\n std::vector<Meeting> meetings;\n meetings.reserve(in_meetings.size());\n for (int i = 0; i < in_meetings.size(); ++i) {\n const auto& in_meeting = in_meetings[i];\n meetings.push_back({in_meeting[0], in_meeting[1]});\n }\n\n std::sort(meetings.begin(), meetings.end(), [](const auto& a, const auto& b) {\n return a.start < b.start;\n });\n\n std::priority_queue<Meeting> delayed_meetings;\n\n std::priority_queue<Event> events;\n\n // The ongoing meeting in each room.\n std::priority_queue<int, std::vector<int>, std::greater<int>> avail_rooms;\n for (int i = 0; i < n; ++i) {\n avail_rooms.push(i);\n }\n\n for (int i = 0; i < meetings.size(); ++i) {\n const auto& meeting = meetings[i];\n\n while (!events.empty()) {\n const auto top = events.top();\n if (top.time <= meeting.start) {\n events.pop();\n avail_rooms.push(top.room_id);\n } else {\n break;\n }\n }\n\n if (!avail_rooms.empty()) {\n const auto top = avail_rooms.top();\n std::cout << \"Delayed meeting started at \" << meeting.start << \" in room \" << top << std::endl;\n avail_rooms.pop();\n ++count[top];\n events.push({\n meeting.start + meeting.duration(),\n top,\n i,\n });\n } else if (!events.empty()) {\n const auto top = events.top();\n events.pop();\n std::cout << \"Delayed meeting started at \" << top.time << \"in room \" << top.room_id << std::endl;\n ++count[top.room_id];\n events.push({\n top.time + meeting.duration(),\n top.room_id,\n i\n });\n } else {\n std::cout << \"WWW\" << std::endl;\n }\n }\n\n int max_count = 0;\n int ans = 0;\n for (int i = 0; i < n; ++i) {\n if (count[i] > max_count) {\n max_count = count[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "104256"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct Meeting {\n int64_t start;\n int64_t end;\n\n inline int duration() const {\n return end - start;\n }\n\n inline bool operator<(const Meeting& other) const {\n return start > other.start;\n }\n };\n\n struct Event {\n int64_t time;\n int room_id;\n int meeting_id;\n\n inline bool operator<(const Event& other) const {\n return time > other.time || (time == other.time && room_id > other.room_id);\n }\n };\n\n int mostBooked(int n, vector<vector<int>>& in_meetings) {\n std::vector<int> count(n, 0);\n std::vector<Meeting> meetings;\n meetings.reserve(in_meetings.size());\n for (int i = 0; i < in_meetings.size(); ++i) {\n const auto& in_meeting = in_meetings[i];\n meetings.push_back({in_meeting[0], in_meeting[1]});\n }\n\n std::sort(meetings.begin(), meetings.end(), [](const auto& a, const auto& b) {\n return a.start < b.start;\n });\n\n std::priority_queue<Meeting> delayed_meetings;\n\n std::priority_queue<Event> events;\n\n // The ongoing meeting in each room.\n std::priority_queue<int, std::vector<int>, std::greater<int>> avail_rooms;\n for (int i = 0; i < n; ++i) {\n avail_rooms.push(i);\n }\n\n for (int i = 0; i < meetings.size(); ++i) {\n const auto& meeting = meetings[i];\n\n while (!events.empty()) {\n const auto top = events.top();\n if (top.time <= meeting.start) {\n events.pop();\n avail_rooms.push(top.room_id);\n } else {\n break;\n }\n }\n\n if (!avail_rooms.empty()) {\n const auto top = avail_rooms.top();\n // std::cout << \"Delayed meeting started at \" << meeting.start << \" in room \" << top << std::endl;\n avail_rooms.pop();\n ++count[top];\n events.push({\n meeting.start + meeting.duration(),\n top,\n i,\n });\n } else if (!events.empty()) {\n const auto top = events.top();\n events.pop();\n // std::cout << \"Delayed meeting started at \" << top.time << \"in room \" << top.room_id << std::endl;\n ++count[top.room_id];\n events.push({\n top.time + meeting.duration(),\n top.room_id,\n i\n });\n } else {\n std::cout << \"WWW\" << std::endl;\n }\n }\n\n int max_count = 0;\n int ans = 0;\n for (int i = 0; i < n; ++i) {\n if (count[i] > max_count) {\n max_count = count[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "104256"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& m) {\n set<int> freeRooms; \n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> endTimes;\n\n for(int i=0;i<n;i++)freeRooms.insert(i);\n \n sort(m.begin(), m.end());\n vector<int>count(n, 0);\n int curStartTime = 0;\n for(int i =0;i<m.size();i++){\n int start = m[i][0], end = m[i][1];\n while(endTimes.size() && endTimes.top().first <= start){\n int room = endTimes.top().second;\n endTimes.pop();\n freeRooms.insert(room);\n }\n if(freeRooms.size() == 0){\n long newEnd = endTimes.top().first + end - start;\n int room = endTimes.top().second;\n endTimes.pop();\n count[room]++;\n endTimes.push({newEnd , room});\n }else{\n int freeRoom = *freeRooms.begin();\n freeRooms.erase(freeRooms.begin());\n endTimes.push({end, freeRoom});\n count[freeRoom]++;\n }\n \n }\n int mx = * max_element(count.begin(), count.end()); \n for(int i = 0;i<n;i++)if(count[i] == mx)return i; \n return 0;\n }\n};",
"memory": "104918"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n // sort by start time\n sort(meetings.begin(), meetings.end());\n\n set<int> availables;\n for (int i = 0 ; i < n ; i++)\n availables.insert(i);\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> delayed; // start, end\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> not_availables; // time, room idx\n \n vector<int> count(n, 0);\n for (auto &m : meetings) {\n // scheduled delayed as soon as not_availables freed\n while (!delayed.empty()) {\n auto [s, e] = delayed.top(); delayed.pop();\n auto [t, idx] = not_availables.top(); not_availables.pop();\n not_availables.push({t + (e - s), idx});\n count[idx]++;\n }\n\n // update availables by time m[0]\n while (!not_availables.empty() && not_availables.top().first <= m[0]) {\n auto [t, idx] = not_availables.top(); not_availables.pop();\n availables.insert(idx);\n }\n\n // schedule m\n if (availables.size() > 0) {\n int idx = *(availables.begin());\n availables.erase(idx);\n not_availables.push({m[1], idx});\n count[idx]++;\n } else {\n delayed.push({m[0], m[1]});\n }\n }\n\n while (!delayed.empty()) {\n auto [s, e] = delayed.top(); delayed.pop();\n auto [t, idx] = not_availables.top(); not_availables.pop();\n not_availables.push({t + (e - s), idx});\n count[idx]++;\n }\n\n return max_element(count.begin(), count.end()) - count.begin();\n }\n};",
"memory": "105581"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& m) {\n set<pair<int,int>> st;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n st.insert({i, 0});\n }\n priority_queue<pair<long long,long long>, vector<pair<long long,long long>>, greater<pair<long long,long long>>>pq;\n sort(m.begin(), m.end());\n for(int i=0;i<m.size();i++){\n long long s = m[i][0], e = m[i][1], d = e - s;\n while(!pq.empty()){\n if(pq.top().first<=s){\n st.insert({pq.top().second, pq.top().first});\n pq.pop();\n }else{\n break;\n }\n }\n\n if(st.size()==0){\n auto it = pq.top();\n pq.pop();\n mp[it.second]+=1;\n pq.push({it.first+d, it.second});\n }else{\n pair<int,int> p1 = *st.begin();\n st.erase(p1);\n mp[p1.first]+=1;\n pq.push({e, p1.first});\n }\n }\n\n int mx = 0, ans = 0;\n for(auto it: mp){\n if(it.second > mx){\n mx = it.second;\n ans = it.first;\n }\n }\n return ans;\n }\n};",
"memory": "105581"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n // time,index of allocated room\n priority_queue<pair<long long, int>, vector<pair<long long, int>>,\n greater<pair<long long, int>>>\n q;\n q.push({meetings[0][1], 0});\n int room_ind = 0;\n map<int, int> mp;\n set<int> rooms;\n for (int i = 0; i < n; i++) {\n rooms.insert(i);\n }\n\n rooms.erase(0);\n mp[0]++;\n for (int i = 1; i < meetings.size(); i++) {\n if (!q.empty() && q.top().first <= meetings[i][0]) {\n int t = q.top().first;\n int room = q.top().second;\n while (!q.empty() && q.top().first <= meetings[i][0]) {\n if (room > q.top().second) {\n // rooms.erase(room);\n room = q.top().second;\n t = q.top().first;\n } \n rooms.insert(q.top().second);\n q.pop();\n }\n \n if (room > *(rooms.begin()))\n room = *(rooms.begin());\n\n int time = meetings[i][1]; \n q.push({time, room});\n rooms.erase(room);\n mp[room]++;\n } else {\n if (rooms.size() > 0) {\n room_ind = *(rooms.begin());\n q.push({meetings[i][1], room_ind});\n rooms.erase(room_ind);\n mp[room_ind]++;\n\n }\n else {\n long long time = q.top().first + meetings[i][1] - meetings[i][0];\n int room_ind = q.top().second;\n\n mp[room_ind]++;\n rooms.erase(room_ind);\n q.push({time, room_ind});\n q.pop();\n }\n }\n }\n int ans = 0;\n int max = mp[0];\n\n for (int i = 1; i < n; i++) {\n if (mp[i] > max) {\n ans = i;\n max = mp[i];\n }\n }\n return ans;\n }\n};",
"memory": "106243"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>&nums) \n {\n sort(nums.begin(),nums.end());\n set<int>s;\n for(int i=0;i<n;i++)\n {\n s.insert(i);\n }\n priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>pq;\n int mp[1001];\n memset(mp,0,sizeof(mp));\n for(auto &val:nums)\n {\n int start=val[0];\n int end=val[1];\n while(pq.size()&&pq.top().first<=start)\n {\n s.insert(pq.top().second);\n pq.pop();\n }\n if(s.size())\n {\n int empty_room=*s.begin();\n s.erase(s.begin());\n mp[empty_room]++;\n pq.push({end,empty_room});\n }\n else\n {\n auto temp=pq.top();\n pq.pop();\n int empty_room=temp.second;\n mp[empty_room]++;\n pq.push({1ll*temp.first+(end-start),empty_room});\n }\n }\n int max_index=0;\n for(int i=0;i<n;i++)\n {\n if(mp[i]>mp[max_index])\n {\n max_index=i;\n }\n }\n return max_index;\n }\n};",
"memory": "106243"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n int m = meetings.size();\n sort(meetings.begin(), meetings.end());\n vector<int> room(n, 0);\n priority_queue<pair<long,long>, vector<pair<long,long>>, greater<pair<long,long>>> pq;\n pq.push({meetings[0][1], 0});\n room[0]++;\n set<int> empty;\n for (int i = 1; i < n; i++) {\n empty.insert(i);\n }\n for (int i = 1; i < m; i++) {\n while(!pq.empty() && pq.top().first <= meetings[i][0]){\n empty.insert(pq.top().second);\n pq.pop();\n }\n if(empty.size()!=0){\n pq.push({meetings[i][1], *empty.begin()});\n room[*empty.begin()]++;\n empty.erase(empty.begin());\n }else{\n long duration = meetings[i][1] - meetings[i][0];\n long startTime = pq.top().first;\n int roomNo = pq.top().second;\n pq.pop();\n pq.push({startTime+duration, roomNo});\n room[roomNo]++;\n }\n }\n /*\n 0-2 1-2 2-2 3-3\n 0-49\n */\n int maxi = 0;\n for (int i = 1; i < n; i++) {\n if (room[maxi] < room[i]) {\n maxi = i;\n }\n }\n return maxi;\n }\n};",
"memory": "106906"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);\n int m = meetings.size();\n sort(meetings.begin(), meetings.end());\n vector<int> room(n, 0);\n priority_queue<pair<long,long>, vector<pair<long,long>>, greater<pair<long,long>>> pq;\n set<int> empty;\n for (int i = 0; i < n; i++) {\n empty.insert(i);\n }\n for (int i = 0; i < m; i++) {\n while(!pq.empty() && pq.top().first <= meetings[i][0]){\n empty.insert(pq.top().second);\n pq.pop();\n }\n if(empty.size()!=0){\n pq.push({meetings[i][1], *empty.begin()});\n room[*empty.begin()]++;\n empty.erase(empty.begin());\n }else{\n long duration = meetings[i][1] - meetings[i][0];\n long startTime = pq.top().first;\n int roomNo = pq.top().second;\n pq.pop();\n pq.push({startTime+duration, roomNo});\n room[roomNo]++;\n }\n }\n /*\n 0-2 1-2 2-2 3-3\n 0-49\n */\n int maxi = 0;\n for (int i = 1; i < n; i++) {\n if (room[maxi] < room[i]) {\n maxi = i;\n }\n }\n return maxi;\n }\n};",
"memory": "106906"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\n using ll = long long;\n using pp = pair<ll, int>;\n\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n set<int> free;\n priority_queue<pp, vector<pp>, greater<pp>> rooms;\n unordered_map<int, int> roomFreq;\n int m = meetings.size();\n for (int i = 0; i < n; i++) {\n free.insert(i);\n }\n int i = 0;\n ll cur_time;\n while (i < m) {\n cur_time = meetings[i][0];\n while (!rooms.empty()) {\n auto [a, b] = rooms.top();\n if (a <= cur_time) {\n rooms.pop();\n free.insert(b);\n } else\n break;\n }\n if (!free.empty()) {\n auto it = free.begin();\n if (it == free.end())\n continue;\n rooms.push({meetings[i][1], *it});\n roomFreq[*it]++;\n free.erase(*it);\n } else {\n auto [a, b] = rooms.top();\n ll diff = a - cur_time;\n rooms.pop();\n rooms.push({diff + meetings[i][1], b});\n roomFreq[b]++;\n }\n i++;\n }\n ll maxVal = -1;\n int ans = n;\n for (auto const a : roomFreq) {\n if (maxVal < a.second) {\n maxVal = a.second;\n ans = a.first;\n continue;\n } else if (maxVal == a.second) {\n ans = min(ans, a.first);\n }\n }\n return ans;\n }\n};",
"memory": "107568"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "using pii = pair<long long, int>;\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<int, vector<int>, greater<int>> idle;\n priority_queue<pii, vector<pii>, greater<pii>> busy;\n for (int i = 0; i < n; i++) idle.push(i);\n vector<int> cnt(n);\n sort(meetings.begin(), meetings.end());\n for (auto m : meetings) {\n int s = m[0], e = m[1];\n while (!busy.empty() && busy.top().first <= s) {\n idle.push(busy.top().second);\n busy.pop();\n }\n int i = 0;\n if (!idle.empty()) {\n i = idle.top();\n idle.pop();\n busy.push({e, i});\n }\n else {\n auto x = busy.top();\n busy.pop();\n i = x.second;\n busy.push({x.first + e - s, i});\n }\n cnt[i]++;\n }\n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (cnt[ans] < cnt[i]) ans = i;\n }\n return ans;\n }\n};",
"memory": "107568"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<long long>cnt(n+1,0),nxt(n+1,0);\n sort(meetings.begin(),meetings.end());\n for(auto x:meetings){\n long long min_time = 1e18;\n int ind=-1;\n for(int i=1;i<=n;i++){\n if(nxt[i]<=x[0]){\n ind = i;\n break;\n }\n if(nxt[i]>=x[0] and nxt[i]<min_time){\n min_time = nxt[i];\n ind = i;\n }\n }\n if(ind==-1){\n cout<<x[0]<<\" hi \"<<x[1]<<endl;\n }\n else{\n cnt[ind]++;\n nxt[ind]=max(x[0]*1ll,nxt[ind])+(x[1]-x[0]);\n }\n }\n long long maxi = 0;\n int ind = -1;\n for(int i=1;i<=n;i++){\n if(cnt[i]>maxi){\n maxi = cnt[i];\n ind = i;\n }\n }\n return ind-1;\n }\n};",
"memory": "108231"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n int m = meetings.size();\n sort(meetings.begin(), meetings.end());\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;\n for (int i = 0; i < n; i++) {\n pq.emplace(0, i);\n }\n vector<int> cnt(n);\n for (auto v : meetings) {\n int start = v[0], end = v[1];\n while (pq.top().first < start) {\n int x = pq.top().second;\n pq.pop();\n pq.emplace(start, x);\n }\n auto [time, idx] = pq.top();\n pq.pop();\n cnt[idx]++;\n if (time <= start) {\n pq.emplace(end, idx);\n } else {\n pq.emplace(time + end - start, idx);\n }\n }\n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (cnt[i] > cnt[ans]) {\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "108893"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n // brute force: traverse all start/end time(tic-tic)\n // maintain list of queue for each meeting room\n // each time, if a meeting begins, check list of queue\n // find lowest empty room\n // if none, find that with smallest end time and append to it with updated meeting time, (endtime, index) priority_queue\n // using priority_queue of size n, pop and push tasks O(logn) ~ O(1)\n // count each room usage when add 1 meeting\n // overall O(N)\n const int START = 0, END = 1;\n\n vector<long long> endTimeAt(n, 0);\n\n sort(meetings.begin(), meetings.end());\n\n vector<int> roomUsage(n, 0);\n\n for (vector<int> meeting : meetings) {\n // try schedule a new meeting at lowest available\n int i = 0;\n while (i < n && endTimeAt[i] > meeting[START]) i++;\n if (i < n) { // has valid\n endTimeAt[i] = meeting[END];\n roomUsage[i]++;\n } else { // delay\n long minimum = INT64_MAX;\n for (int j = 0; j < n; j++) {\n if (minimum > endTimeAt[j] || (minimum == endTimeAt[j] && j < i)) {\n minimum = endTimeAt[j];\n i = j;\n }\n }\n\n endTimeAt[i] += (meeting[END] - meeting[START]);\n roomUsage[i]++;\n }\n }\n\n int answerIndex = 0, answerCount = roomUsage[0];\n for (int i = 0; i < n; i++) {\n cout << i << \": \" << roomUsage[i] << endl;\n if (roomUsage[i] > answerCount) {\n answerIndex = i;\n answerCount = roomUsage[i];\n }\n }\n\n return answerIndex;\n }\n};",
"memory": "109556"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n // brute force: traverse all start/end time(tic-tic)\n // maintain list of queue for each meeting room\n // each time, if a meeting begins, check list of queue\n // find lowest empty room\n // if none, find that with smallest end time and append to it with updated meeting time, (endtime, index) priority_queue\n // using priority_queue of size n, pop and push tasks O(logn) ~ O(1)\n // count each room usage when add 1 meeting\n // overall O(N)\n const int START = 0, END = 1;\n\n vector<long> endTimeAt(n, 0);\n\n sort(meetings.begin(), meetings.end());\n\n vector<int> roomUsage(n, 0);\n\n for (vector<int> meeting : meetings) {\n // try schedule a new meeting at lowest available\n int i = 0;\n while (i < n && endTimeAt[i] > meeting[START]) i++;\n if (i < n) { // has valid\n endTimeAt[i] = meeting[END];\n roomUsage[i]++;\n } else { // delay\n long minimum = LONG_MAX;\n for (int j = 0; j < n; j++) {\n if (minimum > endTimeAt[j] || (minimum == endTimeAt[j] && j < i)) {\n minimum = endTimeAt[j];\n i = j;\n }\n }\n\n endTimeAt[i] += (meeting[END] - meeting[START]);\n roomUsage[i]++;\n }\n }\n\n int answerIndex = 0, answerCount = roomUsage[0];\n for (int i = 0; i < n; i++) {\n cout << i << \": \" << roomUsage[i] << endl;\n if (roomUsage[i] > answerCount) {\n answerIndex = i;\n answerCount = roomUsage[i];\n }\n }\n\n return answerIndex;\n }\n};",
"memory": "109556"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<int> meetingCount(n, 0);\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> usedRooms;\n priority_queue<int, vector<int>, greater<int>> unusedRooms;\n for (int i = 0; i < n; i++) {\n unusedRooms.push(i);\n }\n sort(meetings.begin(), meetings.end());\n\n for (auto meeting : meetings) {\n int start = meeting[0], end = meeting[1];\n\n while (!usedRooms.empty() && usedRooms.top().first <= start) {\n int room = usedRooms.top().second;\n usedRooms.pop();\n unusedRooms.push(room);\n }\n if (!unusedRooms.empty()) {\n int room = unusedRooms.top();\n unusedRooms.pop();\n usedRooms.push({end, room});\n meetingCount[room]++;\n } else {\n auto [roomAvailabilityTime, room] = usedRooms.top();\n usedRooms.pop();\n usedRooms.push({roomAvailabilityTime + end - start, room});\n meetingCount[room]++;\n }\n }\n\n int maxMeetingCount = 0, maxMeetingCountRoom = 0;\n for (int i = 0; i < n; i++) {\n if (meetingCount[i] > maxMeetingCount) {\n maxMeetingCount = meetingCount[i];\n maxMeetingCountRoom = i;\n }\n }\n return maxMeetingCountRoom;\n }\n};\n\n/*\n\nunused_rooms - pq(i)\n\nused_rooms - pq({end_time, i})\n\n*/",
"memory": "110218"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n priority_queue<int, vector<int>, greater<int>> avlRoom;\n for(int i=0;i<n;i++){\n avlRoom.push(i);\n }\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>>pq;\n vector<int>meetingcnt(n,0);\n for( auto it:meetings){\n int start=it[0];\n int end=it[1];\n while(!pq.empty() && pq.top().first<=start){\n avlRoom.push(pq.top().second);\n pq.pop();\n }\n if(!avlRoom.empty()){\n int r=avlRoom.top();\n avlRoom.pop();\n meetingcnt[r]++;\n pq.push({end,r});\n }\n else{\n auto it=pq.top();\n pq.pop();\n int r=it.second;\n long long newEnd=it.first+ (end-start);\n meetingcnt[r]++;\n pq.push({newEnd,r});\n }\n \n }\n int maxi=0,ans=0;\n for(int i=0;i<n;i++){\n if(meetingcnt[i]>maxi){\n maxi=meetingcnt[i];\n ans=i;\n }\n }\n return ans;\n }\n};",
"memory": "110881"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 2 | {
"code": "bool compare(vector<int>& v1, vector<int>& v2) {\n return v1[0] < v2[0];\n}\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end(), compare);\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> engaged;\n priority_queue<int, vector<int>, greater<int>> unused;\n unordered_map<int, int> freq;\n for(int i = 0; i < n; i++) {\n unused.push(i);\n }\n for(auto x:meetings) {\n int s = x[0], e = x[1];\n while(engaged.size() > 0 && engaged.top().first <= s) {\n int room = engaged.top().second;\n engaged.pop();\n unused.push(room);\n }\n if(unused.size() > 0) \n {\n int room = unused.top();\n unused.pop();\n freq[abs(room)] += 1;\n engaged.push({e, room});\n }\n else \n {\n pair<long long, int> topmost = engaged.top();\n engaged.pop();\n freq[abs(topmost.second)] += 1;\n long long newEnd = topmost.first;\n newEnd += (e - s);\n engaged.push({newEnd, topmost.second});\n }\n }\n int maxi = 0;\n for(int i = 1; i < n; i++) {\n if(freq[i] > freq[maxi])\n maxi = i;\n }\n return maxi;\n }\n};",
"memory": "110881"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Info {\n long long start, end;\n int room;\n Info(long long start, long long end, int room) : start(start), end(end), room(room) {}\n bool const operator<(const Info& ob) const {\n if(end == ob.end)return room > ob.room;\n return end > ob.end;\n }\n };\n\n priority_queue<Info> usedRooms;\n priority_queue<int, vector<int>, greater<int>> unusedRooms;\n\n bool static compare(vector<int>& a, vector<int>& b) {\n if(a[0] == b[0])return a[1] < b[1];\n return a[0] < b[0];\n }\n\n void fillUnusedRooms(int n) {\n for(int i = 0; i < n; i++) {\n unusedRooms.push(i);\n }\n }\n\n int findMaxFrequencyRoom(map<int, int>& freq) {\n int room = 0, f = 0;\n for(auto it: freq) {\n if(it.second > f) {\n f = it.second;\n room = it.first;\n }\n }\n return room;\n }\n\n bool overlapping(long long a_end, long long b_start) {\n if(b_start < a_end)return true;\n return false;\n }\n\n void freeRooms(vector<int>& requestedMeeting) {\n while(!usedRooms.empty()) {\n Info cur = usedRooms.top();\n if(!overlapping(cur.end, requestedMeeting[0])) {\n usedRooms.pop();\n unusedRooms.push(cur.room);\n } else {\n break;\n }\n }\n }\n\n int findAvailableRoom(vector<int>& requestedMeeting) {\n freeRooms(requestedMeeting);\n int assignedRoom;\n if(unusedRooms.size() > 0) {\n assignedRoom = unusedRooms.top();\n usedRooms.push(Info(requestedMeeting[0], requestedMeeting[1], assignedRoom));\n unusedRooms.pop();\n return assignedRoom;\n }\n auto cur = usedRooms.top();\n assignedRoom = cur.room;\n int duration = requestedMeeting[1] - requestedMeeting[0];\n long long newStart = cur.end;\n long long newEnd = cur.end + duration;\n usedRooms.pop();\n usedRooms.push(Info(newStart, newEnd, assignedRoom));\n return assignedRoom;\n }\n\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end(), compare);\n fillUnusedRooms(n); \n map<int, int> freq;\n for(auto meeting: meetings) {\n int room = findAvailableRoom(meeting);\n freq[room]++;\n } \n int maxFreqRoom = findMaxFrequencyRoom(freq);\n return maxFreqRoom; \n }\n};\n\n// 2:48\n\n",
"memory": "111543"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(NULL);\n sort(meetings.begin(),meetings.end());\n vector<long long>roomcnt(n,0);\n priority_queue<long long,vector<long long>,greater<long long>>avail;\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>> used;// {end,room}\n for(int i=0;i<n;i++){\n avail.push(i);\n }\n for(auto it: meetings){\n long long start=it[0];\n long long end=it[1];\n long long dur=end-start;\n while(!used.empty() && used.top().first<=start){\n long long room=used.top().second;\n used.pop();\n avail.push(room);\n }\n if(!avail.empty()){\n long long room=avail.top();\n avail.pop();\n used.push({end,room});\n roomcnt[room]++;\n }\n else{//main case when not availale\n long long room=used.top().second;\n long long prev_end=used.top().first;\n used.pop();\n used.push({prev_end+dur,room});\n roomcnt[room]++;\n }\n }\n long long ans=-1;\n long long maxuse=0;\n //sort(roomcnt.begin(),roomcnt.end(),greater<int>()); wrong idea\n for(int room=0;room<n;room++){\n if(roomcnt[room]>maxuse){\n maxuse=roomcnt[room];\n ans=room;\n }\n }\n return ans;\n }\n};",
"memory": "112206"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n priority_queue<pair<uint64_t,int>,vector<pair<uint64_t,int>>,greater<pair<uint64_t,int>>> pq;\n pair<uint64_t,int> p;\n priority_queue<int> s;\n uint64_t d;\n int ans = 0, room;\n vector<int> v(n);\n for(int i = 0; i <n;i++){\n s.push(-i);\n }\n for(auto m:meetings){\n d = 0;\n while(!pq.empty() and m[0]>=pq.top().first){\n p = pq.top();\n pq.pop();\n s.push(-p.second);\n }\n if(pq.size()==n){\n p = pq.top();\n pq.pop();\n d=max((uint64_t)0,p.first-m[0]);\n s.push(-p.second);\n }\n room = -s.top();\n pq.push({m[1]+d,room});\n v[room]++;\n if(v[room]>v[ans]){\n ans=room;\n }else if(v[room]==v[ans]){\n ans = min(room,ans);\n }\n s.pop();\n }\n return ans;\n }\n};",
"memory": "112206"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define F first\n#define S second\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<long long> v(n,0);\n sort(meetings.begin(),meetings.end());\n for (auto it : meetings){\n cout<<it[0]<<\" \"<<it[1]<<\"\\n\";\n }\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>> pq;\n for (long long i=0;i<n;i++){\n pq.push({0,i});\n }\n long long m = meetings.size();\n long long id = 0;\n while (id<m){\n long long f = 0;\n vector<long long> temp;\n while (!pq.empty() && pq.top().F<meetings[id][0]){\n auto it = pq.top();\n temp.push_back(it.S);\n pq.pop();\n }\n for (auto it : temp){\n pq.push({meetings[id][0],it});\n }\n while (!pq.empty() && id<m && pq.top().F>=meetings[id][0]){\n auto it = pq.top();\n long long a = it.F;\n long long b = it.S;\n pq.pop();\n v[b]++;\n a += meetings[id][1] - meetings[id][0];\n pq.push({a,b});\n id++;\n f = 1;\n }\n }\n long long ans = 0, maxi = 0;\n for (long long i=0;i<n;i++){\n if (maxi<v[i]){\n maxi = v[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "112868"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "\tstruct Meeting {\n\t\tint64_t start = 0;\n\t\tint64_t end = 0;\n\t\tint room = 0;\n\t};\n\n\tstruct Room {\n\t\tint id = 0;\n\t\tMeeting* meeting = nullptr;\n\t\tint64_t delay = 0;\n\t\tint uses = 0;\n\t};\n\n\tclass Solution {\n\tpublic:\n\t\tint mostBooked(int roomCount, vector<vector<int>>& intervals) {\n\t\t\t// Sort ascending by start time\n\t\t\tstd::ranges::sort(intervals, [](auto& a, auto& b) {\n\t\t\t\treturn a[0] < b[0];\n\t\t\t\t});\n\n\t\t\t// Enqueue meetings\n\t\t\tstd::vector<Meeting> meetings;\n\t\t\tstd::ranges::transform(intervals, std::back_inserter(meetings), [](auto& interval) {\n\t\t\t\treturn Meeting{ interval[0], interval[1], -1 };\n\t\t\t\t});\n\n\t\t\t// Create rooms\n\t\t\tauto rooms = vector<Room>();\n\t\t\tfor (int i = 0; i < roomCount; i++) {\n\t\t\t\trooms.push_back({ i, nullptr, 0, 0 });\n\t\t\t}\n\n\t\t\tfor (auto& meeting : meetings) {\n\t\t\t\tmeeting.room = assignRoom(rooms, meeting).id;\n\t\t\t}\n\n\t\t\t// Find most used room\n\t\t\tauto mostUsed = std::ranges::max_element(rooms, [](auto& a, auto& b) {\n\t\t\t\treturn a.uses < b.uses;\n\t\t\t\t});\n\t\t\treturn mostUsed->id;\n\t\t}\n\n\t\tRoom& assignRoom(vector<Room>& rooms, Meeting& meeting) {\n\t\t\tint64_t closestEnd = std::numeric_limits<int64_t>::max();\n\t\t\tRoom* closestEndRoom = nullptr;\n\n\t\t\tfor (size_t i = 0; i < rooms.size(); i++) {\n\t\t\t\tif (rooms[i].meeting == nullptr || rooms[i].meeting->end <= meeting.start) {\n\t\t\t\t\trooms[i].meeting = &meeting;\n\t\t\t\t\trooms[i].uses++;\n\t\t\t\t\trooms[i].delay = 0;\n\t\t\t\t\treturn rooms[i];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (closestEnd > rooms[i].meeting->end) {\n\t\t\t\t\t\tclosestEndRoom = &rooms[i];\n\t\t\t\t\t\tclosestEnd = rooms[i].meeting->end;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tclosestEndRoom->meeting = &meeting;\n\t\t\tclosestEndRoom->uses++;\n\t\t\tclosestEndRoom->delay = closestEnd - meeting.start;\n\t\t\tmeeting.start += closestEndRoom->delay;\n\t\t\tmeeting.end += closestEndRoom->delay;\n\t\t\treturn *closestEndRoom;\n\t\t}\n\t};",
"memory": "113531"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "\tstruct Meeting {\n\t\tint64_t start = 0;\n\t\tint64_t end = 0;\n\t\tint room = 0;\n\t};\n\n\tstruct Room {\n\t\tint id = 0;\n\t\tMeeting* meeting = nullptr;\n\t\tint64_t delay = 0;\n\t\tint uses = 0;\n\t};\n\n\tclass Solution {\n\tpublic:\n\t\tint mostBooked(int roomCount, vector<vector<int>>& intervals) {\n\t\t\t// Sort ascending by start time\n\t\t\tstd::ranges::sort(intervals, [](auto& a, auto& b) {\n\t\t\t\treturn a[0] < b[0];\n\t\t\t\t});\n\n\t\t\t// Enqueue meetings\n\t\t\tstd::vector<Meeting> meetings;\n\t\t\tstd::ranges::transform(intervals, std::back_inserter(meetings), [](auto& interval) {\n\t\t\t\treturn Meeting{ interval[0], interval[1], -1 };\n\t\t\t\t});\n\n\t\t\t// Create rooms\n\t\t\tauto rooms = vector<Room>();\n\t\t\tfor (int i = 0; i < roomCount; i++) {\n\t\t\t\trooms.push_back({ i, nullptr, 0, 0 });\n\t\t\t}\n\n\t\t\tfor (auto& meeting : meetings) {\n\t\t\t\tmeeting.room = assignRoom(rooms, meeting).id;\n\t\t\t}\n\n\t\t\t// Find most used room\n\t\t\tauto mostUsed = std::ranges::max_element(rooms, [](auto& a, auto& b) {\n\t\t\t\treturn a.uses < b.uses;\n\t\t\t\t});\n\t\t\treturn mostUsed->id;\n\t\t}\n\n\t\tRoom& assignRoom(vector<Room>& rooms, Meeting& meeting) {\n\t\t\tint64_t closestEnd = std::numeric_limits<int64_t>::max();\n\t\t\tRoom* closestEndRoom = nullptr;\n\n\t\t\tfor (size_t i = 0; i < rooms.size(); i++) {\n\t\t\t\tif (rooms[i].meeting == nullptr || rooms[i].meeting->end <= meeting.start) {\n\t\t\t\t\trooms[i].meeting = &meeting;\n\t\t\t\t\trooms[i].uses++;\n\t\t\t\t\trooms[i].delay = 0;\n\t\t\t\t\treturn rooms[i];\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tif (closestEnd > rooms[i].meeting->end) {\n\t\t\t\t\t\tclosestEndRoom = &rooms[i];\n\t\t\t\t\t\tclosestEnd = rooms[i].meeting->end;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tclosestEndRoom->meeting = &meeting;\n\t\t\tclosestEndRoom->uses++;\n\t\t\tclosestEndRoom->delay = closestEnd - meeting.start;\n\t\t\tmeeting.start += closestEndRoom->delay;\n\t\t\tmeeting.end += closestEndRoom->delay;\n\t\t\treturn *closestEndRoom;\n\t\t}\n\t};",
"memory": "113531"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define pii pair<long long,int>\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n set<int> st;\n vector<int> mp(n, 0);\n\n for (int i = 0; i < n; ++i) {\n st.insert(i);\n }\n\n for (auto it : meetings) {\n int start = it[0], end = it[1];\n while (!pq.empty() && start >= pq.top().first) {\n st.insert(pq.top().second);\n pq.pop();\n }\n\n if (st.empty()) {\n auto [val, room] = pq.top();\n pq.pop();\n mp[room]++;\n pq.push({val + end - start, room});\n } else {\n auto room = *st.begin();\n st.erase(st.begin());\n mp[room]++;\n pq.push({end, room});\n }\n }\n\n int ans = 0, max_meetings = 0;\n for (int i = 0; i < n; ++i) {\n if (mp[i] > max_meetings) {\n max_meetings = mp[i];\n ans = i;\n }\n }\n return ans;\n }\n};\n",
"memory": "116843"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n\n vector<int> cnt(n, 0);\n\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;\n for (int i = 0; i < n; ++i)\n pq.push({0, i});\n\n set<pair<int, long long>> emp;\n for (auto m: meetings) {\n while (!pq.empty() && pq.top().first <= m[0]) {\n emp.insert({pq.top().second, pq.top().first});\n pq.pop();\n }\n \n if (!emp.empty()) {\n auto p = *emp.begin();\n emp.erase(emp.begin());\n\n if (p.second <= m[0]) {\n pq.push({m[1], p.first});\n } else {\n pq.push({p.second + m[1] - m[0], p.first});\n }\n\n ++cnt[p.first];\n } else {\n auto p = pq.top();\n pq.pop();\n\n pq.push({p.first + m[1] - m[0], p.second});\n\n ++cnt[p.second];\n }\n }\n\n int ans = 0;\n for (int i = 0; i < n; ++i)\n if (cnt[i] > cnt[ans])\n ans = i;\n\n return ans;\n }\n};",
"memory": "117506"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<int>cnt(n);\n set<int>s;\n for(int i = 0; i<n; i++){\n s.insert(i);\n }\n sort(meetings.begin(),meetings.end());\n priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>pq;\n for(auto vec: meetings){\n int l = vec[0];\n int r = vec[1];\n while(pq.size() && pq.top().first <= l){\n auto [_, i] = pq.top(); pq.pop();\n s.insert(i);\n }\n if(s.size() == 0){\n auto [t, i] = pq.top(); pq.pop();\n cnt[i]++;\n pq.push({r+t-l,i});\n }\n else{\n int i = *s.begin(); s.erase(i);\n cnt[i]++;\n pq.push({r,i});\n }\n }\n int mx = 0;\n int ans = 0;\n for(int i = 0; i<n; i++){\n if(cnt[i] > mx){\n mx = cnt[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "118168"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#include <queue>\n\nstruct MeetingRoom {\n long int room_num;\n long int available_time;\n};\n\nstruct Meeting {\n long int start_time;\n long int end_time;\n};\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meeting_vects) {\n const auto compare_rooms = [&n](const MeetingRoom& a, const MeetingRoom& b) {\n return a.available_time > b.available_time;\n };\n const auto compare_available_rooms = [](const MeetingRoom& a, const MeetingRoom& b) {\n return a.room_num > b.room_num;\n };\n // Keeps track of the time when room will be free.\n std::priority_queue<MeetingRoom, std::deque<MeetingRoom>, decltype(compare_rooms)>\n room_availability(compare_rooms);\n // Because the priority queue doesn't know when it now, it can't prioritize\n // between all available by room number, so we will put these in a separate queue\n // that prioritizes by room number.\n std::priority_queue<MeetingRoom, std::deque<MeetingRoom>,\n decltype(compare_available_rooms)> \n available_now(compare_available_rooms);\n // Counts the number of meetings assigned to a meeting room (for final output).\n std::unordered_map<int, int> meeting_count;\n // We start with all rooms available and zero counts.\n for (int i = 0; i < n; ++i) {\n available_now.push(MeetingRoom{i, 0});\n meeting_count[i] = 0;\n }\n // Reverse sort the meetings by start time so we can pop from the end. This order\n // never changes, as the original start time determines order, and we know there are\n // no ties, as original start times are unique.\n const auto compare_meetings = [](const Meeting& m1, \n const Meeting& m2) {\n return m1.start_time > m2.start_time;\n };\n std::vector<Meeting> meetings;\n meetings.reserve(meeting_vects.size());\n for (const auto meeting_vect : meeting_vects) {\n meetings.push_back(Meeting({meeting_vect[0], meeting_vect[1]}));\n }\n sort(meetings.begin(), meetings.end(), compare_meetings);\n printf(\"number of meetings: %d\\n\", meetings.size());\n \n long int time = 0;\n int countdown = 100000;//meetings.size();\n while (!meetings.empty()) {\n //printf(\"num meetings: %d, time %d\\n\", meetings.size(), time);\n Meeting& next_meeting = meetings[meetings.size()-1];\n //printf(\"next meeting: [%d, %d)\\n\", next_meeting.start_time, next_meeting.end_time);\n // We can't do anything useful until there is a meeting room free.\n if (available_now.empty() && \n room_availability.top().available_time > time) {\n time = room_availability.top().available_time;\n }\n // If the next meeting doesn't start yet, advance the time to the next meeting\n // start time.\n if (next_meeting.start_time > time) {\n time = next_meeting.start_time;\n }\n // If the next meeting was supposed to start before now, we need to delay it so\n // we know when it will actually end.\n else if (next_meeting.start_time < time) {\n //printf(\"delaying [%d, %d) until [%d, %d)\\n\", next_meeting.start_time, next_meeting.end_time, time, next_meeting.end_time + (time - next_meeting.start_time));\n next_meeting.end_time += (time - next_meeting.start_time); \n next_meeting.start_time = time;\n }\n // First shift any newly available rooms to the available_now queue.\n while (!room_availability.empty() && \n room_availability.top().available_time <= time) {\n const auto available_room = room_availability.top();\n room_availability.pop();\n //printf(\"room %d available now.\\n\", available_room.room_num);\n available_now.push(available_room);\n }\n // If there are rooms available now, get the lowest-numbered one.\n if (!available_now.empty()) {\n auto room = available_now.top();\n available_now.pop();\n // Now the room will be available at the end of this meeting\n //printf(\"assigning meeting [%d, %d) to room %d\\n\", next_meeting.start_time, next_meeting.end_time, room.room_num);\n room.available_time = next_meeting.end_time;\n room_availability.push(room);\n meeting_count[room.room_num]++;\n meetings.pop_back();\n }\n else {\n // Delay the meeting until the next available time.\n const int soonest_available_time = room_availability.top().available_time;\n //printf(\"delaying by %d\\n\", (soonest_available_time - next_meeting.start_time));\n next_meeting.end_time += (soonest_available_time - next_meeting.start_time);\n next_meeting.start_time = soonest_available_time;\n }\n countdown--;\n //if (countdown == 0) break;\n }\n printf(\"num meetings at end: %d\\n\", meetings.size());\n // Now find the room with the largest count. \n int max_room_num = -1, max_count = -1;\n for (int room_num = 0; room_num < n; ++room_num) {\n if (meeting_count[room_num] > max_count) {\n max_count = meeting_count[room_num];\n max_room_num = room_num;\n }\n }\n return max_room_num;\n }\n};",
"memory": "118831"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Compare {\n bool operator()(const vector<long long>& a, const vector<long long>& b) {\n if (a[0] == b[0]) return a[1] > b[1];\n return a[0] > b[0];\n }\n };\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end(), [](vector<int> &a,vector<int> &b){\n return a[0]<b[0];\n });\n priority_queue<vector<long long>, vector<vector<long long>>, Compare> minheap;\n vector<int> room(n,0);//记录每个房间的使用次数\n priority_queue<int,vector<int>,greater<int>> freeRooms;\n for(int i=0;i<n;i++){\n freeRooms.push(i);\n }\n \n for(const auto& meeting:meetings){\n int start=meeting[0];\n int end=meeting[1];\n while(!minheap.empty()&&start>=minheap.top()[0]){\n int empty=minheap.top()[1];\n minheap.pop();\n freeRooms.push(empty);\n }\n if(freeRooms.empty()){\n auto earliest=minheap.top();\n minheap.pop();\n int roomindex=earliest[1];\n // long long wait=minheap.top()[0]-meetings[i][0];\n long long newEnd = earliest[0] + (end-start);\n //int occupy=minheap.top()[1];\n minheap.push({newEnd,roomindex});\n room[roomindex]+=1;\n }\n else {\n int j=freeRooms.top();\n freeRooms.pop();\n minheap.push({end, j});\n room[j]+=1;\n \n }\n }\n int max_value=INT_MIN;\n int in=0;\n for(int i=0;i<n;i++){\n if(room[i]>max_value){\n max_value=room[i];\n in=i;\n }\n \n }\n return in;\n }\n};",
"memory": "118831"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n auto compare = [](vector<long long int>& a, vector<long long int>& b) {\n return a[0] > b[0];\n };\n priority_queue <vector<long long int>, vector<vector<long long int>>, greater<vector<long long int>>> rooms_end_times; \n \n vector<long long int> rooms_used(n);\n \n priority_queue <long long int, vector<long long int>, greater<long long int>> rooms_avail; \n for(long long int i=0;i<n;i++)\n rooms_avail.push(i);\n \n sort(meetings.begin(), meetings.end());\n \n long long int time = 0;\n vector<long long int> answer = {0,0};\n for(int i=0;i<meetings.size();i++)\n {\n int start = meetings[i][0];\n int end = meetings[i][1];\n int duration = end - start;\n \n while(!rooms_end_times.empty() && rooms_end_times.top()[0]<=start) {\n rooms_avail.push(rooms_end_times.top()[1]);\n rooms_end_times.pop();\n }\n if(!rooms_avail.empty())\n {\n int room = rooms_avail.top();\n rooms_avail.pop();\n rooms_end_times.push({end, room});\n rooms_used[room]++;\n }\n else {// delaying\n vector<long long int> meeting_info = rooms_end_times.top();\n rooms_end_times.pop();\n \n rooms_end_times.push({meeting_info[0]+duration, meeting_info[1]});\n rooms_used[meeting_info[1]]++;\n // rooms_avail.push(meeting_info[1]);\n }\n // int room = rooms_avail.top();\n // rooms_used[room]++;\n // rooms_end_times.push({end, room});\n // rooms_avail.pop();\n }\n for(int i=0;i<n;i++)\n {\n cout<<rooms_used[i]<<\" \"<<i<<endl;\n if(rooms_used[i]>answer[1])\n answer = {i, rooms_used[i]};\n }\n return answer[0];\n\n }\n};",
"memory": "119493"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n priority_queue<pair<long, long>, vector<pair<long, long>>, greater<pair<long, long>>> events, pending;\n for (long i = 0; i < meetings.size(); i++) {\n events.push({meetings[i][0], i});\n // pq.push({m[1], 0});\n }\n vector<long> used(n, 0);\n long current_time = 0;\n priority_queue<long, vector<long>, greater<long>> rooms;\n for (long i = 0; i < n; i++) rooms.push(i);\n long max_used = 0;\n while (events.size()) {\n long t = events.top().first;\n long e = events.top().second;\n // std::cout << t << \" \" << e << endl;\n events.pop();\n if (e < 0) {\n long room = e + n;\n rooms.push(room);\n if (pending.size()) {\n long pt = pending.top().first;\n long pe = pending.top().second;\n pending.pop();\n long room = rooms.top();\n rooms.pop();\n used[room]++;\n max_used = max(max_used, used[room]);\n long event_time = meetings[pe][1]-meetings[pe][0];\n events.push({t+event_time, room-n});\n }\n }\n else {\n if (rooms.empty()) {\n pending.push({t, e});\n continue;\n } \n long room = rooms.top();\n rooms.pop();\n used[room]++;\n max_used = max(max_used, used[room]);\n long event_time = meetings[e][1]-meetings[e][0];\n events.push({t+event_time, room-n});\n }\n }\n for (int i = 0; i < n; i++) {\n if (used[i] == max_used) return i;\n }\n return -1;\n }\n};",
"memory": "119493"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n int m_sz = meetings.size();\n sort(meetings.begin(), meetings.end());\n \n set<pair<long long, int>> st; // < ending time, room no. >\n vector<int> v(n, 0); // freq of each room meetings\n\n int i = 1, r = 1; // r for empty room pointer\n st.insert( { meetings[0][1], 0 } ); // inserting first meeting in first room\n v[0] = 1;\n\n while(i < m_sz) {\n int s = meetings[i][0], e = meetings[i][1]; // incoming meeting\n \n auto it = *st.begin(); // get the lowest ending time room\n long long curr_e = it.first;\n int room = it.second;\n\n // start time of incoming meeting is greater than ending time of one or more of the meetings\n // find that meeting with lowest room number\n if(s >= curr_e) { \n auto curr = st.begin();\n auto lo = st.begin();\n\n while(curr != st.end()) {\n if(curr -> first <= s && (curr -> second) < (lo -> second) ) {\n lo = curr;\n }\n ++curr;\n }\n\n int r1 = lo -> second; // lowest room no.\n st.erase(lo);\n st.insert( { e, r1 } );\n v[r1]++;\n }\n \n else if(st.size() < n) { // we have an empty room, put meeting in that room\n st.insert( { e, r } ); // r will be pointing to the current empty room\n v[r]++;\n r++;\n }\n\n// no empty room, select that room which has lowest ending time, \n// and if more than one room with that ending time, select the one with the lowest room no.\n else { \n st.erase(st.begin());\n st.insert( { curr_e + (e - s) , room } );\n v[room]++;\n } \n\n i++;\n }\n\n // find max value among all rooms\n int res = 0, mx = 0;\n for(int i = 0; i < n; ++i) {\n cout << v[i] << \" \";\n if(v[i] > mx) {\n mx = v[i];\n res = i;\n }\n }\n\n return res;\n }\n};",
"memory": "120156"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n int m_sz = meetings.size();\n sort(meetings.begin(), meetings.end());\n \n set<pair<long long, int>> st; // < ending time, room no. >\n vector<int> v(n, 0); // freq of each room meetings\n\n int i = 1, r = 1; // r for empty room pointer\n st.insert( { meetings[0][1], 0 } ); // inserting first meeting in first room\n v[0] = 1;\n\n while(i < m_sz) {\n int s = meetings[i][0], e = meetings[i][1]; // incoming meeting\n \n auto it = *st.begin(); // get the lowest ending time room\n long long curr_e = it.first;\n int room = it.second;\n\n // start time of incoming meeting is greater than ending time of one or more of the meetings\n // find that meeting with lowest room number\n if(s >= curr_e) { \n auto curr = st.begin();\n auto lo = st.begin();\n\n while(curr != st.end()) {\n if(curr -> first <= s && (curr -> second) < (lo -> second) ) {\n lo = curr;\n }\n ++curr;\n }\n\n int r1 = lo -> second; // lowest room no.\n st.erase(lo);\n st.insert( { e, r1 } );\n v[r1]++;\n }\n \n else if(st.size() < n) { // we have an empty room, put meeting in that room\n st.insert( { e, r } ); // r will be pointing to the current empty room\n v[r]++;\n r++;\n }\n\n// no empty room, select that room which has lowest ending time, \n// and if more than one room with that ending time, select the one with the lowest room no.\n else { \n st.erase(st.begin());\n st.insert( { curr_e + (e - s) , room } );\n v[room]++;\n } \n\n i++;\n }\n\n // find max value among all rooms\n int res = 0, mx = 0;\n for(int i = 0; i < n; ++i) {\n cout << v[i] << \" \";\n if(v[i] > mx) {\n mx = v[i];\n res = i;\n }\n }\n\n return res;\n }\n};",
"memory": "120818"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n int m_sz = meetings.size();\n sort(meetings.begin(), meetings.end());\n \n set<pair<long long, int>> st; // < ending time, room no. >\n vector<int> v(n, 0); // freq of each room meetings\n\n int i = 1, r = 1; // r for empty room pointer\n st.insert( { meetings[0][1], 0 } ); // inserting first meeting in first room\n v[0] = 1;\n\n while(i < m_sz) {\n int s = meetings[i][0], e = meetings[i][1]; // incoming meeting\n \n auto it = *st.begin(); // get the lowest ending time room\n long long curr_e = it.first;\n int room = it.second;\n\n // start time of incoming meeting is greater than ending time of one or more of the meetings\n // find that meeting with lowest room number\n if(s >= curr_e) { \n auto curr = st.begin();\n auto lo = st.begin();\n\n while(curr != st.end()) {\n if(curr -> first <= s && (curr -> second) < (lo -> second) ) {\n lo = curr;\n }\n ++curr;\n }\n\n int r1 = lo -> second; // lowest room no.\n st.erase(lo);\n st.insert( { e, r1 } );\n v[r1]++;\n }\n \n else if(st.size() < n) { // we have an empty room, put meeting in that room\n st.insert( { e, r } ); // r will be pointing to the current empty room\n v[r]++;\n r++;\n }\n\n// no empty room, select that room which has lowest ending time, \n// and if more than one room with that ending time, select the one with the lowest room no.\n else { \n st.erase(st.begin());\n st.insert( { curr_e + (e - s) , room } );\n v[room]++;\n } \n\n i++;\n }\n\n // find max value among all rooms\n int res = 0, mx = 0;\n for(int i = 0; i < n; ++i) {\n cout << v[i] << \" \";\n if(v[i] > mx) {\n mx = v[i];\n res = i;\n }\n }\n\n return res;\n }\n};",
"memory": "120818"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n \n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<int> rooms(n);\n \n sort(meetings.begin(), meetings.end());\n \n priority_queue<vector<ll>, vector<vector<ll>>, greater<>> using_room;\n priority_queue<ll, vector<ll>, greater<>> empty_room;\n for (int i = 0; i < n; i += 1) {\n empty_room.push(i);\n }\n\n ll cur_time = 0;\n for (vector<int> meeting: meetings) {\n \n ll start = meeting[0], end = meeting[1];\n cur_time = max(cur_time, start);\n if (empty_room.empty() && using_room.top()[0] >= cur_time) {\n cur_time = max(cur_time, using_room.top()[0]);\n }\n\n while (!using_room.empty() && cur_time >= using_room.top()[0]) {\n empty_room.push(using_room.top()[1]);\n using_room.pop();\n }\n // cout << empty_room.top() << \" \" << cur_time << endl;\n rooms[empty_room.top()] += 1;\n using_room.push({cur_time + end - start, empty_room.top()});\n empty_room.pop();\n }\n int use_time = 0, ind = 0;\n\n for (int i = 0; i < n; i += 1) {\n if (use_time < rooms[i]) {\n use_time = rooms[i];\n ind = i;\n }\n }\n\n return ind;\n }\n};",
"memory": "121481"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(long long n, vector<vector<int>>& meetings) {\n vector<bool> flag(n, false);\n vector<long long> counter(n, 0);\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> ending_pq;\n priority_queue<long long, vector<long long>, greater<long long>> empty_rooms;\n long long current_time = 0;\n for(long long i = 0; i < n; i++) {\n empty_rooms.push(i);\n }\n vector<pair<long long, long long>> meeting_numbers;\n for(auto meeting : meetings) {\n meeting_numbers.push_back(make_pair(meeting[0], meeting[1]));\n }\n sort(meeting_numbers.begin(), meeting_numbers.end());\n for(auto meeting : meeting_numbers) {\n long long start = meeting.first, end = meeting.second;\n current_time = start;\n while(!ending_pq.empty()) {\n if (ending_pq.top().first > current_time) {\n break;\n }\n empty_rooms.push(ending_pq.top().second);\n ending_pq.pop();\n }\n long long current_room = -1;\n if (!empty_rooms.empty()) {\n current_room= empty_rooms.top();\n empty_rooms.pop();\n current_time = max(current_time, start);\n } else {\n auto dummy = ending_pq.top();\n ending_pq.pop();\n long long next_time = dummy.first;\n current_room = dummy.second;\n current_time = max(current_time, next_time);\n }\n ending_pq.push(make_pair(current_time + (end - start), current_room));\n // cout << \"curr start: \" << start << \" curr end: \" << end << endl;\n // cout << \"chosen room: \" << current_room << endl;\n // cout << \"current_time: \" << current_time << \" ending time \" << (current_time + end - start) << endl;\n counter[current_room]++;\n }\n\n long long max = 0;\n for (long long i = 0; i < n; i++) {\n if (counter[i] > counter[max]) {\n max = i;\n }\n }\n return max;\n }\n};",
"memory": "122143"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // int mostBooked(int n, vector<vector<int>>& nums) {\n // sort(nums.begin(),nums.end());\n // vector<int> fre(n,0);\n // vector<long long> end(n,0);\n // for(int i=0;i<nums.size();i++) {\n // bool flag = false;\n // for(int j=0;j<n;j++) {\n // if(end[j]<=nums[i][0]) {\n // end[j] = nums[i][1];\n // fre[j]++;\n // flag = true;\n // break;\n // }\n // }\n // if(!flag) {\n // long long minVal = LONG_MAX;\n // int minIndex;\n // for(int j=0;j<n;j++) {\n // if(minVal>end[j]) {\n // minIndex = j;\n // minVal = end[j];\n // }\n // }\n // end[minIndex] = end[minIndex] + 1LL*(nums[i][1] - nums[i][0]);\n // fre[minIndex]++;\n // }\n // } \n // int maxVal = INT_MIN;\n // int maxIndex;\n // for(int i=0;i<n;i++) {\n // if(maxVal<fre[i]) {\n // maxIndex = i;\n // maxVal = fre[i];\n // }\n // }\n // return maxIndex;\n // }\n \n\n int mostBooked(int n, vector<vector<int>>& nums) {\n sort(nums.begin(),nums.end());\n vector<int> fre(n,0);\n priority_queue<vector<long long>,vector<vector<long long>>,greater<vector<long long>>> pq1;\n priority_queue<int,vector<int>,greater<int>> pq2;\n\n for(int i=0;i<n;i++) {\n pq2.push(i);\n }\n for(int i=0;i<nums.size();i++) {\n while(!pq1.empty() && pq1.top()[0]<=nums[i][0]) {\n auto x = pq1.top(); pq1.pop();\n pq2.push(x[1]);\n } \n if(!pq2.empty()) {\n int room = pq2.top(); pq2.pop();\n fre[room]++;\n pq1.push({nums[i][1],room});\n } else {\n auto x = pq1.top(); pq1.pop();\n x[0] = x[0] + 1LL*(nums[i][1]-nums[i][0]);\n fre[x[1]]++;\n pq1.push(x);\n }\n } \n int maxVal = INT_MIN;\n int maxIndex;\n for(int i=0;i<n;i++) {\n if(maxVal<fre[i]) {\n maxIndex = i;\n maxVal = fre[i];\n }\n }\n return maxIndex;\n }\n\n};",
"memory": "122806"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define PLL pair<long, long>\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) \n {\n vector<long> roomCounter (n, 0);\n priority_queue<long, vector<long>, greater<long>> roomAvailabilityPQ;\n priority_queue<PLL, vector<PLL>, greater<PLL>> inProgressPQ, waitPQ;\n\n long mostUsedRoom = 0, currentMax = 0;\n for(auto meeting : meetings)\n {\n waitPQ.push(make_pair(meeting[0], meeting[1]));\n }\n\n for(uint8_t i = 0; i < n; i++)\n roomAvailabilityPQ.push(i);\n \n while(!waitPQ.empty())\n {\n PLL topWaitEle = waitPQ.top();\n waitPQ.pop();\n if(!inProgressPQ.empty())\n {\n while(!inProgressPQ.empty() && inProgressPQ.top().first <= topWaitEle.first)\n {\n PLL inProgressTopEle = inProgressPQ.top();\n inProgressPQ.pop();\n roomAvailabilityPQ.push(inProgressTopEle.second);\n }\n\n if(roomAvailabilityPQ.empty())\n {\n PLL topInProgressEle = inProgressPQ.top();\n inProgressPQ.pop();\n roomAvailabilityPQ.push(topInProgressEle.second);\n topWaitEle.second = topInProgressEle.first + (topWaitEle.second - topWaitEle.first); \n }\n } \n\n int earliestAvailableRoom = roomAvailabilityPQ.top();\n roomAvailabilityPQ.pop();\n roomCounter[earliestAvailableRoom]++;\n inProgressPQ.push(make_pair(topWaitEle.second, earliestAvailableRoom)); \n }\n\n for(int i = 0; i < n; i++)\n {\n if(roomCounter[i] > currentMax)\n {\n currentMax = roomCounter[i];\n mostUsedRoom = i;\n }\n }\n\n return mostUsedRoom; \n }\n};",
"memory": "123468"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define PLL pair<long, long>\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) \n {\n vector<long> roomCounter (n, 0);\n priority_queue<long, vector<long>, greater<long>> roomAvailabilityPQ;\n priority_queue<PLL, vector<PLL>, greater<PLL>> inProgressPQ, waitPQ;\n\n long mostUsedRoom = 0, currentMax = 0;\n for(auto meeting : meetings)\n {\n waitPQ.push(make_pair(meeting[0], meeting[1]));\n }\n\n for(uint8_t i = 0; i < n; i++)\n roomAvailabilityPQ.push(i);\n \n while(!waitPQ.empty())\n {\n PLL topWaitEle = waitPQ.top();\n waitPQ.pop();\n if(inProgressPQ.empty())\n {\n int earliestAvailableRoom = roomAvailabilityPQ.top();\n roomAvailabilityPQ.pop();\n roomCounter[earliestAvailableRoom]++;\n inProgressPQ.push(make_pair(topWaitEle.second, earliestAvailableRoom));\n }\n else\n {\n while(!inProgressPQ.empty() && inProgressPQ.top().first <= topWaitEle.first)\n {\n PLL inProgressTopEle = inProgressPQ.top();\n inProgressPQ.pop();\n roomAvailabilityPQ.push(inProgressTopEle.second);\n }\n\n if(roomAvailabilityPQ.empty())\n {\n PLL topInProgressEle = inProgressPQ.top();\n inProgressPQ.pop();\n roomAvailabilityPQ.push(topInProgressEle.second);\n topWaitEle.second = topInProgressEle.first + (topWaitEle.second - topWaitEle.first); \n }\n\n int earliestAvailableRoom = roomAvailabilityPQ.top();\n roomAvailabilityPQ.pop();\n roomCounter[earliestAvailableRoom]++;\n inProgressPQ.push(make_pair(topWaitEle.second, earliestAvailableRoom));\n }\n }\n\n for(int i = 0; i < n; i++)\n {\n if(roomCounter[i] > currentMax)\n {\n currentMax = roomCounter[i];\n mostUsedRoom = i;\n }\n }\n\n return mostUsedRoom; \n }\n};",
"memory": "124131"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define ll long long\nstruct Meeting{\n int start;\n ll end;\n Meeting(int start,ll end){\n this->start=start;\n this->end=end;\n }\n};\nstruct Room{\n int roomId;\n ll end;\n Room(int roomId, ll end){\n this->roomId=roomId;\n this->end=end;\n }\n};\nclass Compare{\n public:\n bool operator()(Room a,Room b){\n if(a.end==b.end){\n return a.roomId>b.roomId;\n }\n return a.end>b.end;\n }\n};\nclass Solution {\npublic:\n bool static compare(Meeting a, Meeting b){\n if(a.start==b.start){\n return a.end<b.end;\n }\n return a.start<b.start;\n }\n int mostBooked(int N,vector<vector<int>>& intervals) {\n int n=intervals.size();\n if(n==0)\n return 0;\n vector<Meeting> meetings;\n for(auto i:intervals){\n struct Meeting meeting(i[0],(ll)i[1]);\n meetings.push_back(meeting);\n }\n sort(meetings.begin(),meetings.end(),compare);\n int end=meetings[0].end;\n vector<int> mostUsed(N,0);\n priority_queue<Room,vector<Room>,Compare> usedRooms;\n priority_queue<int,vector<int>,greater<int>> unusedRooms;\n for(int i=0;i<N;i++){\n unusedRooms.push(i);\n }\n for(int i=0;i<n;i++){\n int start=meetings[i].start,end=meetings[i].end;\n while(!usedRooms.empty() && usedRooms.top().end<=start){\n unusedRooms.push(usedRooms.top().roomId);\n usedRooms.pop();\n }\n if(unusedRooms.empty()){ // wait for another room\n Room room=usedRooms.top();\n usedRooms.pop();\n room.end+=(end-start);\n usedRooms.push(room);\n mostUsed[room.roomId]++;\n }\n else{\n int id=unusedRooms.top();\n unusedRooms.pop();\n mostUsed[id]++;\n usedRooms.push(Room(id,end));\n }\n }\n int mostFreq=0;\n for(auto i:mostUsed){\n mostFreq=max(mostFreq,i);\n //cout<<i<<\" \";\n }\n for(int i=0;i<N;i++){\n if(mostUsed[i]==mostFreq)\n return i;\n }\n return 0;\n }\n};",
"memory": "124131"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define ll long long \n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);\n vector<ll> ans(100, 0);\n ll maxi = 0, room = 0;\n priority_queue<ll, vector<ll>, greater<ll>> pq1;\n priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>>> pq2;\n sort(begin(meetings), end(meetings));\n for(ll i = 0; i < n; i++) pq1.push(i);\n for(const auto i : meetings){\n while(!pq2.empty() && pq2.top()[0] <= i[0]) {\n pq1.push(pq2.top()[1]);\n pq2.pop();\n }\n if(!pq1.empty()){\n ll room = pq1.top();\n ans[room]++;\n pq1.pop();\n pq2.push({(ll)i[1], (ll)room});\n } else {\n ll room = pq2.top()[1], time = pq2.top()[0];\n ans[room]++;\n pq2.pop();\n pq2.push({(ll)i[1]-(ll)i[0]+(ll)time, (ll)room});\n }\n }\n for(ll i = 0; i < 100; i++) if(ans[i] > maxi) { maxi = ans[i]; room = i;}\n return room;\n }\n};",
"memory": "124793"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>> > onHold, busyRooms, toAdd;\n priority_queue<long long, vector<long long>, greater<long long> > availableRooms, nextEvent;\n sort(meetings.begin(), meetings.end());\n vector<long long> num(n);\n for(int i=0; i<meetings.size(); i++){\n toAdd.push(pair<long long,long long> {meetings[i][0], i});\n nextEvent.push(meetings[i][0]);\n }\n for(int i=0; i<n; i++){\n availableRooms.push(i);\n }\n while(!nextEvent.empty()){\n long long t=nextEvent.top();\n nextEvent.pop();\n // first update toAdd\n if(!toAdd.empty() && toAdd.top().first==t){\n pair<long long, long long> p=toAdd.top();\n toAdd.pop();\n onHold.push(p);\n }\n //then free rooms\n if(!busyRooms.empty() && busyRooms.top().first==t){\n pair<long long, long long> p=busyRooms.top();\n busyRooms.pop();\n availableRooms.push(p.second);\n }\n //then allocate new rooms\n while(!onHold.empty() && !availableRooms.empty()){\n pair<long long, long long> p1=onHold.top();\n onHold.pop();\n long long room = availableRooms.top();\n availableRooms.pop();\n long long meetIndex=p1.second;\n long long endTime = t+(meetings[meetIndex][1]-meetings[meetIndex][0]);\n busyRooms.push(pair<long long, long long> {endTime, room});\n nextEvent.push(endTime);\n num[room]++;\n }\n }\n long long ans=0, maxNum=0;\n for(int i=0; i<n; i++){\n if(num[i]>maxNum){\n ans=i;\n maxNum=num[i];\n }\n }\n return ans;\n }\n};",
"memory": "125456"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(), meetings.end());\n vector<int> count(n, 0);\n priority_queue<vector<long long int>, vector<vector<long long int>>, greater<vector<long long int>>>\n pq;\n pq.push({meetings[0][1], 0});\n count[0]++;\n set<int> s;\n for (int i = 1; i < n; i++) {\n s.insert(i);\n }\n for (int k = 1; k < meetings.size(); k++) {\n while (!pq.empty() && pq.top()[0] <= meetings[k][0]) {\n s.insert(pq.top()[1]);\n pq.pop();\n }\n if (!s.empty()) {\n auto itr = s.begin();\n pq.push({(long long int)meetings[k][1], *itr});\n count[*itr]++;\n s.erase(*itr);\n continue;\n }\n vector<long long int> a = pq.top();\n pq.pop();\n count[a[1]]++;\n if (a[0] >= (long long int)meetings[k][0]) {\n pq.push({(long long int)(a[0] + meetings[k][1] - meetings[k][0]), a[1]});\n } else {\n pq.push({meetings[k][1], a[1]});\n }\n }\n int max = -1;\n int maxind = 0;\n for (int a = 0; a < n; a++) {\n if (max < count[a]) {\n maxind = a;\n max = count[a];\n }\n }\n return maxind;\n }\n};",
"memory": "126118"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n struct cmp {\n bool operator()(vector<long long>& a, vector<long long>& b) {\n if (a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n }\n };\n\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<int, vector<int>, greater<int>> available; // Min-heap for available rooms\n priority_queue<vector<long long>, vector<vector<long long>>, cmp> used_room; // Min-heap for rooms in use\n \n for (int i = 0; i < n; i++) {\n available.push(i); // Initially all rooms are available\n }\n\n vector<int> cnt(n, 0); // To count meetings per room\n\n sort(meetings.begin(), meetings.end()); // Sort meetings by start time\n\n for (auto& meeting : meetings) {\n long long start = meeting[0];\n long long end = meeting[1];\n\n // Free up all rooms that should be free by the start of this meeting\n while (!used_room.empty() && used_room.top()[1] <= start) {\n available.push(used_room.top()[2]);\n used_room.pop();\n }\n\n if (!available.empty()) {\n int roomIndex = available.top();\n available.pop();\n cnt[roomIndex]++;\n used_room.push({start, end, roomIndex}); // Add to used rooms with correct start and end times\n } else {\n auto room = used_room.top();\n used_room.pop();\n int roomIndex = room[2];\n cnt[roomIndex]++;\n long long newStart = room[1]; // Meeting starts as soon as the room is free\n long long newEnd = newStart + (end - start);\n used_room.push({newStart, newEnd, roomIndex}); // Push with new calculated times\n }\n }\n\n // Find the room with the maximum number of meetings\n int maxi = 0;\n int ans = 0;\n for (int i = 0; i < n; i++) {\n if (cnt[i] > maxi) {\n maxi = cnt[i];\n ans = i;\n }\n }\n\n return ans;\n }\n};\n",
"memory": "126781"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\n //Approach 2: sorting and using heapsort\n //Time complexity = O(M.log M + M.N)\n //Space complexity = O(N)\n int mostBooked(int n, vector<vector<int>>& meetings) {\n \n vector<long> meetingHeldCount(n, 0);\n \n auto compare = [](auto &lhs, auto &rhs) {\n return lhs[0] == rhs[0] ? \n lhs[1] > rhs[1] : lhs[0] > rhs[0]; };\n\n //Here each node in heap tree will have node[0] = meeting end-time node[1] = room number\n priority_queue<vector<long>, vector<vector<long>>, decltype(compare)> minHeapScheduledMeetings(compare);\n //We need min heap to keep track of all empty rooms where we can schedule the meeting and we will choose the one with min index\n priority_queue<int, vector<int>, greater<int>> emptyRooms;\n \n sort(meetings.begin(), meetings.end());\n //push all rooms on min heap\n\n for(int i=0; i<n; i++){\n emptyRooms.push({i});\n }\n\n for(auto &meeting: meetings) {\n \n int start = meeting[0], end = meeting[1];\n \n while(!minHeapScheduledMeetings.empty() \n && minHeapScheduledMeetings.top()[0] <= start ) {\n\n auto availableRoom = minHeapScheduledMeetings.top();\n minHeapScheduledMeetings.pop();\n emptyRooms.push(availableRoom[1]);\n }\n \n //if we have empty room available\n vector<long> scheduleMeeting = {0, 0};\n if(!emptyRooms.empty()) {\n scheduleMeeting = {end, emptyRooms.top()};\n emptyRooms.pop();\n }\n else {\n // if we don't have empty room available then we take the \n // the which is gonna available first\n scheduleMeeting = minHeapScheduledMeetings.top();\n scheduleMeeting[0] += end - start;\n minHeapScheduledMeetings.pop();\n }\n \n minHeapScheduledMeetings.push(scheduleMeeting);\n meetingHeldCount[scheduleMeeting[1]]++;\n }\n\n int maxHeldMeetings = 0, maxHeldMeetingRoom = 0;\n\n for(int i=0; i<n; i++) {\n if(meetingHeldCount[i] > maxHeldMeetings) {\n maxHeldMeetings = meetingHeldCount[i];\n maxHeldMeetingRoom = i;\n }\n }\n\n return maxHeldMeetingRoom;\n }\n\n //Approach 1: sorting and traversing\n //Time complexity = O(M.log M + M.N)\n //Space complexity = O(N)\n int mostBooked_1(int n, vector<vector<int>>& meetings) {\n \n vector<long> meetingHeldCount(n, 0);\n vector<long> roomAvailableTime(n, 0);\n \n sort(meetings.begin(), meetings.end());\n\n for(auto &meeting: meetings) {\n int start = meeting[0], end = meeting[1];\n long firstAvailableRoomTime = LONG_MAX;\n int firstAvailableRoom = 0;\n bool foundRoom = false;\n\n for(int i=0; i<n; i++) {\n if(roomAvailableTime[i] <= start){\n meetingHeldCount[i]++;\n roomAvailableTime[i] = end;\n foundRoom = true;\n break;\n }\n\n if(firstAvailableRoomTime > roomAvailableTime[i]){\n firstAvailableRoomTime = roomAvailableTime[i];\n firstAvailableRoom = i;\n }\n } \n\n if(!foundRoom){\n roomAvailableTime[firstAvailableRoom] += end - start;\n meetingHeldCount[firstAvailableRoom]++;\n } \n }\n\n int maxHeldMeetings = 0, maxHeldMeetingRoom = 0;\n\n for(int i=0; i<n; i++) {\n if(meetingHeldCount[i] > maxHeldMeetings){\n maxHeldMeetings = meetingHeldCount[i];\n maxHeldMeetingRoom = i;\n }\n }\n\n return maxHeldMeetingRoom;\n }\n};",
"memory": "127443"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n\n //Approach 2: sorting and using heapsort\n //Time complexity = O(M.log M + M.N)\n //Space complexity = O(N)\n int mostBooked(int n, vector<vector<int>>& meetings) {\n \n vector<long> meetingHeldCount(n, 0);\n \n auto compare = [](auto &lhs, auto &rhs) {\n return lhs[0] == rhs[0] ? \n lhs[1] > rhs[1] : lhs[0] > rhs[0]; };\n\n //Here each node in heap tree will have node[0] = meeting end-time node[1] = room number\n priority_queue<vector<long>, vector<vector<long>>, decltype(compare)> occupiedRoom(compare);\n //We need min heap to keep track of all empty rooms where we can schedule the meeting and we will choose the one with min index\n priority_queue<int, vector<int>, greater<int>> emptyRooms;\n \n sort(meetings.begin(), meetings.end());\n //push all rooms on min heap\n\n for(int i=0; i<n; i++){\n emptyRooms.push({i});\n }\n\n for(auto &meeting: meetings) {\n \n int start = meeting[0], end = meeting[1];\n \n while(!occupiedRoom.empty() \n && occupiedRoom.top()[0] <= start ) {\n\n auto availableRoom = occupiedRoom.top();\n occupiedRoom.pop();\n emptyRooms.push(availableRoom[1]);\n }\n \n //if we have empty room available\n vector<long> scheduleMeeting = {0, 0};\n if(!emptyRooms.empty()) {\n scheduleMeeting = {end, emptyRooms.top()};\n emptyRooms.pop();\n }\n else {\n // if we don't have empty room available then we take the \n // the which is gonna available first\n scheduleMeeting = occupiedRoom.top();\n scheduleMeeting[0] += end - start;\n occupiedRoom.pop();\n }\n \n occupiedRoom.push(scheduleMeeting);\n meetingHeldCount[scheduleMeeting[1]]++;\n }\n\n int maxHeldMeetings = 0, maxHeldMeetingRoom = 0;\n\n for(int i=0; i<n; i++) {\n if(meetingHeldCount[i] > maxHeldMeetings) {\n maxHeldMeetings = meetingHeldCount[i];\n maxHeldMeetingRoom = i;\n }\n }\n\n return maxHeldMeetingRoom;\n }\n\n //Approach 1: sorting and traversing\n //Time complexity = O(M.log M + M.N)\n //Space complexity = O(N)\n int mostBooked_1(int n, vector<vector<int>>& meetings) {\n \n vector<long> meetingHeldCount(n, 0);\n vector<long> roomAvailableTime(n, 0);\n \n sort(meetings.begin(), meetings.end());\n\n for(auto &meeting: meetings) {\n int start = meeting[0], end = meeting[1];\n long firstAvailableRoomTime = LONG_MAX;\n int firstAvailableRoom = 0;\n bool foundRoom = false;\n\n for(int i=0; i<n; i++) {\n if(roomAvailableTime[i] <= start){\n meetingHeldCount[i]++;\n roomAvailableTime[i] = end;\n foundRoom = true;\n break;\n }\n\n if(firstAvailableRoomTime > roomAvailableTime[i]){\n firstAvailableRoomTime = roomAvailableTime[i];\n firstAvailableRoom = i;\n }\n } \n\n if(!foundRoom){\n roomAvailableTime[firstAvailableRoom] += end - start;\n meetingHeldCount[firstAvailableRoom]++;\n } \n }\n\n int maxHeldMeetings = 0, maxHeldMeetingRoom = 0;\n\n for(int i=0; i<n; i++) {\n if(meetingHeldCount[i] > maxHeldMeetings){\n maxHeldMeetings = meetingHeldCount[i];\n maxHeldMeetingRoom = i;\n }\n }\n\n return maxHeldMeetingRoom;\n }\n};",
"memory": "128106"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<long long> arr(n,0);\n sort(meetings.begin(),meetings.end());\n priority_queue<vector<long long>,vector<vector<long long>>,greater<vector<long long>>> pq;\n priority_queue<vector<long long>,vector<vector<long long>>,greater<vector<long long>>> occ;\n for(long long i=0;i<n;i++){\n pq.push({-1,i});\n }\n for(long long i=0;i<meetings.size();i++){\n if(occ.size()==0){\n vector<long long> temp = pq.top();\n pq.pop();\n temp[0]=meetings[i][1];\n arr[temp[1]]++;\n occ.push(temp);\n }\n else{\n while(!occ.empty() && occ.top()[0]<=meetings[i][0]){\n long long t = occ.top()[1];\n pq.push({-1,t});\n occ.pop();\n }\n if(pq.empty()){\n vector<long long> temp = occ.top();\n occ.pop();\n long long diff = temp[0]-meetings[i][0];\n arr[temp[1]]++;\n long long h = (long long)meetings[i][1]+diff;\n occ.push({h,temp[1]});\n }\n else{\n vector<long long> temp = pq.top();\n pq.pop();\n arr[temp[1]]++;\n occ.push({meetings[i][1],temp[1]});\n }\n }\n }\n long long maxi=0;\n int num = -1;\n for(int i=0;i<arr.size();i++){\n if(maxi<arr[i]){\n maxi=arr[i];\n num=i;\n }\n }\n return num;\n }\n};",
"memory": "128106"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<int> cnt(n);\n\n set<int> open;\n set<pair<long long, int>> freed;\n\n sort(meetings.begin(), meetings.end());\n for (int i = 0; i < n; ++i) {\n open.insert(i);\n }\n\n for (const vector<int>& plan : meetings) {\n int start = plan[0];\n int end = plan[1];\n\n while (!freed.empty() && freed.begin()->first <= start) {\n open.insert(freed.begin()->second);\n freed.erase(freed.begin());\n }\n\n if (!open.empty()) {\n int new_room = (*(open.begin()));\n open.erase(open.begin());\n\n freed.insert(pair<long long, int>(end, new_room));\n ++cnt[new_room];\n } else {\n auto first_freed = (*(freed.begin()));\n freed.erase(freed.begin());\n\n ++cnt[first_freed.second];\n\n freed.insert(pair<long long, int>(first_freed.first + (end - start), first_freed.second));\n }\n }\n\n int ans = 0;\n for (int i = 1; i < n; ++i) {\n if (cnt[i] > cnt[ans]) {\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "128768"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n // Set of available rooms, sorted by room index\n set<int> available;\n for (int i = 0; i < n; i++) {\n available.insert(i);\n }\n\n // Set of busy rooms {end time, room index}, sorted by end time first, then index\n set<pair<long long, int>> busy;\n\n // Vector to count the number of meetings per room\n vector<int> roomCount(n, 0);\n\n // Sort meetings by their start time\n sort(meetings.begin(), meetings.end());\n\n for (const auto& meeting : meetings) {\n long long start = meeting[0];\n long long end = meeting[1];\n\n // Free up rooms that are now available before this meeting starts\n while (!busy.empty() && busy.begin()->first <= start) {\n available.insert(busy.begin()->second);\n busy.erase(busy.begin());\n }\n\n if (!available.empty()) {\n // Use the earliest available room by index\n int room = *available.begin();\n available.erase(available.begin());\n busy.insert({end, room});\n roomCount[room]++;\n } else {\n // No available rooms, use the room that will be free the earliest\n auto nextFree = *busy.begin();\n busy.erase(busy.begin());\n int room = nextFree.second;\n long long newEnd = nextFree.first + (end - start);\n busy.insert({newEnd, room});\n roomCount[room]++;\n }\n }\n\n // Find the room with the most meetings\n int maxRoom = 0;\n for (int i = 1; i < n; i++) {\n if (roomCount[i] > roomCount[maxRoom]) {\n maxRoom = i;\n }\n }\n\n return maxRoom;\n }\n};\n",
"memory": "129431"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n // for(auto &meeting:meetings)\n // meeting[1]--;\n set<pair<ll,ll>> dp;\n vector<ll> numHeld(n);\n set<ll> free;\n for(ll i=0;i<n;i++)\n free.insert(i);\n for(ll i=0;i<meetings.size();i++){\n ll newStart = meetings[i][0];\n ll newEnd = meetings[i][1];\n if(free.size()>0){\n while(!dp.empty() && (*dp.begin()).first<=newStart){\n free.insert((*dp.begin()).second);\n dp.erase(dp.begin());\n }\n numHeld[*free.begin()]++;\n dp.insert({newEnd,*free.begin()});\n free.erase(free.begin());\n }else{\n ll oldEnd = (*dp.begin()).first;\n ll oldIdx = (*dp.begin()).second;\n ll duration = newEnd - newStart;\n if(oldEnd>=newStart){\n numHeld[oldIdx]++;\n dp.erase(dp.begin());\n dp.insert({oldEnd+duration,oldIdx});\n }else{\n while(!dp.empty() && (*dp.begin()).first<=newStart){\n free.insert((*dp.begin()).second);\n dp.erase(dp.begin());\n }\n numHeld[*free.begin()]++;\n dp.insert({newEnd,*free.begin()});\n free.erase(free.begin());\n }\n }\n }\n ll ans = 0;\n for(ll i=0;i<n;i++){\n cout<<i<<\" \"<<numHeld[i]<<endl;\n if(numHeld[i]>numHeld[ans])\n ans = i;\n }\n return ans;\n }\n};",
"memory": "129431"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n int n = 0;\n priority_queue<int, vector<int>, greater<int>> available;\n priority_queue<pair<long,long>, vector<pair<long,long>>, greater<pair<long,long>>> bookedRooms;\n vector<int> rooms;\n vector<vector<int>> meetings;\n\n void fillAvailableRoomsQueue(){\n for(int i=0 ; i<n; i++){\n this->available.push(i);\n }\n }\n\n void freeUpRooms(){\n auto [endtime, room] = this->bookedRooms.top();\n this->bookedRooms.pop();\n this->available.push(room);\n }\n\n void blockMeetingRoomsIfAvailable(int endTime){\n int room = this->available.top();\n this->available.pop();\n this->bookedRooms.push({endTime, room});\n this->rooms[room]++;\n }\n void blockMeetingRoomWhenNextAvailable(long nextMeetingStartTime, long nextMeetingEndTime){\n auto [endtime, room] = this->bookedRooms.top();\n this->bookedRooms.pop();\n long newMeetingDelay = endtime - nextMeetingStartTime;\n this->bookedRooms.push({nextMeetingEndTime + newMeetingDelay, room});\n this->rooms[room]++;\n }\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n this->rooms.resize(n,0);\n this->n = n;\n sort(meetings.begin(), meetings.end());\n this->meetings = meetings;\n\n fillAvailableRoomsQueue();\n \n for(vector<int> meeting: meetings){\n int startTime = meeting[0], endTime = meeting[1];\n while(this->bookedRooms.size() > 0 && this->bookedRooms.top().first <= startTime){\n freeUpRooms();\n }\n\n if(available.size()){\n blockMeetingRoomsIfAvailable(endTime);\n } else {\n blockMeetingRoomWhenNextAvailable(startTime, endTime);\n }\n }\n\n int ans = 0;\n for(int i=0 ; i<n ; i++){\n if(this->rooms[i] > this->rooms[ans]){\n ans = i;\n }\n }\n\n return ans;\n }\n};",
"memory": "130093"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& ar) {\n vector<int> co(n, 0);\n multiset<pair<long,int>> ms;\n for(int i=0;i<n;++i)\n ms.insert({-1, i});\n \n sort(ar.begin(), ar.end());\n for(int i=0;i<ar.size();++i)\n {\n while((*(ms.begin())).first<ar[i][0])\n {\n auto x = *(ms.begin());\n ms.erase(ms.begin());\n ms.insert({ar[i][0], x.second});\n }\n \n auto [x, ind] = *(ms.begin());\n ms.erase(ms.begin());\n ++co[ind];\n ms.insert({max(x+1, (long)ar[i][0]) + ar[i][1]-ar[i][0]-1, ind});\n }\n\n int mx = 0, ind = 0;\n for(int i=0;i<n;++i)\n if(co[i]>mx)\n ind=i, mx=co[i];\n \n return ind;\n }\n};",
"memory": "130756"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& ar) {\n vector<int> co(n, 0);\n multiset<pair<long,int>> ms;\n for(int i=0;i<n;++i)\n ms.insert({-1, i});\n \n sort(ar.begin(), ar.end());\n for(int i=0;i<ar.size();++i)\n {\n while((*(ms.begin())).first<ar[i][0])\n {\n auto x = *(ms.begin());\n ms.erase(ms.begin());\n ms.insert({ar[i][0], x.second});\n }\n \n auto [x, ind] = *(ms.begin());\n ms.erase(ms.begin());\n ++co[ind];\n ms.insert({max(x+1, (long)ar[i][0]) + ar[i][1]-ar[i][0]-1, ind});\n }\n\n int mx = 0, ind = 0;\n for(int i=0;i<n;++i)\n if(co[i]>mx)\n ind=i, mx=co[i];\n \n return ind;\n }\n};",
"memory": "130756"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, const vector<vector<int>>& meetings) {\n // Create sorted lists of start and end times of meetings\n vector<vector<int>> start;\n start.reserve(meetings.size());\n for (int i = 0; i < meetings.size(); ++i) {\n start.push_back({meetings[i][0], i});\n }\n sort(start.begin(), start.end());\n\n // Create room_availability in O(n) using heapify\n vector<vector<long>> roomIndices;\n roomIndices.reserve(n);\n for (int i = 0; i < n; ++ i) {\n roomIndices.push_back({0, i});\n }\n priority_queue<vector<long>, vector<vector<long>>, std::greater<vector<long>>> occupied_rooms(std::greater<vector<long>>(), roomIndices);\n\n priority_queue<int, vector<int>, greater<int>> available_rooms;\n\n vector<int> roomToBookedCount(n, 0);\n\n for (int i = 0; i < start.size(); ++i) {\n const int meeting_id = start[i][1];\n const long duration = meetings[meeting_id][1] - meetings[meeting_id][0];\n const long scheduled_start = meetings[meeting_id][0];\n\n // Mark available rooms for meetings that have ended before the scheudled start\n while (occupied_rooms.size() && occupied_rooms.top()[0] <= scheduled_start) {\n const int room_id = occupied_rooms.top()[1];\n occupied_rooms.pop();\n available_rooms.push(room_id);\n }\n\n // Wait for availability if no room is available\n long actual_start = scheduled_start;\n if (available_rooms.empty()) {\n actual_start = occupied_rooms.top()[0];\n const int room_id = occupied_rooms.top()[1];\n occupied_rooms.pop();\n available_rooms.push(room_id);\n }\n const long actual_end = actual_start + duration;\n\n const int room_id = available_rooms.top();\n available_rooms.pop(); \n\n occupied_rooms.push({actual_end, room_id});\n ++roomToBookedCount[room_id];\n //cout << \"Scheduling meeting #\" << meeting_id << \" in room #\" << room_id << \" from \" << actual_start << \" until \" << actual_end << \".\" << endl;\n }\n\n return max_element(roomToBookedCount.begin(), roomToBookedCount.end()) - roomToBookedCount.begin();\n }\n};",
"memory": "131418"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n set<long long> free_rooms;\n for(long long i = 0;i<n;i++) {\n free_rooms.insert(i+1);\n }\n\n priority_queue<pair<long long,long long>, vector<pair<long long,long long>>, greater<pair<long long,long long>>> waiting, busy_rooms;\n for(auto meeting : meetings) {\n waiting.push({meeting[0], meeting[1]});\n }\n vector<long long> used(n+1,0);\n while(! waiting.empty()) {\n\n while(! busy_rooms.empty() && busy_rooms.top().first <= waiting.top().first) {\n free_rooms.insert(busy_rooms.top().second);\n busy_rooms.pop();\n }\n\n auto cur = waiting.top();\n waiting.pop();\n\n if(! free_rooms.empty()) {\n long long room = *free_rooms.begin();\n free_rooms.erase(room);\n used[room] ++;\n busy_rooms.push({cur.second, room});\n \n } else {\n long long room = busy_rooms.top().second;\n long long cur_time = busy_rooms.top().first;\n busy_rooms.pop();\n used[room]++;\n busy_rooms.push({cur_time + cur.second - cur.first, room});\n }\n }\n vector<pair<long long, long long>> sorted_rooms;\n for(long long i = 0;i<n;i++) {\n sorted_rooms.push_back({used[i+1], i+1});\n \n }\n int ans = 0;\n long long freq = used[1];\n for(long long i =2;i<=n;i++){\n if(used[i] > freq){\n freq = used[i];\n ans = i-1;\n }\n }\n return ans;\n\n }\n};",
"memory": "132081"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n priority_queue<long long, vector<long long> , greater<long long>> freeRooms;\n\n for(int i=0;i<n;i++){\n freeRooms.push(i);\n }\n\n priority_queue<vector<long long> , vector<vector<long long>> , greater<vector<long long>>> pq;\n\n vector<int> rooms(n , 0);\n\n for(auto meet : meetings){\n long long st = (long long)meet[0];\n long long ed = (long long)meet[1];\n\n if(pq.size() > 0 && pq.top()[0] <= st){\n while(pq.size() > 0 && pq.top()[0] <= st){\n freeRooms.push(pq.top()[1]);\n pq.pop();\n } \n }\n \n if(freeRooms.size() > 0){\n long long ind = freeRooms.top();\n freeRooms.pop();\n rooms[ind]++;\n pq.push({ed , ind});\n }else{\n vector<long long> tp = pq.top();\n pq.pop();\n\n long long finishTime = tp[0] - st + ed;\n pq.push({finishTime , tp[1]});\n rooms[tp[1]]++;\n }\n }\n\n int mx = -1;\n int ind = -1;\n\n for(int i=0;i<n;i++){\n if(rooms[i] > mx){\n mx = rooms[i];\n ind = i;\n }\n }\n\n return ind;\n }\n};",
"memory": "132081"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\n #define ll long long\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<ll> meets(n,0);\n set<ll> s;\n for(int i=0;i<n;i++) s.insert(i);\n ll time = 0, ans = 0, mv = 0;\n priority_queue<vector<ll>, vector<vector<ll>>, greater<vector<ll>> > pq;\n sort(meetings.begin(), meetings.end());\n for(int i=0;i<meetings.size();i++) {\n if(time<meetings[i][0]) time = meetings[i][0];\n ll ct = time, et = time + meetings[i][1] - meetings[i][0];\n while(pq.size() && pq.top()[0]<=ct) {\n s.insert(pq.top()[1]);\n pq.pop();\n }\n if(s.size()) {\n pq.push({et,*s.begin()});\n meets[*s.begin()]++;\n s.erase(s.begin());\n } else {\n time = pq.top()[0];\n i--;\n }\n }\n for(int i=0;i<n;i++) {\n if(meets[i]>mv) {\n ans = i;\n mv = meets[i];\n }\n }\n return ans;\n }\n};",
"memory": "132743"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n priority_queue<pair<uint64_t,int>,vector<pair<uint64_t,int>>,greater<pair<uint64_t,int>>> pq;\n pair<uint64_t,int> p;\n set<int> s;\n uint64_t d;\n int ans = 0;\n vector<int> v(n);\n for(int i = 0; i <n;i++){\n s.insert(i);\n }\n for(auto m:meetings){\n d = 0;\n while(!pq.empty() and m[0]>=pq.top().first){\n p = pq.top();\n pq.pop();\n s.insert(p.second);\n }\n if(pq.size()==n){\n p = pq.top();\n pq.pop();\n d=max((uint64_t)0,p.first-m[0]);\n s.insert(p.second);\n }\n auto it = s.begin();\n pq.push({m[1]+d,*it});\n v[*it]++;\n if(v[*it]>v[ans]){\n ans=*it;\n }else if(v[*it]==v[ans]){\n ans = min(*it,ans);\n }\n s.erase(it);\n }\n return ans;\n }\n};",
"memory": "132743"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int k, vector<vector<int>>& v) {\n sort(v.begin(),v.end());\n priority_queue<vector<long long int>,vector<vector<long long int>>,greater<vector<long long int>>> pq;\n set<long long int> st;\n for(int i=0;i<k;i++){\n st.insert(i);\n }\n\n unordered_map<int,int> mp;\n mp[0]++;\n st.erase(st.begin());\n pq.push({v[0][1],0});\n int i=1;\n long long int t=v[0][0];\n while(!pq.empty() && i<v.size()){\n // cout<<t<<endl;\n t=max(t,(long long int)v[i][0]);\n while(!pq.empty() && pq.top()[0]<=t){\n st.insert(pq.top()[1]);\n pq.pop();\n }\n\n if(!st.empty()){\n auto it=st.begin();\n mp[*it]++;\n // cout<<i<<\" \"<<*it<<\" \"<<t+v[i][1]-v[i][0]<<endl;\n pq.push({t+v[i][1]-v[i][0], *it});\n st.erase(it);\n i++;\n } else {\n t=pq.top()[0];\n }\n }\n\n int c=0;\n for(int i=0;i<k;i++){\n // cout<<mp[i]<<endl;\n c=max(mp[i],c);\n }\n\n // cout<<c<<endl;\n // vector<int> ans;\n for(int i=0;i<v.size();i++){\n if(mp[i]==c){\n return i;\n }\n }\n return 0;\n }\n};",
"memory": "133406"
} |
2,479 | <p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
<p>Meetings are allocated to rooms in the following manner:</p>
<ol>
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
</ol>
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
<strong>Output:</strong> 0
<strong>Explanation:</strong>
- At time 0, both rooms are not being used. The first meeting starts in room 0.
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
- At time 2, both rooms are being used. The third meeting is delayed.
- At time 3, both rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
Both rooms 0 and 1 held 2 meetings, so we return 0.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
- At time 4, all three rooms are being used. The fourth meeting is delayed.
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
- At time 6, all three rooms are being used. The fifth meeting is delayed.
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= n <= 100</code></li>
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
<li><code>meetings[i].length == 2</code></li>
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int mostBooked(int k, vector<vector<int>>& v) {\n sort(v.begin(),v.end());\n priority_queue<vector<long long int>,vector<vector<long long int>>,greater<vector<long long int>>> pq;\n set<long long int> st;\n for(int i=0;i<k;i++){\n st.insert(i);\n }\n\n unordered_map<int,int> mp;\n mp[0]++;\n st.erase(st.begin());\n pq.push({v[0][1],0});\n int i=1;\n long long int t=v[0][0];\n while(!pq.empty() && i<v.size()){\n cout<<t<<endl;\n t=max(t,(long long int)v[i][0]);\n while(!pq.empty() && pq.top()[0]<=t){\n st.insert(pq.top()[1]);\n pq.pop();\n }\n\n if(!st.empty()){\n auto it=st.begin();\n mp[*it]++;\n // cout<<i<<\" \"<<*it<<\" \"<<t+v[i][1]-v[i][0]<<endl;\n pq.push({t+v[i][1]-v[i][0], *it});\n st.erase(it);\n i++;\n } else {\n t=pq.top()[0];\n }\n }\n\n int c=0;\n for(int i=0;i<k;i++){\n // cout<<mp[i]<<endl;\n c=max(mp[i],c);\n }\n\n // cout<<c<<endl;\n // vector<int> ans;\n for(int i=0;i<v.size();i++){\n if(mp[i]==c){\n return i;\n }\n }\n return 0;\n }\n};",
"memory": "134068"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.