0721-0730

721. Accounts Merge $\star\star$

722. Remove Comments $\star\star$

723. Candy Crush $\star\star$

724. Find Pivot Index $\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
 public:
  int pivotIndex(vector<int>& nums) {
    int sum = accumulate(nums.begin(), nums.end(), 0);
    int presum = 0;

    for (int i = 0; i < nums.size(); ++i) {
      if (presum == sum - presum - nums[i]) return i;
      presum += nums[i];
    }

    return -1;
  }
};

725. Split Linked List in Parts $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
 public:
  vector<ListNode*> splitListToParts(ListNode* root, int k) {
    vector<ListNode*> ans(k, NULL);
    int length = 0;
    for (auto curr = root; curr; curr = curr->next) ++length;
    int l = length / k;
    int r = length % k;

    ListNode* head = root;
    ListNode* prev = NULL;

    for (int i = 0; i < k; ++i, --r) {
      ans[i] = head;
      for (int j = 0; j < l + (r > 0); ++j) {
        prev = head;
        head = head->next;
      }
      if (prev) prev->next = NULL;
    }

    return ans;
  }
};

726. Number of Atoms $\star\star\star$

727. Minimum Window Subsequence $\star\star\star$

728. Self Dividing Numbers $\star$

729. My Calendar I $\star\star$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class MyCalendar {
 public:
  bool book(int start, int end) {
    for (pair<int, int>& book : books)
      if (max(book.first, start) < min(book.second, end)) return false;
    books.push_back({start, end});

    return true;
  }

 private:
  vector<pair<int, int>> books;
};

730. Count Different Palindromic Subsequences $\star\star\star$