Skip to content

Commit

Permalink
daily: Nov21 and Nov22
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed Nov 30, 2024
1 parent 74fd67e commit c5b107c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions daily/Nov20.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <numeric>
using namespace std;

class Solution {
public:
vector<int> shortestDistanceAfterQueries(int n, vector<vector<int>>& queries) {
vector<int> fa(n - 1);
iota(fa.begin(), fa.end(), 0);

auto find = [&](int x) -> int {
int rt = x;
while (fa[rt] != rt) {
rt = fa[rt];
}
while (fa[x] != rt) {
int tmp = fa[x];
fa[x] = rt;
x = tmp;
}
return rt;
};

vector<int> ans(queries.size());
int cnt = n - 1; // num union find
for (int qi = 0; qi < queries.size(); qi++) {
int l = queries[qi][0], r = queries[qi][1] - 1;
int fr = find(r);
for (int i = find(l); i < r; i = find(i + 1)) {
fa[i] = fr;
cnt--;
}
ans[qi] = cnt;
}
return ans;
}

};
27 changes: 27 additions & 0 deletions daily/Nov21.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;

class Solution {
public:
/**
* @brief LC1056: confusingNumber
* Unordered map, time: O(N), Space: O(N)
*
* @param n
* @return true
* @return false
*/
bool confusingNumber(int n) {
unordered_map<char, char> mp = {
{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}};
auto numstr = to_string(n);
string newstr = "";
for (auto& ch : numstr) {
if (!mp.count(ch)) {
return false;
}
newstr = mp[ch] + newstr;
}
return newstr != numstr;
}
};

0 comments on commit c5b107c

Please sign in to comment.