Skip to content

7.19 #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

7.19 #417

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions check-divisibility-by-digit-sum-and-product/answer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
bool checkDivisibility(int n) {
int p = 1, sum = 0;
for (int m = n; m > 0; m /= 10) {
sum += m % 10;
p *= m % 10;
}
return n % (sum + p) == 0;
}
};
34 changes: 34 additions & 0 deletions count-islands-with-total-value-divisible-by-k/answer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int countIslands(vector<vector<int>> &a, int k) {
int n = a.size(), m = a[0].size();
vector<vector<bool>> vis(n, vector<bool>(m, 0));

function<void(int, int, int &)> dfs = [&](int x, int y, int &mod) {
vis[x][y] = 1;
assert(a[x][y] > 0);
mod = (mod + a[x][y]) % k;
if (y + 1 < m and !vis[x][y + 1] and a[x][y + 1] > 0)
dfs(x, y + 1, mod);
if (y > 0 and !vis[x][y - 1] and a[x][y - 1] > 0)
dfs(x, y - 1, mod);
if (x + 1 < n and !vis[x + 1][y] and a[x + 1][y] > 0)
dfs(x + 1, y, mod);
if (x > 0 and !vis[x - 1][y] and a[x - 1][y] > 0)
dfs(x - 1, y, mod);
};

int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!vis[i][j] and a[i][j] > 0) {
int mod = 0;
dfs(i, j, mod);
if (mod == 0)
ans++;
}
}
}
return ans;
}
};
56 changes: 56 additions & 0 deletions network-recovery-pathways/answer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using int64 = long long;

template <class T> bool umax(T &a, const T b) { return a < b ? a = b, 1 : 0; }

class Solution {
public:
int findMaxPathScore(vector<vector<int>> &edges, vector<bool> &online,
long long k) {
int n = online.size();
vector<vector<pair<int, int>>> adj(n);
for (auto e : edges)
adj[e[0]].push_back({e[1], e[2]});

vector<int64> dis(n);
auto Test = [&](int val) -> bool {
dis.assign(n, LLONG_MAX);
using State = pair<int64, int>;
priority_queue<State, vector<State>, greater<State>> q;
dis[0] = 0;
q.push({0, 0});

while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
// skip invalid node
if (dis[u] != d)
continue;

for (auto [v, weight] : adj[u]) {
if (!online[v])
continue;
if (weight < val)
continue;
int64 w = dis[u] + weight;
if (w < dis[v]) {
dis[v] = w;
q.push({dis[v], v});
}
}
}
return dis[n - 1] <= k;
};

int l = 0, r = 0, ans = -1;
for (auto e : edges)
umax(r, e[2]);
while (l <= r) {
int mid = (l + r) / 2;
if (Test(mid))
ans = mid, l = mid + 1;
else
r = mid - 1;
}
return ans;
}
};
66 changes: 66 additions & 0 deletions number-of-integers-with-popcount-depth-equal-to-k-i/answer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#define bitCount(n) __builtin_popcountll((n))

using int64 = long long;

class Solution {
public:
std::string toBinary(int64 n) {
if (n == 0)
return "0";

std::string s = "";
while (n > 0) {
s += (n % 2) ? '1' : '0';
n /= 2;
}

// Reverse since we built it backwards
std::reverse(s.begin(), s.end());
return s;
}

long long popcountDepth(long long n, int k) {
if (k == 0)
return 1;

vector<int> cnt(65, 0);
cnt[1] = 0;
for (int i = 2; i <= 64; i++)
cnt[i] = cnt[bitCount(i)] + 1;

string s = toBinary(n);
int m = s.size();

// dp[pos][ones][equal]
vector<vector<vector<int64>>> dp(
m, vector<vector<int64>>(65, vector<int64>(2, 0)));
dp[0][0][0] = 1;
dp[0][1][1] = 1;
for (int i = 1; i < m; i++) {
int d = s[i] - '0';
for (int j = 0; j <= i; j++) {
// choose 0
dp[i][j][0] += dp[i - 1][j][0];
if (d == 1)
dp[i][j][0] += dp[i - 1][j][1];
else
dp[i][j][1] += dp[i - 1][j][1];

// choose 1
dp[i][j + 1][0] += dp[i - 1][j][0];
if (d == 1)
dp[i][j + 1][1] += dp[i - 1][j][1];
}
}

int64 ans = 0;
for (int i = 1; i <= m; i++) {
if (cnt[i] != k - 1)
continue;
ans += dp[m - 1][i][0] + dp[m - 1][i][1];
}
if (k == 1)
return ans - 1;
return ans;
}
};
59 changes: 59 additions & 0 deletions split-array-by-prime-indices/answer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
struct PrimeFactorization {

// primes.size() <= sqrt(n)
vector<int> primes;
vector<bool> p;

PrimeFactorization(int n) {
p.resize(n);
p.assign(n, true);

p[0] = p[1] = 0;
for (int i = 4; i < n; i += 2)
p[i] = 0;
for (int i = 3; i * i < n; i += 2) {
if (!p[i])
continue;
for (int j = i * i; j < n; j += i)
p[j] = 0;
}

primes = {2};
for (int i = 3; i < n; i += 2)
if (p[i])
primes.push_back(i);
}

vector<pair<int, int>> getFactors(int n) {
vector<pair<int, int>> ans;
for (int i = 0; n > 1 && i < (int)primes.size(); i++) {
int cnt = 0;
while (n % primes[i] == 0) {
n /= primes[i];
cnt++;
}
if (cnt)
ans.emplace_back(primes[i], cnt);
}
if (n > 1)
ans.emplace_back(n, 1);
return ans;
}
} pf(1e5 + 1);

using int64 = long long;

class Solution {
public:
long long splitArray(vector<int> &a) {
const auto &p = pf.p;
int64 ans = 0;
for (int i = 0; i < a.size(); i++) {
if (p[i])
ans += a[i];
else
ans -= a[i];
}
return abs(ans);
}
};