|
12 | 12 | 10. Monotonic Array |
13 | 13 | 11. Longest Continuous Increasing Subsequence |
14 | 14 | 12. Maximum Product Subarray |
15 | | - |
| 15 | +13. First Missing Positive |
| 16 | +14. Maximum Swap |
| 17 | +15. Meeting Room II |
| 18 | +16. Sort Colors |
16 | 19 |
|
17 | 20 | ## Implementation |
18 | 21 |
|
@@ -455,7 +458,6 @@ public: |
455 | 458 | } |
456 | 459 | }; |
457 | 460 | ``` |
458 | | - |
459 | 461 | ### **Maximum Product Subarray** |
460 | 462 |
|
461 | 463 | ***Big O:*** O(n) speed, O(1) space |
@@ -488,4 +490,135 @@ public: |
488 | 490 | return result; |
489 | 491 | } |
490 | 492 | }; |
| 493 | +``` |
| 494 | + |
| 495 | +### **First Missing Positive** |
| 496 | + |
| 497 | +***Big O:*** O(n) speed, O(1) space |
| 498 | +``` |
| 499 | +Tips: |
| 500 | +
|
| 501 | +Hashmap. |
| 502 | +``` |
| 503 | +```c++ |
| 504 | +class Solution { |
| 505 | +public: |
| 506 | + int firstMissingPositive(vector<int>& nums) { |
| 507 | + unordered_map<int, int> umap; |
| 508 | + int max_v = 0; |
| 509 | + |
| 510 | + for (int i = 0; i < nums.size(); i++) { |
| 511 | + if (nums[i] > 0) { |
| 512 | + umap[nums[i]] = 1; |
| 513 | + max_v = max(max_v, nums[i]); |
| 514 | + } |
| 515 | + } |
| 516 | + |
| 517 | + for (int i = 1; i < max_v; i++) { |
| 518 | + if (umap.find(i) == umap.end()) |
| 519 | + return i; |
| 520 | + } |
| 521 | + |
| 522 | + return max_v+1; |
| 523 | + } |
| 524 | +}; |
| 525 | +``` |
| 526 | +
|
| 527 | +### **Maximum Swap** |
| 528 | +
|
| 529 | +***Big O:*** O(n) speed, O(1) space |
| 530 | +``` |
| 531 | +Tips: |
| 532 | + |
| 533 | +Greedy. |
| 534 | +``` |
| 535 | +```c++ |
| 536 | +class Solution { |
| 537 | +public: |
| 538 | + int maximumSwap(int num) { |
| 539 | + string num_str = to_string(num); |
| 540 | + int n = num_str.size(); |
| 541 | + int max_val = -1, max_i = -1; |
| 542 | + int left = -1, right = -1; |
| 543 | + |
| 544 | + for (int i = n - 1; i >= 0; i--) { |
| 545 | + if (num_str[i] > max_val) { |
| 546 | + max_val = num_str[i]; |
| 547 | + max_i = i; |
| 548 | + } else if (num_str[i] < max_val) { |
| 549 | + left = i; |
| 550 | + right = max_i; |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + if (left == -1) return num; |
| 555 | + char c = num_str[left]; |
| 556 | + num_str[left] = num_str[right]; |
| 557 | + num_str[right] = c; |
| 558 | + return stoi(num_str); |
| 559 | + } |
| 560 | +}; |
| 561 | +``` |
| 562 | + |
| 563 | +### **Meeting Rooms II** |
| 564 | + |
| 565 | +***Big O:*** O(nlog(n)) speed, O(n) space |
| 566 | +``` |
| 567 | +Tips: |
| 568 | +
|
| 569 | +Sort + priority queue. Kick people out if their meeting is already finished. |
| 570 | +``` |
| 571 | +```c++ |
| 572 | +class Solution { |
| 573 | +public: |
| 574 | + int minMeetingRooms(vector<vector<int>>& intervals) { |
| 575 | + sort(intervals.begin(), intervals.end()); |
| 576 | + |
| 577 | + priority_queue<int, vector<int>, greater<int>> pq; |
| 578 | + |
| 579 | + for (auto v : intervals) { |
| 580 | + // check if the last meetings need to be completed |
| 581 | + // before this meeting starts. |
| 582 | + if (!pq.empty() && pq.top() <= v[0]) { |
| 583 | + pq.pop(); |
| 584 | + } |
| 585 | + |
| 586 | + // start this meeting and enter the end time. |
| 587 | + pq.push(v[1]); |
| 588 | + } |
| 589 | + |
| 590 | + return pq.size(); |
| 591 | + } |
| 592 | +}; |
| 593 | +``` |
| 594 | +
|
| 595 | +### **Sort Colors** |
| 596 | +
|
| 597 | +***Big O:*** O(n) speed (one pass), O(1) space |
| 598 | +``` |
| 599 | +Tips: |
| 600 | + |
| 601 | +The idea of solution is to move curr pointer along the array, if nums[curr] = 0 - swap it with nums[p0], if nums[curr] = 2 - swap it with nums[p2]. |
| 602 | +``` |
| 603 | +```c++ |
| 604 | +class Solution { |
| 605 | +
|
| 606 | +public: |
| 607 | + void sortColors(vector<int>& nums) { |
| 608 | + int lo = 0, hi = nums.size() - 1, i = 0; |
| 609 | +
|
| 610 | + while (i <= hi) { |
| 611 | + if (nums[i] == 0) swap(nums, lo++, i++); |
| 612 | + else if (nums[i] == 2) swap(nums, i, hi--); |
| 613 | + else if (nums[i] == 1) i++; |
| 614 | + } |
| 615 | + } |
| 616 | +
|
| 617 | + void swap(vector<int>& nums, int i, int j) { |
| 618 | + int t = nums[i]; |
| 619 | + nums[i] = nums[j]; |
| 620 | + nums[j] = t; |
| 621 | + } |
| 622 | +
|
| 623 | +}; |
491 | 624 | ``` |
0 commit comments