commit 4e6b28661ee53cda09b1fcfc95fae9e360a1efb3
parent dca51809e945f819aa539e654ff5998adab655aa
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Wed, 24 Jan 2024 22:04:40 +0000
5 Random Problems
Diffstat:
6 files changed, 125 insertions(+), 0 deletions(-)
diff --git a/Problems/0565.cpp b/Problems/0565.cpp
@@ -0,0 +1,18 @@
+class Solution {
+ public:
+ int arrayNesting(const vector<int> &nums) const {
+ static int seen[100001];
+
+ const int n = size(nums);
+ memset(seen, 0x00, n * sizeof(int));
+
+ int res = 0;
+ for (int i = 0; i < size(nums); i++) {
+ int crnt = i, size = 0;
+ while (!seen[crnt])
+ size++, seen[crnt] = true, crnt = nums[crnt];
+ res = max(res, size);
+ }
+ return res;
+ }
+};
diff --git a/Problems/0937.cpp b/Problems/0937.cpp
@@ -0,0 +1,31 @@
+class Solution {
+ public:
+ vector<string> reorderLogFiles(const vector<string> &logs) const {
+ const int n = size(logs);
+
+ typedef tuple<bool, int, string, string> record;
+ vector<record> vec(n);
+
+ for (int i = 0; i < n; i++) {
+ bool letter = false, space = false;
+ int idx = 0;
+ for (int j = 0; j < size(logs[i]); j++) {
+ if (logs[i][j] == ' ')
+ space = true;
+ else if (!space)
+ idx = j;
+ else {
+ letter = isalpha(logs[i][j]);
+ break;
+ }
+ }
+ vec[i] = {!letter, letter ? 0 : i, logs[i].substr(idx + 2), logs[i].substr(0, idx + 1)};
+ }
+
+ sort(begin(vec), end(vec));
+ vector<string> res(n);
+ for (int i = 0; i < n; i++)
+ res[i] = get<3>(vec[i]) + " " + get<2>(vec[i]);
+ return res;
+ }
+};
diff --git a/Problems/1686.cpp b/Problems/1686.cpp
@@ -0,0 +1,18 @@
+class Solution {
+ public:
+ int stoneGameVI(const vector<int> &aliceValues, const vector<int> &bobValues) {
+ const int n = size(aliceValues);
+ vector<int> idx(n);
+
+ iota(begin(idx), end(idx), 0);
+ sort(begin(idx), end(idx),
+ [&](int a, int b) { return aliceValues[a] + bobValues[a] > aliceValues[b] + bobValues[b]; });
+
+ int alice = 0, bob = 0, turn = 1;
+ for (const int i : idx) {
+ (turn ? alice : bob) += turn ? aliceValues[i] : bobValues[i];
+ turn = !turn;
+ }
+ return alice == bob ? 0 : alice > bob ? 1 : -1;
+ }
+};
diff --git a/Problems/2074.cpp b/Problems/2074.cpp
@@ -0,0 +1,33 @@
+class Solution {
+ public:
+ ListNode *reverseEvenLengthGroups(ListNode *head) {
+ ListNode *prev = head, *crnt = head->next;
+
+ for (int lvl = 2; crnt; lvl++) {
+ ListNode *bg = prev;
+
+ int size = 0;
+ while (crnt && size < lvl) {
+ prev = crnt;
+ crnt = crnt->next;
+ size++;
+ }
+
+ if (size % 2) continue;
+
+ crnt = bg->next;
+ ListNode *last = crnt, *first = crnt, *tmp;
+ while (size--) {
+ tmp = crnt;
+ crnt = crnt->next;
+ tmp->next = last;
+ last = tmp;
+ }
+ bg->next = last;
+ first->next = crnt;
+ prev = first;
+ }
+
+ return head;
+ }
+};
diff --git a/Problems/2284.cpp b/Problems/2284.cpp
@@ -0,0 +1,20 @@
+class Solution {
+ public:
+ string largestWordCount(const vector<string> &messages, const vector<string> &senders) const {
+ unordered_map<string, int> um;
+ const int n = size(messages);
+ string res;
+ int maxi = 0;
+ for (int i = 0; i < n; i++) {
+ int cnt = 0;
+ for (const char c : messages[i])
+ cnt += c == ' ';
+ cnt = um[senders[i]] += cnt + 1;
+ if (cnt != maxi ? cnt >= maxi : senders[i] > res) {
+ res = senders[i];
+ maxi = cnt;
+ }
+ }
+ return res;
+ }
+};
diff --git a/README.md b/README.md
@@ -377,6 +377,7 @@ for solving problems.
| 0560 | Medium | [Subarray Sum Equals K](Problems/0560.cpp) |
| 0561 | Easy | [Array Partition](Problems/0561.cpp) |
| 0563 | Easy | [Binary Tree Tilt](Problems/0563.cpp) |
+| 0565 | Medium | [Array Nesting](Problems/0565.cpp) |
| 0566 | Easy | [Reshape the Matrix](Problems/0566.cpp) |
| 0567 | Medium | [Permutation in String](Problems/0567.cpp) |
| 0570 | Medium | [Managers with at Least 5 Direct Reports](Problems/0570.cpp) |
@@ -532,6 +533,7 @@ for solving problems.
| 0933 | Easy | [Number of Recent Calls](Problems/0933.cpp) |
| 0934 | Medium | [Shortest Bridge](Problems/0934.cpp) |
| 0935 | Medium | [Knight Dialer](Problems/0935.cpp) |
+| 0937 | Medium | [Reorder Data in Log Files](Problems/0937.cpp) |
| 0938 | Easy | [Range Sum of BST](Problems/0938.cpp) |
| 0941 | Easy | [Valid Mountain Array](Problems/0941.cpp) |
| 0944 | Easy | [Delete Columns to Make Sorted](Problems/0944.cpp) |
@@ -834,6 +836,7 @@ for solving problems.
| 1680 | Medium | [Concatenation of Consecutive Binary Numbers](Problems/1680.cpp) |
| 1683 | Easy | [Invalid Tweets](Problems/1683.cpp) |
| 1685 | Medium | [Sum of Absolute Differences in a Sorted Array](Problems/1685.cpp) |
+| 1686 | Medium | [Stone Game VI](Problems/1686.cpp) |
| 1688 | Easy | [Count of Matches in Tournament](Problems/1688.cpp) |
| 1689 | Medium | [Partitioning Into Minimum Number Of Deci-Binary Numbers](Problems/1689.cpp) |
| 1690 | Medium | [Stone Game VII](Problems/1690.cpp) |
@@ -936,6 +939,7 @@ for solving problems.
| 2050 | Hard | [Parallel Courses III](Problems/2050.cpp) |
| 2058 | Medium | [Find the Minimum and Maximum Number of Nodes Between Critical Points](Problems/2058.cpp) |
| 2073 | Easy | [Time Needed to Buy Tickets](Problems/2073.cpp) |
+| 2074 | Medium | [Reverse Nodes in Even Length Groups](Problems/2074.cpp) |
| 2079 | Medium | [Watering Plants](Problems/2079.cpp) |
| 2085 | Easy | [Count Common Words With One Occurrence](Problems/2085.cpp) |
| 2090 | Medium | [K Radius Subarray Averages](Problems/2090.cpp) |
@@ -981,6 +985,7 @@ for solving problems.
| 2272 | Hard | [Substring With Largest Variance](Problems/2272.cpp) |
| 2275 | Medium | [Largest Combination With Bitwise AND Greater Than Zero](Problems/2275.cpp) |
| 2279 | Medium | [Maximum Bags With Full Capacity of Rocks](Problems/2279.cpp) |
+| 2284 | Medium | [Sender With Largest Word Count](Problems/2284.cpp) |
| 2285 | Medium | [Maximum Total Importance of Roads](Problems/2285.cpp) |
| 2294 | Medium | [Partition Array Such That Maximum Difference Is K](Problems/2294.cpp) |
| 2295 | Medium | [Replace Elements in an Array](Problems/2295.cpp) |