The last problem this week was a classic computational geometry problem and was not easy to write. But the acceptance rate was still quite high, probably because the test cases were relatively weak.
My ranking in the Cruel group dropped to 32, a 100% decline. The feeling I had before that I had become stronger was an illusion. It was because I got a good rank of 113 in Weekly Contest 186 three weeks ago, so according to the group’s ranking algorithm, my ranking stayed relatively high for the next three weeks. After that best result aged out, my ranking returned to its original level, around 30. It was not that I had become stronger; I was just lucky. Also, I happened to use Python in contest 186, and Python really does have an advantage in speed contests.
1450. Number of Students Doing Homework at a Given Time
A warm-up problem. One pass.
Time complexity: O(N), space complexity: O(1).
1 2 3 4 5 6 7 8 9 10
classSolution { public: intbusyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime){ int ans = 0; for (int i = 0; i < startTime.size(); ++i) { if (startTime[i] <= queryTime && queryTime <= endTime[i]) ++ans; } return ans; } };
1451. Rearrange Words in a Sentence
Use the sorting function provided by the C++ STL.
Time complexity: O(N log N), space complexity: O(N).
classSolution { vector<pair<string,int>> splitSentence(const string& text) { string tmp; vector<pair<string,int>> stk; stringstream ss(text); int position = 0; while(getline(ss,tmp,' ')) { if (!islower(tmp[0])) tmp[0] = tolower(tmp[0]); stk.push_back({tmp, position}); ++position; } return stk; } public: string arrangeWords(string text){ auto split = splitSentence(text); sort(split.begin(), split.end(), [](constauto& a, constauto& b) -> bool { if (a.first.size() == b.first.size()) { return a.second < b.second; } else { return a.first.size() < b.first.size(); } }); string ans; for (constauto& word : split) { ans += word.first; ans.push_back(' '); } ans.pop_back(); ans[0] = toupper(ans[0]); return ans; } };
1452. People Whose List of Favorite Companies Is Not a Subset of Another List
Brute force. For each person, judge whether their list is contained by another person’s list. This is N^2. When checking containment, sort the company lists first and then compare them sequentially, O(m * log m + m). N is the number of people, and M is the number of companies.
Time complexity: O(N^2 * M log M), space complexity: O(N).
Here I used the includes function from STL algorithms, which can determine whether one sorted range contains another.
classSolution { public: vector<int> peopleIndexes(vector<vector<string>>& favoriteCompanies){ for (auto& v : favoriteCompanies) { sort(v.begin(), v.end()); } constint n = favoriteCompanies.size(); vector<int> ans; for (int i = 0; i < n; ++i) { bool remove = false; for (int j = 0; j < n; ++j) { if (i == j) continue; if (includes(favoriteCompanies[j].begin(), favoriteCompanies[j].end(), favoriteCompanies[i].begin(), favoriteCompanies[i].end())) { remove = true; break; } } if (!remove) { ans.push_back(i); } } return ans; } };
1453. Maximum Number of Darts Inside of a Circular Dartboard
A classic problem: the disk partial covering problem. You can find it with a casual online search. The principle can be referenced from GeeksforGeeks and this discuss post; it is the famous angular sweep.
Time complexity: O(n^2 log n) space complexity: O(n ^ 2).