id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
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 \n vector<long long >a(n+1,0);\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>>q;\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>>shiyongzhong;\n set<int>kong;\n for(int i=1;i<=n;i++)kong.insert(i);\n for(auto&p:meetings)q.push({p[0],p[1]});\n long long nowtime=0;\n unordered_map<long long,long long>mp;\n while(!q.empty()){\n int flag=0;\n if(nowtime<q.top().first){\n nowtime=q.top().first;\n }\n while(!shiyongzhong.empty()&&nowtime>=shiyongzhong.top().first){\n kong.insert(shiyongzhong.top().second);\n shiyongzhong.pop();\n }\n \n if(!kong.empty()){\n shiyongzhong.push({nowtime+(q.top().second-q.top().first),*kong.begin()});\n mp[*kong.begin()]++;\n kong.erase(kong.begin());\n flag=1;\n q.pop();\n }\n\n if(flag==0){\n nowtime=max(nowtime,shiyongzhong.top().first);\n }\n }\n int ans=0;\n int id=0;\n for(int i=1;i<=n;i++){\n if(ans<mp[i]){\n ans=mp[i];id=i;\n }\n }\n return id-1;\n }\n};\n",
"memory": "134731"
} |
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> pq;\n for(int i=0;i<N;i++){\n pq.push(Room(i,(ll)0));\n }\n for(int i=0;i<n;i++){\n int start=meetings[i].start,end=meetings[i].end;\n set<int> roomsFreed;\n while(!pq.empty() && start>=pq.top().end){\n roomsFreed.insert(pq.top().roomId);\n pq.pop();\n }\n for(auto id:roomsFreed){\n pq.push(Room(id,(ll)0));\n }\n Room room=pq.top();\n if(start<room.end){ //no empty room\n pq.pop();\n int roomId=room.roomId;\n ll endTime=room.end;\n mostUsed[roomId]++;\n pq.push(Room(roomId,endTime+end-start));\n }\n else{\n pq.pop();\n int roomId=room.roomId;\n mostUsed[roomId]++;\n pq.push(Room(roomId,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": "134731"
} |
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);cout.tie(NULL);\n }\n int mostBooked(int n, vector<vector<int>>& meetings) {\n set<int> availableRooms;\n for(int i = 0; i<n;i++)availableRooms.insert(i);\n vector<int> meetingCount(n,0);\n\n priority_queue<pair<long,int>, vector<pair<long,int>>, greater<pair<long,int>>> occupiedRooms;\n long timeDiff;\n\n sort(meetings.begin(), meetings.end());\n\n for(auto meeting: meetings){\n while(!occupiedRooms.empty() && occupiedRooms.top().first <= meeting[0]){\n availableRooms.insert(occupiedRooms.top().second);\n occupiedRooms.pop();\n }\n\n if(availableRooms.empty()){\n availableRooms.insert(occupiedRooms.top().second);\n timeDiff = occupiedRooms.top().first - meeting[0];\n occupiedRooms.pop();\n }\n int room = *availableRooms.begin();\n availableRooms.erase(room);\n meetingCount[room]++;\n occupiedRooms.push({meeting[1]+timeDiff, room});\n timeDiff =0;\n }\n int idx = 0;\n for(int i=1;i<n;i++){\n if(meetingCount[idx]<meetingCount[i])idx=i;\n }\n return idx;\n }\n};",
"memory": "135393"
} |
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 intt long long\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n priority_queue<vector<intt>,vector<vector<intt>>,greater<vector<intt>>>pq1;\n priority_queue<intt,vector<intt>,greater<intt>>pq2;\n for(int i=1;i<=n;i++){\n pq2.push(i);\n }\n\n vector<intt>freq(n+1,0);\n for(auto it:meetings){\n while(!pq1.empty() && pq1.top()[0]<=it[0]){\n auto ele=pq1.top();\n pq2.push(ele[1]);\n pq1.pop();\n }\n\n\n\n if(pq2.size()>0){\n auto itt=pq2.top();\n pq2.pop();\n freq[itt]++;\n pq1.push({it[1],itt});\n }else{\n auto itt=pq1.top();\n pq1.pop();\n // while(!pq1.empty() && pq1.top()[0]==itt[0]){\n // auto tele=pq1.top();\n // pq2.push(tele[1]);\n // pq1.pop();\n\n // }\n intt diff=abs(itt[0]-it[0]);\n pq1.push({diff+it[1],itt[1]});\n freq[itt[1]]++;\n \n\n \n }\n }\n intt room=0;\n intt maxi=-1;\n for(int i=1;i<=n;i++){\n if(maxi<freq[i]){\n maxi=freq[i];\n room=i-1;\n }\n\n }\n return room;\n\n\n }\n};",
"memory": "135393"
} |
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 using pii = pair<long long, int>;\n priority_queue<pii, vector<pii>, greater<>> meetings;\n unordered_map<int, long long> start_count;\n for (auto & meeting : _meetings) {\n start_count[meeting[0]] = 0;\n meetings.push({meeting[0], meeting[1]});\n }\n priority_queue<pii, vector<pii>, greater<>> rooms;\n unordered_map<int, int> freq;\n auto [start, end] = meetings.top(); meetings.pop();\n rooms.push({end, 0});\n freq[0] = 1;\n priority_queue<int, vector<int>, greater<>> free_rooms;\n for (int i = 1; i < n; ++i) {\n free_rooms.push(i);\n }\n while (!meetings.empty()) {\n auto [start, end] = meetings.top(); // Get the earliest start time meeting so far\n meetings.pop();\n while (!rooms.empty() and rooms.top().first <= start) {\n auto [ending_time, room] = rooms.top(); rooms.pop();\n free_rooms.push(room);\n }\n if (free_rooms.empty()) { // If there are no free rooms, we need to delay\n auto [ending_time, room] = rooms.top();\n rooms.pop();\n rooms.push({ending_time + (end - start), room});\n ++freq[room];\n } else {\n int room = free_rooms.top(); free_rooms.pop();\n rooms.push({end, room});\n ++freq[room];\n }\n }\n int mx = 0, mx_idx = 0;\n for (int i = n - 1; i >= 0; --i) {\n if (freq[i] >= mx) {\n mx = freq[i];\n mx_idx = i;\n }\n }\n return mx_idx;\n }\n};",
"memory": "136056"
} |
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": "typedef long long ll;\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector< int > ruc(n);\n sort( meetings.begin(), meetings.end() );\n multiset< int > availableRooms;\n set< pair<ll,int> > occupiedRooms;\n\n for( int i =0 ;i<n; i++ ) availableRooms.insert(i);\n for( auto meeting : meetings ) {\n while ( !occupiedRooms.empty() and occupiedRooms.begin()->first <= meeting[0]) {\n auto it = occupiedRooms.begin();\n availableRooms.insert( it->second );\n occupiedRooms.erase(it);\n }\n if ( availableRooms.empty() ) {\n auto it = occupiedRooms.begin();\n ll newEndTime = it->first + ( meeting[1] - meeting[0] );\n occupiedRooms.insert( {newEndTime,it->second} );\n ruc[it->second]++;\n occupiedRooms.erase(it);\n } else {\n auto it = availableRooms.begin();\n occupiedRooms.insert( {meeting[1], *it} );\n ruc[*it]++;\n availableRooms.erase(it);\n }\n }\n int ans = -1;\n int maxUse = 0;\n for( int i = 0; i<n; i++ ){\n if ( ruc[ i ] > maxUse ) { maxUse = ruc[i]; ans = i; }\n }\n return ans;\n }\n};",
"memory": "136718"
} |
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(begin(meetings), end(meetings), [](auto &a, auto &b) {\n if(a[0] == b[0]) return a[1] < b[1];\n return a[0] < b[0];\n });\n auto comp = [](auto &a, auto &b) {\n if(a[0] == b[0]) return a[1] > b[1];\n return a[0] > b[0];\n };\n priority_queue<vector<long long>, vector<vector<long long>>, decltype(comp)> pq;\n\n for(int i = 0; i < n; ++i) {\n pq.push({0, i, 0});\n }\n\n long long ans = INT_MAX, curr = 0;\n\n for(auto &meet : meetings) {\n vector<long long> t;\n\n while(pq.size() && pq.top()[0] < meet[0]) {\n t = pq.top(); pq.pop();\n pq.push({meet[0], t[1], t[2]});\n }\n\n t = pq.top(); pq.pop();\n if(t[0] > meet[0]) {\n pq.push({t[0]-meet[0]+meet[1], t[1], t[2]+1});\n } else {\n pq.push({meet[1], t[1], t[2]+1});\n }\n \n if(t[2]+1 > curr) curr = t[2]+1, ans = t[1];\n else if(t[2]+1 == curr) ans = min(ans, t[1]);\n }\n\n return ans;\n }\n};",
"memory": "137381"
} |
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 // To store the next available time and booking count for each room\n vector<pair<long long, int>> rooms(n, {0, 0}); // {next available time, booking count}\n \n // Min-heap to sort meetings by start time\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n for (const auto& meeting : meetings) {\n pq.push(meeting);\n }\n \n // Process each meeting in order of their start time\n while (!pq.empty()) {\n auto meeting = pq.top();\n pq.pop();\n int start = meeting[0];\n int end = meeting[1];\n \n int selectedRoom = -1;\n long long earliestAvailableTime = LLONG_MAX;\n\n // Find the first available room or the one with the earliest availability\n for (int i = 0; i < n; ++i) {\n if (rooms[i].first <= start) {\n selectedRoom = i;\n break;\n } else if (rooms[i].first < earliestAvailableTime) {\n earliestAvailableTime = rooms[i].first;\n selectedRoom = i;\n }\n }\n\n // If the room is available at the start time, book it immediately\n if (rooms[selectedRoom].first <= start) {\n rooms[selectedRoom].first = end;\n } \n // Otherwise, book it at the earliest available time\n else {\n rooms[selectedRoom].first += (end - start);\n }\n rooms[selectedRoom].second++; // Increment the booking count\n }\n\n // Find the room with the maximum bookings\n int maxBookings = -1, result = -1;\n for (int i = 0; i < n; ++i) {\n if (rooms[i].second > maxBookings || (rooms[i].second == maxBookings && i < result)) {\n maxBookings = rooms[i].second;\n result = i;\n }\n }\n\n return result;\n }\n};\n",
"memory": "138043"
} |
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 vector<vector<ll>>v;\n for(int i=0;i<meetings.size();i++){\n v.push_back({meetings[i][0],meetings[i][1],meetings[i][1]-meetings[i][0]});\n }\n sort(v.begin(),v.end());\n vector<int>freq(n,0);\n set<int>lowest;\n for(int i=0;i<n;i++)\n lowest.insert(i);\n priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>>pq;\n for(int i=0;i<v.size();i++){\n ll room = INT_MAX;\n while(!pq.empty() && pq.top().first<=v[i][0]){\n room = min(room,pq.top().second);\n lowest.insert(pq.top().second);\n pq.pop();\n }\n if(room==INT_MAX){\n if(pq.size()>=n){\n auto t = pq.top();\n pq.pop();\n pq.push({t.first+v[i][2],t.second});\n freq[t.second]++;\n }\n else{\n int k = *(lowest.begin());\n pq.push({v[i][1],k});\n freq[k]++;\n lowest.erase(k);\n }\n }\n else{\n room = *(lowest.begin());\n pq.push({v[i][1],room});\n freq[room]++;\n lowest.erase(room);\n }\n }\n int m = INT_MIN;\n for(int i=0;i<n;i++){\n m = max(m,freq[i]);\n }\n for(int i=0;i<n;i++){\n if(m==freq[i])\n return i;\n }\n return 0;\n }\n};",
"memory": "138706"
} |
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(begin(meetings), end(meetings));\n vector<int> cnt(n, 0);\n using pii = pair<long long, int>;\n multiset<pii> q;\n set<int> free;\n for(int i = 0; i < n; ++i) free.insert(i);\n long long cur = 0;\n for(auto& m: meetings) {\n int start = m[0], end = m[1];\n while(q.size() && q.begin()->first <= start) {\n free.insert(q.begin()->second);\n q.erase(q.begin());\n }\n cur = start;\n if(free.empty()) {\n free.insert(q.begin()->second);\n cur = q.begin()->first;\n q.erase(q.begin());\n }\n int room = *free.begin();\n q.insert({end - start + cur, room});\n free.erase(free.begin());\n cnt[room]++;\n }\n int max_num = 0;\n int max_room = 0;\n for(int i = 0; i < n; ++i) {\n if(max_num < cnt[i]) {\n max_num = cnt[i];\n max_room = i;\n }\n }\n return max_room;\n }\n};",
"memory": "139368"
} |
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 set<int> free_room;\n for(int i=0;i<n;i++){\n free_room.insert(i);\n }\n set<pair<long long int,int>> st;\n int i=0;\n long long int time=0;\n sort(nums.begin(),nums.end());\n vector<int> ans(n,0);\n\n while(i<nums.size()){\n while(!st.empty() && (*st.begin()).first<=time){\n free_room.insert((*st.begin()).second);\n st.erase(st.begin());\n }\n if(free_room.empty()){\n time=(*st.begin()).first;\n continue;\n }\n if(time<nums[i][0]){\n time=nums[i][0];\n continue;\n }\n int j=*free_room.begin();\n ans[j]++;\n free_room.erase(j);\n long long int dur=1ll*(nums[i][1]-nums[i][0]);\n st.insert({time+dur,j});\n i++;\n }\n int val=-1;\n int cnt=0;\n for(int i=0;i<n;i++){\n if(cnt<ans[i]){\n cnt=ans[i];\n val=i;\n }\n }\n return val;\n }\n};",
"memory": "140031"
} |
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 static bool cmp(vector<int> &a,vector<int>&b){\n if(a[0] == b[0]){\n return a[1]<b[1];\n }\n return a[0]<b[0];\n }\n\n\n int mostBooked(int n, vector<vector<int>>& meetings) {\n sort(meetings.begin(),meetings.end());\n\n set<int> roomsAvailable;\n\n for(int i=0;i<n;i++){\n roomsAvailable.insert(i);\n }\n\n set<pair<long long,int>> bookingfinish;\n\n vector<long long> roomsBookingCnt(n,0);\n\n for(auto &m:meetings){\n long long start = m[0];\n long long end= m[1];\n\n //first empty the rooms whose time is finished\n auto it = bookingfinish.begin();\n while(it != bookingfinish.end() && it->first <= start){\n int room = it->second;\n roomsAvailable.insert(room);\n bookingfinish.erase(it);\n it = bookingfinish.begin();\n }\n\n if(roomsAvailable.begin() == roomsAvailable.end()){\n auto bookingPtr = bookingfinish.begin();\n long long finishTime = bookingPtr->first;\n end = (end-start) + finishTime;\n int room = bookingPtr->second;\n roomsAvailable.insert(room);\n bookingfinish.erase(bookingPtr);\n }\n\n auto roomPtr = roomsAvailable.begin();\n int room = (*roomPtr);\n roomsAvailable.erase(roomPtr);\n roomsBookingCnt[room]++;\n bookingfinish.insert({end , room});\n }\n\n int maxMeetings = 0;\n int ansRoom = 0;\n for(int i=0;i<n;i++){\n if(roomsBookingCnt[i] > maxMeetings){\n ansRoom = i;\n maxMeetings = roomsBookingCnt[i];\n }\n cout<<i<<\" \"<<roomsBookingCnt[i]<<endl;\n }\n return ansRoom;\n }\n};",
"memory": "140693"
} |
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 map<long long,long long> counts;\n set<pair<long long,long long>> times;\n set<long long> rooms;\n sort(meetings.begin(), meetings.end());\n \n for(int i=0;i<n;i++)rooms.insert(i);\n \n for(int i=0;i<meetings.size();i++)\n {\n long long start = meetings[i][0];\n long long end = meetings[i][1];\n \n long long finishtime = 0;\n\n while(times.size() && (*times.begin()).first <= start)\n {\n auto firsttime = *times.begin();\n long long room = firsttime.second;\n \n times.erase(times.begin());\n rooms.insert(room);\n }\n if(!rooms.size())\n {\n auto firsttime = *times.begin();\n long long room = firsttime.second;\n \n times.erase(times.begin());\n rooms.insert(room);\n finishtime = firsttime.first;\n }\n // cout<<\"Here\"<<' '<<i<<' '<<start<<' '<<end<<endl;\n // for(auto i:rooms)cout<<i<<' ';cout<<endl;\n if(rooms.size())\n {\n long long room = *rooms.begin();\n rooms.erase(rooms.begin());\n \n counts[room]++;\n if(finishtime > 0)\n times.insert({finishtime + end - start, room});\n else times.insert({end, room});\n }\n // cout<<\"I\"<<' '<<i<<endl;\n // for(auto i:rooms)cout<<i<<' ';cout<<endl;\n // for(auto i:times) cout<<i.first<<' '<<i.second<<endl;\n }\n long long ans = 0;\n long long mcount = LLONG_MIN;\n for(auto [a,b]:counts)\n {\n if(b > mcount)\n {\n mcount = b;\n ans = a;\n }\n }\n return ans;\n }\n};",
"memory": "141356"
} |
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> counter(n,0);\n \n sort(meetings.begin(), meetings.end());\n queue<vector<int>> meetingQ;\n for(auto x : meetings){\n meetingQ.push(x);\n }\n\n priority_queue<int, vector<int>, greater<int>> unoccupiedRooms;\n for(int i = 0; i < n; i++){\n unoccupiedRooms.push(i);\n }\n\n priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> occupiedRooms;\n\n while(!meetingQ.empty()){\n auto meeting = meetingQ.front();\n meetingQ.pop();\n\n int startTime = meeting[0];\n int endTime = meeting[1];\n while(!occupiedRooms.empty() && startTime >= occupiedRooms.top().first){\n auto o = occupiedRooms.top();\n unoccupiedRooms.push(o.second);\n occupiedRooms.pop();\n }\n\n if(!unoccupiedRooms.empty()){\n int u = unoccupiedRooms.top();\n unoccupiedRooms.pop();\n counter[u]++;\n occupiedRooms.push({endTime,u});\n } else {\n auto o = occupiedRooms.top();\n occupiedRooms.pop();\n int room = o.second;\n long long at = o.first;\n occupiedRooms.push({at + (endTime - startTime),room});\n counter[room]++;\n }\n }\n\n int maxRoom = 0;\n for(int i = 1; i < n; i++){\n if(counter[i] > counter[maxRoom]){\n maxRoom = i;\n }\n }\n\n return maxRoom;\n }\n};",
"memory": "142018"
} |
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> counter(n,0);\n \n sort(meetings.begin(), meetings.end());\n queue<vector<int>> meetingQ;\n for(auto x : meetings){\n meetingQ.push(x);\n }\n\n priority_queue<int, vector<int>, greater<int>> unoccupiedRooms;\n for(int i = 0; i < n; i++){\n unoccupiedRooms.push(i);\n }\n\n priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> occupiedRooms;\n\n while(!meetingQ.empty()){\n auto meeting = meetingQ.front();\n meetingQ.pop();\n\n int startTime = meeting[0];\n int endTime = meeting[1];\n while(!occupiedRooms.empty() && startTime >= occupiedRooms.top().first){\n auto o = occupiedRooms.top();\n unoccupiedRooms.push(o.second);\n occupiedRooms.pop();\n }\n\n if(!unoccupiedRooms.empty()){\n int u = unoccupiedRooms.top();\n unoccupiedRooms.pop();\n counter[u]++;\n occupiedRooms.push({endTime,u});\n } else {\n auto o = occupiedRooms.top();\n occupiedRooms.pop();\n int room = o.second;\n long long at = o.first;\n occupiedRooms.push({at + (endTime - startTime),room});\n counter[room]++;\n }\n }\n\n int maxRoom = 0;\n for(int i = 1; i < n; i++){\n if(counter[i] > counter[maxRoom]){\n maxRoom = i;\n }\n }\n\n return maxRoom;\n }\n};",
"memory": "142681"
} |
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 class Compare\n{\npublic:\n bool operator() (std::vector<long>& d, std::vector<long>& f)\n {\n if (d[1] > f[1]) {\n return true;\n }\n else if (d[1] == f[1]\n && d[2] > f[2]) {\n return true;\n }\n\n return false;\n }\n};\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n std::sort(meetings.begin(), meetings.end(),\n [](const std::vector<int>& a, const std::vector<int>& b) {\n return a[0] < b[0];\n });\n std::unordered_map<int, int> count;\n std::priority_queue<std::vector<long>, std::vector<std::vector<long>>, Compare> pq;\n for (int i = 0; i < n; i++) {\n pq.push({0, 0, i});\n }\n\n for (int i = 0; i < meetings.size(); i++) {\n std::vector<long> putback;\n while (!pq.empty() && pq.top()[1] <= meetings[i][0]) {\n putback.push_back(pq.top()[2]);\n pq.pop();\n }\n for (int j = 0; j < putback.size(); j++) {\n pq.push({0, 0, putback[j]});\n }\n std::vector<long> f = pq.top();\n pq.pop();\n count[f[2]]++;\n long start = f[1];\n long end = f[1] + meetings[i][1] - meetings[i][0];\n if (meetings[i][0] > f[1]) {\n end += meetings[i][0];\n }\n pq.push({start, end, f[2]});\n }\n\n int max = 0;\n int num = -1;\n for (int i = 0; i < n; i++) {\n if (count[i] > max) {\n max = count[i];\n num = i;\n }\n }\n\n return num;\n }\n};",
"memory": "143343"
} |
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 {\n // treat room as elements in pq\n // Step 1: sort meetings in terms of start time\n sort(meetings.begin(), meetings.end());\n int meeting_index = 0;\n\n // Step 2: establish n rooms\n // {end time, room_id}\n priority_queue<vector<long long>, vector<vector<long long>>, greater<vector<long long>>> rooms;\n for(int i =0; i < n ; i++)\n {\n rooms.push({0, i});\n }\n \n // Step 3:\n vector<int> count(n, 0);\n int time_shift = 0;\n while(meeting_index < (int)meetings.size())\n {\n while(rooms.top()[0] + time_shift >= meetings[meeting_index][0])\n {\n // assign meeting_index to pq\n vector<long long> to_pop = rooms.top();\n int room_id = to_pop[1];\n int duration = meetings[meeting_index][1] - meetings[meeting_index][0];\n vector<long long> to_push = {to_pop[0] + duration, room_id };\n\n // pop\n rooms.pop();\n rooms.push(to_push);\n\n // count\n count[room_id]++;\n\n // increment meeting_index\n meeting_index++;\n\n if(meeting_index == meetings.size())\n {\n break;\n }\n }\n \n // must update time_shift\n if(meeting_index < (int)meetings.size())\n {\n vector<long long> release = rooms.top();\n rooms.pop();\n release[0] = meetings[meeting_index][0];\n rooms.push(release);\n }\n }\n\n // Step 4: find the room with max count\n int return_room = 0;\n int max_count = 0;\n for(int i = 0; i<n;i++)\n {\n if(count[i] > max_count)\n {\n return_room = i;\n max_count = count[i];\n }\n }\n\n return return_room;\n }\n};",
"memory": "144006"
} |
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 ComparisonClass {\npublic:\n bool operator() (vector<ll> &v1, vector<ll> &v2){\n if(v1[0]==v2[0]){\n return v1[1]>=v2[1];\n }\n return v1[0]>=v2[0];\n }\n};\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n vector<ll> vec(n,0);\n priority_queue<vector<ll>,vector<vector<ll>>,ComparisonClass> pq;\n for (int i=0;i<n;i++){\n pq.push({0,i});\n }\n\n sort(meetings.begin(), meetings.end());\n\n for (auto i : meetings){\n while(pq.top()[0]<i[0]){\n auto e = pq.top();\n pq.pop();\n pq.push({i[0],e[1]});\n }\n auto e = pq.top();\n pq.pop();\n ll room = e[1];\n ll et = e[0];\n ll newEt = i[1];\n // cout<<room<<\" \"<<et<<\" \"<<\" \"<<i[0]<<\" \"<<i[1]<<endl;\n if(et>i[0]){\n newEt = et + (i[1]-i[0]);\n }\n vec[room] += 1;\n pq.push({newEt, room});\n }\n\n\n ll mx = 0;\n ll ans = 0;\n for (int i=0;i<n;i++){\n if(mx<vec[i]){\n mx = vec[i];\n ans = i;\n }\n }\n return ans;\n }\n};",
"memory": "144668"
} |
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#define vi vector<ll>\n#define vvi vector<vi>\n\nclass Solution {\npublic:\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<ll, vi, greater<ll>>room_set;\n priority_queue<vi, vvi, greater<vi>>rejoin_set;\n for(int i=0;i<n;i++) room_set.push(i);\n\n auto f = [](auto &a, auto &b) {\n return a[0] < b[0];\n };\n sort(meetings.begin(), meetings.end(), f);\n int x = 0;\n vi cnt(n, 0);\n for(const auto m:meetings) {\n int s = m[0], e=m[1];\n while(!rejoin_set.empty()) {\n vi curr = rejoin_set.top();\n int finish = curr[0], room = curr[1];\n if(finish <= s) {\n room_set.push(room);\n rejoin_set.pop();\n } else {\n break;\n }\n }\n if(room_set.empty()) {\n vi best = rejoin_set.top();\n int room = best[1], avail = best[0];\n rejoin_set.pop();\n rejoin_set.push({(ll)avail+e-s, room});\n cnt[room]++;\n if(cnt[x] < cnt[room]) {\n x = room;\n } else if (cnt[x]==cnt[room]) x = min(x, room);\n continue;\n }\n int room = room_set.top();\n room_set.pop();\n cnt[room]++;\n if(cnt[x] < cnt[room]) {\n x = room;\n } else if (cnt[x]==cnt[room]) x = min(x, room);\n rejoin_set.push({e, room});\n }\n return x;\n }\n};",
"memory": "145331"
} |
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()(vector<long long>& a, vector<long long>& b) {\n if (a[0] == b[0]) {\n return a[1] > b[1];\n } else {\n return a[0] > b[0];\n }\n }\n };\n\n int mostBooked(int n, vector<vector<int>>& meetings) {\n priority_queue<vector<long long>, vector<vector<long long>>, Compare> pq;\n for (int i = 0; i < n; i++) {\n pq.push({0, i, 0});\n }\n \n sort(meetings.begin(), meetings.end());\n\n for (vector<int>& m : meetings) {\n while (pq.top()[0] < m[0]) {\n vector<long long> curr = pq.top();\n pq.pop();\n curr[0] = m[0];\n pq.push(curr);\n }\n vector<long long> curr = pq.top();\n // cout << curr[1] << \"\\n\";\n pq.pop();\n curr[0] += m[1] - m[0];\n curr[2]++;\n pq.push(curr);\n }\n\n int res = 0;\n int most = 0;\n while (pq.size()) {\n vector<long long> curr = pq.top();\n pq.pop();\n if (curr[2] == most && curr[1] < res) {\n res = curr[1];\n } else if (curr[2] > most) {\n most = curr[2];\n res = curr[1];\n }\n }\n return res;\n }\n};",
"memory": "145993"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n\tint partitionString(string& s) {\n\t\tint count = 0;\n\n\t\tbool used[26];\n\t\tfor (char c : s)\n\t\t{\n\t\t\tint index = c - 'a'; // Normalize to 0-26\n\t\t\tif (used[index])\n\t\t\t{\n\t\t\t\tmemset(used, 0, sizeof(used));\n\t\t\t\tcount++;\n\t\t\t}\n\t\t\t\n\t\t\tused[index] = true;\n\t\t}\n\n\t\treturn count += 1; // For ending strings\n\t}\n};",
"memory": "11106"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int partitionString(string& s) {\n int mask = 0;\n int count = 0;\n for (char c : s) {\n int bit = 1 << c - 'a';\n if (mask & bit) {\n count++;\n mask = 0;\n }\n mask |= bit;\n }\n return count + 1;\n }\n};",
"memory": "11106"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<int> freq(26);\n int substrings = 1;\n for( int i = 0, j = 0; j < s.size(); j++ ){\n if ( freq[s[j] - 'a'] ){\n ++substrings;\n while( i < j) --freq[s[i++] - 'a'] ;\n }\n ++freq[s[j] - 'a'] ;\n }\n return substrings;\n }\n};",
"memory": "11920"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n // unordered_map <char, int> mp;\n // int count = 1;\n // int start = 0;\n // for(int i=0; i<s.length(); i++)\n // {\n // if(mp.find(s[i]) != mp.end())\n // {\n // if(mp[s[i]] >= start)\n // {\n // start = i;\n // mp[s[i]] = i;\n // count ++;\n // }\n\n // }\n\n \n // mp[s[i]] = i;\n\n // }\n\n // return count;\n\n\n// 2nd apporach \n vector<int> lastSeen(26, -1);\n \n int count = 1;\n int substringStart = 0;\n\n for (int i = 0; i < s.length(); i++) {\n if (lastSeen[s[i] - 'a'] >= substringStart) {\n count++;\n substringStart = i;\n }\n lastSeen[s[i] - 'a'] = i;\n }\n\n return count;\n\n\n\n\n\n\n\n }\n};",
"memory": "11920"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_map<char, int> mp; // Create an unordered map to keep track of character counts\n int i = 0, j = 0, ans = 1, n = s.size(); // Initialize i and j as pointers, ans as the result, and n as the size of the string\n while (j < n) // Loop through the string using j as the right pointer\n {\n mp[s[j]]++; // Increment the count of the current character s[j] in the map\n if (mp[s[j]] > 1) // If the character appears more than once (duplicate found)\n {\n while (i < j) mp[s[i++]]--; // Move the left pointer i until the duplicate is removed (i.e., making the substring unique)\n ans++; // Increment the number of partitions\n }\n j++; // Move the right pointer j to check the next character\n }\n return ans; \n }\n};",
"memory": "12734"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) \n {\n unordered_map<char, int> mp; \n int i = 0, j = 0, ans = 1, n = s.size(); \n while (j < n) \n {\n mp[s[j]]++; \n if (mp[s[j]] > 1) \n {\n while (i < j) mp[s[i++]]--; \n ans++; \n }\n j++; \n }\n return ans; \n }\n};",
"memory": "12734"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<bool> a(26,0);\n auto b=a;\n int c=0;\n for(int i=0; i<s.size(); i++)\n {\n if(a[s[i]-'a'])\n {\n c++;\n a=b;\n }\n a[s[i]-'a']=1;\n }\n return c+1;\n\n }\n};",
"memory": "13548"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) \n {\n unordered_set<int> set(s.begin(), s.end());\n\n if(set.size() == 1) return s.length();\n if(set.size() == s.length()) return 1;\n\n cout << \"NOT RETURNED \\n\";\n\n int ps = 1;\n vector<int> h(26, 0);\n\n for(auto i: s){\n if(h[i - 'a'] > 0){\n ps++;\n h.assign(26, 0); // reset\n }\n h[i - 'a']++;\n }\n\n return ps;\n }\n};",
"memory": "14361"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "/*\nInput: s = \"abacaba\"\nOutput: 4\nExplanation:\nTwo possible partitions are (\"a\",\"ba\",\"cab\",\"a\") and (\"ab\",\"a\",\"ca\",\"ba\").\nIt can be shown that 4 is the minimum number of substrings needed.\n\ns = abacaba\nidx = 4\nletters = [26 * false] = {\"a\" : true, \"b\" : false, \"c\" : false}\nletters = 0\npartitions = 4\n\nInput: s = \"abacaba\"\nOutput: 4\nExplanation:\nTwo possible partitions are (\"a\",\"ba\",\"cab\",\"a\") and (\"ab\",\"a\",\"ca\",\"ba\").\nIt can be shown that 4 is the minimum number of substrings needed.\n\ns = abacaba\nidx = 0\n\ndp = [1, ]\n\n\nObservations:\nTwo max possible partitions if you use the string s and reverse the string s.\n\nApproach:\n\nApproach #1: Greedy.\nInitialize number of partitions to 1.\nIterate through string.\n Keep unsigned int mask.\n If character at mask bit exists, then increment partition and reset mask.\nDo same operation reversed.\nReturn min of both sets of partitions.\n\n*/\nclass Solution {\npublic:\n bool checkLettersMask(int mask, int c) {\n return mask & (1 << (c - 'a'));\n }\n int updateLettersMask(int mask, int c) {\n return mask | (1 << (c - 'a'));\n }\n int partitionStringOp(string s) {\n int partitions = 1;\n int mask = 0;\n for (int i=0;i<s.size();i++) {\n if (checkLettersMask(mask, s[i])) {\n partitions++;\n mask = 0;\n }\n mask = updateLettersMask(mask, s[i]);\n }\n return partitions;\n }\n int partitionString(string s) {\n string r = s;\n reverse(r.begin(), r.end());\n return min(partitionStringOp(s), partitionStringOp(r)); \n }\n};",
"memory": "14361"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<int> dp(s.size() + 1, 0);\n string curr = \"\";\n for (int i = 1; i <= s.size(); i++) {\n if (find(curr.begin(), curr.end(), s[i - 1]) != curr.end()) {\n curr = s[i - 1];\n dp[i] = 1 + dp[i - 1];\n } else {\n curr = curr + s[i - 1];\n dp[i] = dp[i - 1];\n }\n }\n return dp[s.size()] + 1;\n }\n};",
"memory": "15175"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n std::unordered_map<char,std::stack<int>> indices;\n for(int i=s.size()-1;i>=0;--i){\n indices[s[i]].push(i);\n }\n int m = 0;\n int count = 0;\n for(int i=0;i<s.size();++i){\n if(i==m){\n count++;\n m = std::numeric_limits<int>::max();\n }\n if(indices[s[i]].size()>1){\n indices[s[i]].pop();\n // check the next one\n m = min(indices[s[i]].top(),m);\n }\n //std::cout << s[i] << \", \" << m << std::endl;\n }\n return count;\n }\n};",
"memory": "15989"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int n = s.size();\n vector<int>vec(n,0);\n int head = -1;\n int tail =0 ;\n vector<int>cnt(26,0);\n\n while(tail < n){\n while(head+1 < n && cnt[s[head+1]-'a'] == 0){\n head++;\n cnt[s[head]-'a']++;\n }\n \n // cout<<tail<<\" \"<<head<<endl;\n\n vec[tail] = head ;\n\n if(head < tail){\n tail++;\n head = tail-1;\n }\n else{\n cnt[s[tail]-'a']--;\n tail++;\n }\n }\n\n int ans = 0 ;\n\n vector<int>dp(n+1,0);\n dp[n] = 0;\n \n // for(int i =0 ; i<n ;i++){\n // cout<<vec[i]<<\" \";\n // }\n // cout<<endl;\n\n for(int i = n-1; i>=0 ; i--){ \n dp[i] = 1 + dp[vec[i]+1]; \n }\n \n return dp[0];\n }\n};",
"memory": "16803"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(const string& s) {\n int out = 0;\n for(int i = 0; i < s.size();) {\n out++;\n vector<bool> a(24, 0);\n for(; i < s.size() && !a[s[i] - 'a']; i++) {\n a[s[i] - 'a'] = true;\n }\n }\n return out;\n }\n};",
"memory": "17616"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n int partitionString(const string &s) {\n int ans = 0;\n int i = 0;\n while(i != s.size()){\n vector<bool> seen(26,false);\n for(; i < s.size(); ++i){\n int idx = s[i]-'a';\n if(seen[idx]){\n break;\n }\n seen[idx] = true;\n }\n ++ans;\n }\n return ans;\n }\n};",
"memory": "18430"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n \n int partitionString(const string &s) {\n int ans = 0;\n int i = 0;\n while(i != s.size()){\n vector<bool> seen(26,false);\n for(; i < s.size(); ++i){\n int idx = s[i]-'a';\n if(seen[idx]){\n break;\n }\n seen[idx] = true;\n }\n ++ans;\n }\n return ans;\n }\n};",
"memory": "19244"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int ind=0;\n int ans=0;\n while(ind<s.size())\n {\n vector<bool>visited(26,false);\n while(ind<s.size() && !visited[s[ind]-'a'])\n {\n visited[s[ind++]-'a']=true;\n }\n ans++;\n }\n return ans;\n }\n};",
"memory": "20058"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int subStringsCount = 0;\n for (int i = 0; i < size(s); ) {\n vector<bool> v(26,false);\n while(i < size(s) and v[s[i] - 'a'] == false) {\n v[s[i] - 'a'] = true;\n i++;\n }\n subStringsCount++;\n }\n return subStringsCount;\n }\n};",
"memory": "20871"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int res = 0;\n for(int i = 0, j; i < s.size(); ) {\n j = i;\n vector<bool> f(26, false);\n while(j < s.size()) {\n int ch = s[j] - 'a';\n if(f[ch]) break;\n f[ch] = true;\n ++j;\n }\n i = j;\n ++res;\n }\n return res;\n }\n};",
"memory": "21685"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<bool> contained = vector<bool>(26, 0);\n int ss_count = 1;\n for (int i = 0; i < s.size(); i++) {\n if (contained[s[i]-'a']) {\n ss_count++;\n contained = vector<bool>(26, 0);\n }\n contained[s[i]-'a'] = true;\n }\n return ss_count;\n }\n};",
"memory": "22499"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int ans = 0;\n int i =0;\n while(i < s.size())\n {\n vector<bool> track(26,false);\n while(i<s.size() && track[s[i]-'a'] != true)\n {\n track[s[i]-'a'] = true;\n i++;\n }\n ans++;\n }\n return ans;\n }\n};",
"memory": "22499"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int ans = 0;\n int n = s.length();\n vector<bool> m(26,false);\n for(auto i = 0 ; i < n ; i ++){\n if(m[s[i] - 'a']){\n ans ++;\n vector<bool> v(26,false);\n m = v;\n }\n m[s[i] -'a'] = 1;\n }\n return ans + 1;\n }\n};",
"memory": "23313"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int ans = 1;\n int n = s.length();\n vector<bool> m(26,false);\n for(auto i = 0 ; i < n ; i ++){\n if(m[s[i] - 'a']){\n ans ++;\n vector<bool> v(26,false);\n m = v;\n }\n m[s[i] -'a'] = 1;\n }\n return ans;\n }\n};",
"memory": "23313"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int out = 0;\n for(int i = 0; i < s.size();) {\n out++;\n vector<bool> a(256, 0);\n for(; i < s.size() && !a[s[i]]; i++) {\n a[s[i]] = true;\n }\n }\n return out;\n }\n};",
"memory": "24126"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n Solution(){\n ios_base :: sync_with_stdio(false);\n }\n int partitionString(string s) {\n int index = 0;\n int n = s.size();\n int ans = 0;\n while(index < n){\n ans++;\n vector<bool>visited(256,0);\n while(index < n && !visited[s[index]]){\n visited[s[index++]] = true;\n }\n }\n return ans;\n }\n};",
"memory": "24940"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int n = s.size(), l = 0, r = 0, cntUnique = 0;\n vector<int> f(128);\n unordered_set<int> res;\n while (r < n) {\n f[s[r]]++;\n if (f[s[r]] == 1) {\n cntUnique++;\n }\n bool newGroup = false;\n if (r - l + 1 > cntUnique) {\n while (l < r) {\n f[s[l]]--;\n l++;\n }\n cntUnique = 1;\n }\n res.insert(l);\n r++;\n }\n return res.size();\n }\n};",
"memory": "25754"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n std::list<std::string> l;\n std::string currentString;\n int pos = 0;\n for (auto i : s) {\n if (currentString.find(i) == std::string::npos) {\n currentString += i;\n } else {\n l.push_back(currentString);\n currentString.erase();\n currentString = i;\n }\n }\n return l.size() + 1;\n }\n};",
"memory": "26568"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint partitionString(std::string s) {\n std::unordered_set<std::string> store;\n std::string temp;\n auto counter = 0;\n auto result = 0;\n while (counter < s.length()) {\n bool arr[26]={0};\n auto j=counter;\n for (;j<s.length(); ++j) {\n auto ch = s[j]; \n temp.push_back(ch);\n //std::cout << \"ch : \" << ch << \"|temp:\" << temp << \"|counter \" << counter << std::endl;\n if (arr[ch-'a']) {\n temp.pop_back();\n if (!temp.empty()) {\n //std::cout << \"Found : \" << temp << std::endl;\n result++;\n store.emplace(temp); \n temp.clear();\n }\n break;\n }\n arr[ch-'a'] = true;\n } \n counter = j;\n }\n \n if (!temp.empty()) {\n result++;\n }\n //std::cout << \"Result : \" << result << std::endl; \n return result; \n }\n};",
"memory": "27381"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n string temp=\"\";\n vector<string>ans;\n for(int i=0;i<s.size();i++){\n if(temp.find(s[i])!=string::npos){\n ans.push_back(temp);\n temp=\"\";\n }\n temp+=s[i];\n }\n ans.push_back(temp);\n return ans.size();\n }\n};",
"memory": "28195"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n bool searchInCharSet(char &ch, string &str){\n int i=0;\n while(i < str.size()){\n if(str[i]==ch) return true;\n i++;\n } \n return false;\n }\n\n int partitionString(string s) {\n int n=s.size();\n // unordered_set<string> container;\n vector<string> container;\n // unordered_set<char> st;\n string str=\"\";\n\n for(int i=0;i<n;i++){\n if(searchInCharSet(s[i],str)){\n // container.insert(str);\n container.push_back(str);\n str.erase();\n // st.clear();\n }\n // st.insert(s[i]);\n str.push_back(s[i]);\n if(i == n-1) container.push_back(str);\n }\n return container.size();\n }\n};",
"memory": "29009"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n bool present[26];\n for(int i = 0; i < 26; i++)\n present[i] = false;\n int k = 0;\n vector<string> words;\n string word = \"\";\n while(k < s.size()) {\n if(present[s[k] - 'a']) {\n words.push_back(word);\n word = \"\";\n for(int i = 0; i < 26; i++)\n present[i] = false;\n }\n word.push_back(s[k]);\n present[s[k] - 'a'] = true;\n k++;\n }\n words.push_back(word);\n return words.size();\n }\n};",
"memory": "29823"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint partitionString(string s) {\n vector<string> ans;\n string a=\"\";\n unordered_map<char,int> mp;\n int i=0,j=0;\n while(i < s.length() && j < s.length())\n {\n mp[s[j]]++;\n if(mp[s[j]] > 1)\n {\n for (int k = i; k < j; k++)\n {\n a.push_back(s[k]);\n mp[s[k]]--;\n }\n ans.push_back(a);\n a=\"\";\n i=j;\n j++;\n }\n else\n j++;\n }\n if (!a.empty() || i < s.length()) {\n a = s.substr(i);\n ans.push_back(a);\n }\n return ans.size();\n }\n};",
"memory": "30636"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n map<char, int> mp;\n vector<string> vs;\n string tmp = \"\";\n for (auto x : s) {\n mp[x]++;\n if (mp[x] > 1) {\n vs.push_back(tmp);\n for (auto y : tmp) mp[y] = 0;\n mp[x]++;\n tmp = \"\";\n }\n tmp += x;\n }\n if (!tmp.empty()) vs.push_back(tmp);\n return vs.size();\n }\n};",
"memory": "31450"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<vector<char>> freq;\n vector<char> temp;\n for(int i = 0; i < s.length(); i++) {\n if(find(temp.begin(), temp.end(), s[i]) == temp.end()) {\n temp.push_back(s[i]);\n }\n else {\n freq.push_back(temp);\n temp.clear();\n temp.push_back(s[i]);\n }\n }\n return freq.size() + 1;\n }\n\n};",
"memory": "32264"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<vector<char>> freq;\n vector<char> temp;\n for(int i = 0; i < s.length(); i++) {\n if(find(temp.begin(), temp.end(), s[i]) == temp.end()) {\n temp.push_back(s[i]);\n }\n else {\n freq.push_back(temp);\n temp.clear();\n temp.push_back(s[i]);\n }\n }\n return freq.size() + 1;\n }\n\n};",
"memory": "33078"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n vector<vector<char>> freq;\n vector<char> temp;\n for(int i = 0; i < s.length(); i++) {\n if(find(temp.begin(), temp.end(), s[i]) == temp.end()) {\n temp.push_back(s[i]);\n }\n else {\n freq.push_back(temp);\n temp.clear();\n temp.push_back(s[i]);\n }\n }\n return freq.size() + 1;\n }\n\n};",
"memory": "33078"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_map<char,int>m;\n int i=0,j=0;int n=s.size();\n int cnt=1;\n while(j<n){\n m[s[j]]++;\n if(m[s[j]]>1){\n while(i<j){\n m[s[i]]--;\n if(m[s[i]]==0)m.erase(s[i]);\n i++;\n }\n cnt++;\n } \n j++;\n }\n // if(i==n)cnt++;\n return cnt;\n }\n};",
"memory": "33891"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int i=0,j=0,n=s.size(),ans=1;\n unordered_map<char,int> mpp;\n while(j<n){\n mpp[s[j]]++;\n if(mpp[s[j]]>1){\n ans++;\n while(i<j){\n mpp[s[i]]--;\n if(mpp[s[i]]==0)mpp.erase(s[i]);\n i++;\n }\n }\n j++;\n }\n return ans;\n }\n};",
"memory": "34705"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> seen;\n int answer = 1;\n\n for ( char c : s ) {\n if ( seen.find(c) != seen.end() ) {\n seen = {c};\n answer += 1;\n }\n else {\n seen.insert(c);\n }\n }\n\n return answer;\n }\n};",
"memory": "35519"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> seen_chars = {};\n int num_sub_str = 1;\n for (char c: s) {\n if (seen_chars.find(c) != seen_chars.end()) {\n num_sub_str += 1;\n seen_chars = {c};\n } else {\n seen_chars.insert(c);\n }\n } \n return num_sub_str;\n }\n};\n\n",
"memory": "36333"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int saiz(const vector<string>& v){\n int n = v.size();\n int c=0;\n for(int i=0;i<n;++i){\n if(!v[i].empty()) c++;\n }\n return c;\n }\n bool IsIn(const string& s, char c){\n int n = s.size();\n for(int i = 0; i<n; ++i){\n if(c==s[i]) return true;\n }\n return false;\n }\n int partitionString(string s) {\n int n = s.size();\n vector<string> v(n);\n int c=0;\n for(int i=0;i<n;++i){\n if(!IsIn(v[c],s[i])) v[c] += s[i];\n else{ c++; v[c]+=s[i];}\n }\n return saiz(v);\n }\n};",
"memory": "37146"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(const string& s) {\n unordered_set<char> uniqueChars;\n int count = 1; // Initialize count to 1, since we'll always have at least one substring\n\n for (char c : s) {\n if (!uniqueChars.insert(c).second) {\n // Duplicate character found, slide the window to the right\n uniqueChars.clear();\n uniqueChars.insert(c);\n count++;\n }\n }\n\n return count;\n}\n\n\n\n};",
"memory": "37960"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> set;\n int size = s.length();\n int partition = 0;\n for(int i = 0; i < size; i++) {\n int be_size = set.size();\n set.insert(s[i]);\n int af_size = set.size();\n if(be_size == af_size) { \n partition++;\n set.clear();\n set.insert(s[i]);\n }\n }\n if(set.size() != 0) {\n partition++;\n }\n return partition;\n }\n};",
"memory": "38774"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> setS;\n int count = 0;\n for (int i = 0; i < s.size(); i++)\n {\n if (setS.find(s[i]) != setS.end()){\n count++;\n setS.clear();\n }\n setS.insert(s[i]);\n }\n if (!setS.empty()) {\n count++;\n }\n return count;\n }\n};",
"memory": "39588"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "#include <unordered_set>\n\nclass Solution {\npublic:\n int partitionString(string s) {\n int i = 0;\n\n unordered_set<char> charset;\n int str_count = 0;\n while(i<s.length()) {\n while(i<s.length() && !charset.contains(s[i])) {\n charset.insert(s[i]);\n i++;\n }\n charset.clear();\n str_count++;\n }\n return str_count;\n }\n \n};",
"memory": "40401"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> st;\n int cnt = 1;\n for(int i=0; i<s.size(); i++){\n if(st.find(s[i]) != st.end()){\n cnt++;\n st.clear();\n }\n st.insert(s[i]);\n }\n return cnt;\n }\n};",
"memory": "41215"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> occurence;\n int result = 1;\n for(int i = 0;i<s.size();i++){\n if(occurence.count(s[i])>0){\n result++;\n occurence.clear();\n }\n occurence.insert(s[i]);\n }\n return result;\n }\n};",
"memory": "41215"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int c=0;\n unordered_map<int ,int>map;\n for(int i=0;i< s.size();i++){\n map[s[i]]++;\n if(map[s[i]]==2){\n c++;\n i--;\n\n map.clear();\n }\n }return c+1;\n\n }\n};",
"memory": "42029"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int n = s.size(), ans = 1;\n unordered_set<char> st = {s[0]};\n for (int i = 1; i < n; i++) {\n if (st.find(s[i]) != st.end()) {\n ans++;\n st.clear();\n }\n st.insert(s[i]);\n }\n return ans;\n }\n};",
"memory": "42843"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "#pragma GCC optimize (\"O3,unroll-loops\")\n#pragma GCC target (\"avx2,bmi,bmi2,lzcnt,popcnt\")\nclass Solution {\npublic:\n int partitionString(string s) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(0);\n int count=0;\n unordered_set<char>st;\n for(int i=0;i<s.size();i++){\n \n if(st.find(s[i])==st.end())st.insert(s[i]);\n else {\n count++;\n st.clear();\n st.insert(s[i]);\n }\n }\n return count+1;\n \n }\n};",
"memory": "43656"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<char, bool> check; // Do check.clear()\n string st;\n int n,ans=1;\n // void finder(int index){\n // if(index>=n) return;\n // if(check[st[index]] == true){\n // ans++;\n // check.clear();\n // check[st[index]] = true;\n // }\n // else{\n // check[st[index]] = true;\n // }\n // finder(index+1);\n // }\n\n int partitionString(string s) {\n st = s;\n n = st.size();\n // finder(0);\n for(int i=0;i<n;i++){\n if(check[st[i]] == true){\n ans++;\n check.clear();\n check[st[i]] = true;\n }\n else{\n check[st[i]] = true;\n }\n }\n return ans;\n }\n};",
"memory": "44470"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int subStrings(string s){\n int n = s.size();\n int cnt = 1;\n unordered_map<char, int> ump;\n\n int i=0;\n while(i < n){\n if(ump.find(s[i]) != ump.end()){\n cnt++;\n ump.clear();\n ump[s[i]] = i;\n }\n else{\n ump[s[i]] = i;\n }\n i++;\n }\n\n return cnt;\n }\n\n int partitionString(string s) {\n return subStrings(s);\n }\n};",
"memory": "45284"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<char, bool> check; // Do check.clear()\n string st;\n int n,ans=1;\n void finder(int index){\n if(index>=n) return;\n if(check[st[index]] == true){\n cout<<index<<\" \";\n ans++;\n check.clear();\n check[st[index]] = true;\n }\n else{\n check[st[index]] = true;\n }\n finder(index+1);\n }\n\n int partitionString(string s) {\n st = s;\n n = st.size();\n finder(0);\n cout<<endl;\n return ans;\n }\n};",
"memory": "46098"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n unordered_map<char, bool> check; // Do check.clear()\n string st;\n int n,ans=1;\n void finder(int index){\n if(index>=n) return;\n if(check[st[index]] == true){\n cout<<index<<\" \";\n ans++;\n check.clear();\n check[st[index]] = true;\n }\n else{\n check[st[index]] = true;\n }\n finder(index+1);\n }\n\n int partitionString(string s) {\n st = s;\n n = st.size();\n finder(0);\n cout<<endl;\n return ans;\n }\n};",
"memory": "46098"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> visit;\n int count = 0;\n string curr = \"\";\n for(const auto& i: s){\n if(visit.count(i) == 1){\n visit.clear();\n visit.insert(i);\n count++;\n }else{\n visit.insert(i);\n curr += i;\n }\n }\n return count + 1; \n }\n};",
"memory": "46911"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> visit;\n int count = 0;\n string curr = \"\";\n for(const auto& i: s){\n if(visit.count(i) == 1){\n visit.clear();\n visit.insert(i);\n count++;\n }else{\n visit.insert(i);\n curr += i;\n }\n }\n return count + 1; \n }\n};",
"memory": "46911"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n\n unordered_set<char> seen;\n int count = 0;\n string cur_sub;\n\n for (char c : s) { \n if (seen.find(c) != seen.end()) {\n ++count;\n seen.clear();\n }\n\n seen.insert(c);\n cur_sub += c;\n }\n\n if (!cur_sub.empty()) {\n ++count;\n }\n\n return count;\n\n }\n};",
"memory": "47725"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int j = 0, mincuts = 0;\n unordered_map<char,int> mp;\n\n while(j < s.size())\n {\n mp[s[j]]++;\n while(mp[s[j]] > 1)\n {\n mp.clear();\n mincuts++;\n j--;\n }\n j++;\n }\n return mincuts + 1;\n }\n};",
"memory": "48539"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int isValid(string s)\n {\n unordered_map<char, int> mp;\n for(auto ch : s)\n {\n mp[ch]++;\n if(mp[ch] > 1)\n {\n return false;\n }\n }\n return true;\n }\n\n int solve(int idx, string s)\n {\n if(idx == s.size())\n {\n return 0;\n }\n \n int ans = INT_MAX;\n for(int j = idx; j < s.size(); j++)\n {\n string sub = s.substr(idx, j - idx + 1);\n if(isValid(sub))\n {\n int mincut = 1 + solve(j + 1, s);\n ans = min(ans, mincut);\n }\n }\n return ans;\n }\n\n int partitionString(string s) {\n /* Time Limit Exceeded\n \n return solve(0, s); \n */\n\n unordered_map<char,int> mp;\n int i = 0, j = 0, mincuts = 0;\n\n while(j < s.size())\n {\n mp[s[j]]++;\n while(mp[s[j]] > 1)\n {\n mp.clear();\n mincuts++;\n j--;\n }\n j++;\n }\n return mincuts + 1;\n }\n};",
"memory": "49353"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int ans=1;\n vector<int> cnt(26,0);\n \n for(auto x:s){\n cnt[x-'a']++;\n if(cnt[x-'a']>1){\n cnt=vector<int> (26,0);\n cnt[x-'a']=1;\n ans++;\n }\n \n }\n return ans;\n }\n};",
"memory": "53421"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int cnt = 0;\n int n = s.length();\n for(int i=0;i<n;i++)\n {\n vector<int> freq(26,0);\n while(i < n && freq[s[i]-'a'] == 0)\n {\n freq[s[i]-'a']++;\n i++;\n }\n cnt++;\n i--;\n }\n return cnt;\n }\n};",
"memory": "54235"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int n=s.length();\n int i=0;\n int count=0;\n unordered_map<char,int> mp(n);\n while(i<n){\n if(mp.find(s[i])==mp.end()){\n mp[s[i]]++;\n \n }\n else if(mp.find(s[i])!=mp.end()){\n count++;\n mp.clear();\n mp[s[i]]++;\n }\n i++;\n }\n return count+1;\n }\n};",
"memory": "55049"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int helper(int i, string &s){\n vector<int> fre(26, 0);\n int n = s.length();\n int ans = 0;\n while((i < n) && (fre[s[i]-'a']==0)){\n fre[s[i]-'a']++;\n i++;\n ans++;\n }\n return ans;\n }\n int partitionString(string s) {\n int n = s.length();\n int ans = 0;\n int i = 0;\n while(i < n){\n int len = helper(i, s);\n ans++;\n i += len;\n }\n return ans;\n }\n};",
"memory": "55049"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int counter = 0;\n int index = 0;\n\n while (index < s.size())\n {\n remove_prefix(s, index);\n counter++;\n }\n\n return counter;\n }\n\n // removes the longest prefix w/o repeating chars from s\n void remove_prefix(string & s, int & index)\n {\n vector<int> mappy (26, -1);\n\n while (index < s.size())\n {\n if (mappy[s[index] - 'a'] != -1)\n {\n break;\n }\n mappy[s[index] - 'a'] = index;\n index++;\n }\n }\n};",
"memory": "55863"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_set<char> unique;\n int parts = 1;\n for (const auto& c : s) {\n if (!unique.emplace(c).second) {\n ++parts;\n unique.clear();\n unique.insert(c);\n }\n }\n return s.empty() ? 0 : parts;\n }\n};",
"memory": "55863"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n set<char> cur_pool;\n int n = 1;\n\n for (char ch : s) {\n if (cur_pool.count(ch) > 0) {\n n += 1;\n cur_pool = {ch};\n } else {\n cur_pool.insert(ch);\n }\n }\n\n return n;\n }\n};",
"memory": "56676"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_map<string ,int> mp;\n unordered_set<char> st;\n int len=s.length();\n int i=0;\n while(i<len){\n if(st.count(s[i])==0){\n string temp=\"\";\n while(i<len && st.count(s[i])==0){\n st.insert(s[i]);\n temp+=s[i];\n i++;\n }\n mp[temp]++;\n st.clear();\n temp=\"\";\n }\n }\n auto itr=mp.begin();\n int count=0;\n for(;itr!=mp.end();itr++){\n count+=itr->second;\n }\n return count;\n }\n};",
"memory": "57490"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n unordered_map<string ,int> mp;\n unordered_set<char> st;\n int len=s.length();\n int i=0;\n int count=0;\n while(i<len){\n if(st.count(s[i])==0){\n string temp=\"\";\n while(i<len && st.count(s[i])==0){\n st.insert(s[i]);\n temp+=s[i];\n i++;\n }\n mp[temp]++;\n count++;\n st.clear();\n temp=\"\";\n }\n }\n // auto itr=mp.begin();\n // \n // for(;itr!=mp.end();itr++){\n // count+=itr->second;\n // }\n return count;\n }\n};",
"memory": "58304"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n string partiton = \"\";\n vector<string> ans;\n unordered_set<char> t;\n for(int i = 0; i < s.length(); i++)\n {\n if(t.find(s[i]) != t.end())\n {\n ans.push_back(partiton);\n partiton = s[i];\n t.clear();\n t.insert(s[i]);\n }\n else\n {\n partiton+=s[i];\n t.insert(s[i]);\n }\n }\n return ans.size() + 1;\n }\n};",
"memory": "59118"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n \n std::vector<std::string> partitions;\n std::string substring;\n std::unordered_set<char> distinct_set;\n\n // Iterate through string\n for(std::string::size_type i = 0; i<s.size(); i++) {\n // for(char c : s) {\n char c = s[i];\n\n bool distinct = !distinct_set.contains(c);\n\n // If it is distinct, append to substring\n if(distinct) {\n substring += c;\n distinct_set.insert(c);\n } else {\n // Otherwise, append the current substring to partitions, and create a new substring\n partitions.push_back(substring);\n substring.clear();\n distinct_set.clear();\n substring += c;\n distinct_set.insert(c);\n // if(i == s.size()-1) {\n // partitions.push_back(substring);\n // }\n\n }\n\n\n \n }\n partitions.push_back(substring);\n // Print partition for debug\n std::cout << std::endl;\n for(std::string str : partitions) {\n std::cout << \" \";\n for(char c : str) {\n std::cout << c;\n }\n }\n return partitions.size();\n \n }\n\n\n\n\n\n};",
"memory": "59931"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "#include <vector>\nusing namespace std;\n\nclass Solution {\npublic:\n int inCharacters(char x, vector<char> Characters) {\n for (int i = 0; i < Characters.size(); i++) {\n if (x == Characters[i]) return 1;\n }\n return 0;\n }\n int partitionString(string s) {\n vector<vector<char>> Substrings;\n vector<char> Substring;\n vector<char> Characters;\n\n for (int i = 0; i < s.size(); i++) {\n if (inCharacters(s[i], Characters) == 0) {\n Substring.push_back(s[i]);\n Characters.push_back(s[i]);\n if(i == s.size() - 1) {\n Substrings.push_back(Substring);\n }\n }\n else {\n Substrings.push_back(Substring);\n Substring = {};\n Characters = {};\n Substring.push_back(s[i]);\n Characters.push_back(s[i]);\n if (i == s.size() - 1) {\n Substrings.push_back(Substring);\n }\n }\n }\n return Substrings.size();\n }\n};",
"memory": "60745"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n set<char> st;\n int count=0;\n for(auto c: s){\n if(st.find(c)!=st.end()){\n st.clear();\n count++;\n st.insert(c);\n }\n else st.insert(c);\n }\n return count+1; \n }\n};",
"memory": "61559"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) \n {\n set<char>st;\n int ans=1;\n for(auto a:s){\n if(st.find(a)!=st.end()){\n ans++;\n st.clear();\n }\n st.insert(a);\n }\n return ans;\n }\n};",
"memory": "62373"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "\nclass Solution {\npublic:\n int partitionString(string s) {\n set<char>st;\n int g=1;\n for(int i=0;i<s.size();i++){\n if(st.find(s[i])!=st.end()){\n g++;\n st.clear();\n }\n st.insert(s[i]);\n }\n return g;\n }\n};\n",
"memory": "63186"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n int partitionString(string s) {\n int n=s.size();\n int count=0;\n set<char> st;\n\n for(int i=0;i<n;i++){\n if(st.find(s[i]) != st.end()){\n count++;\n st.clear();\n }\n st.insert(s[i]);\n if(i == n-1) count++;\n }\n return count;\n }\n};",
"memory": "64000"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int n=s.size();\n set<int>container;\n int count=0;\n for(int i=0;i<n;i++)\n {\n if(i==n-1)\n {\n count++;\n }\n if(container.find(s[i])!=container.end())\n {\n count++;\n container.clear();\n }\n container.insert(s[i]);\n \n }\n return count;\n }\n};",
"memory": "64000"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n if(s.size()==1) return 1;\n int ans = 0;\n set<char> st;\n st.insert(s[0]);\n for(int i=1;i<s.size();i++){\n if(st.find(s[i])==st.end()) st.insert(s[i]);\n else{\n ans++;\n st.clear();\n st.insert(s[i]);\n }\n }\n return ans+1;\n }\n};",
"memory": "64814"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n int ans=1;\n set<char> st;\n for(auto it: s){\n if(st.find(it)!=st.end()){\n ans++;\n st.clear();\n }\n st.insert(it);\n } \n\n return ans;\n }\n};",
"memory": "65628"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n set<char>st;\n int setSize=0;\n int total=0;\n for(int i=0;i< s.size();i++)\n {\n st.insert(s[i]);\n if(st.size()> setSize)\n {\n setSize= st.size();\n }\n else{\n st={};\n \n st.insert(s[i]);\n setSize=1;\n total+=1;\n }\n }\n if(st.size()>0) total+=1;\n return total;\n \n }\n};",
"memory": "66441"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int count=1;\n int l=0;\n set<char> myset;\n while(l<s.size()){\n int x=myset.size();\n myset.insert(s[l]);\n int y=myset.size();\n if(x==y){\n count++;\n myset={};\n myset.insert(s[l]);\n }\n l++; \n \n }\n return count++;\n }\n};",
"memory": "67255"
} |
2,487 | <p>Given a string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that the characters in each substring are <strong>unique</strong>. That is, no letter appears in a single substring more than <strong>once</strong>.</p>
<p>Return <em>the <strong>minimum</strong> number of substrings in such a partition.</em></p>
<p>Note that each character should belong to exactly one substring in a partition.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "abacaba"
<strong>Output:</strong> 4
<strong>Explanation:</strong>
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "ssssss"
<strong>Output:</strong> 6
<strong>Explanation:
</strong>The only valid partition is ("s","s","s","s","s","s").
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
<li><code>s</code> consists of only English lowercase letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int partitionString(string s) {\n int count=1;\n int l=0;\n set<char> myset;\n while(l<s.size()){\n int x=myset.size();\n myset.insert(s[l]);\n int y=myset.size();\n if(x==y){\n count++;\n myset={};\n myset.insert(s[l]);\n }\n l++; \n \n }\n return count++;\n }\n};",
"memory": "68069"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.