teleren/devto huggingface dataset:
1735. Leetcode Solution in Java
Tags: java
Published: 2022-07-06 14:52:02

```
class Solution {
    int[] primes = new int[14];
    int[] exponents = new int[14];
    int index = 0;
    long[][] combinations = new long[11000][20];
    static final int MODULO = 1000000007;

    public int[] waysToFillArray(int[][] queries) {
        for (int i = 0; i < 10500; i++) {
            combinations[i][0] = 1;
            if (i < 15)
                combinations[i][i] = 1;
            int end = Math.min(i, 15);
            for (int j = 1; j < end; j++)
                combinations[i][j] = (combinations[i - 1][j - 1] + combinations[i - 1][j]) % MODULO;
        }
        int queriesCount = queries.length;
        int[] ways = new int[queriesCount];
        for (int i = 0; i < queriesCount; i++) {
            int n = queries[i][0], k = queries[i][1];
            long way = 1;
            fact(k);
            for (int j = 1; j <= index; j++)
                way = way * combinations[n + exponents[j] - 1][exponents[j]] % MODULO;
            ways[i] = (int) way;
        }
        return ways;
    }

    public void fact(int n) {
        index = 0;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                index++;
                primes[index] = i;
                exponents[index] = 0;
                while (n % i == 0) {
                    exponents[index]++;
                    n /= i;
                }
            }
        }
        if (n != 1) {
            index++;
            primes[index] = n;
            exponents[index] = 1;
        }
    }
}
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/count-ways-to-make-array-with-product/
1703. Leetcode Solution in Javascript
Tags: javascript
Published: 2022-07-06 14:50:38

```
var minOperations = function(T, A) {
    let imap = new Map(), lis = []
    for (let i = 0; i < T.length; i++) imap.set(T[i], i)
    for (let i = 0; i < A.length; i++) {
        let index = imap.get(A[i])
        if (index !== undefined)
            lis[find(index, lis)] = index
    }
    return T.length - lis.length
};

const find = (target, arr, left=0, right=arr.length) => {
    while (left <= right) {
        let mid = left + right >> 1
        if (arr[mid] < target) left = mid + 1
        else right = mid - 1
    }
    return left
}
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/minimum-operations-to-make-a-subsequence/
Consider hosting your own git repositories
Tags: linux, programming, devops, github
Published: 2022-07-06 14:49:40

In the light of the [recent events](https://sfconservancy.org/blog/2022/jun/30/give-up-github-launch/), a lot of people are searching for alternatives to GitHub. 

Hosting your own git repos is not as hard as people may imagine and maybe you should consider that as an option.

It boils down to this:

1. If you don't already have a Linux/BSD VPS, consider getting one (it'll be useful for a lot more than this)

2. Make sure that you have git installed (i.e. `apt/yum install git`)

3. Make a directory and do `git --bare init` there

Congrats - you can now easily access your new repo over ssh:
```
git clone ssh://myhost/mydir
```

It may be also worth mentioning that now you have full access to things like git hooks (from which you have direct access to your whole system - this can be pretty useful).

## Next steps

For private repos, you don't need much more than this. 

Making your repositories publically available is also not hard - there are many ways, the simplest one is probably to just pass HTTP requests from your web server (e.g. nginx) to `git-http-backend` (I'll create a separate post for this).


Note: If you're new to all this, you may want to experiment in local VMs before you're comfortable with managing a real VPS.
1707. Leetcode Solution in Java
Tags: java
Published: 2022-07-06 14:49:19

```
class Solution {
    public int[] maximizeXor(int[] nums, int[][] queries) {
        TrieNode root = new TrieNode();
        for (int num : nums) {
            TrieNode node = root;
            for (int i = 31; i >= 0; i--) {
                int bit = (num & (1 << i)) == 0 ? 0 : 1;
                if (node.children[bit] == null)
                    node.children[bit] = new TrieNode();
                node = node.children[bit];
                node.min = Math.min(node.min, num);
            }
            node.isEnd = true;
        }
        int queriesCount = queries.length;
        int[] maxXors = new int[queriesCount];
        for (int i = 0; i < queriesCount; i++) {
            int xor = 0;
            int[] query = queries[i];
            int x = query[0], m = query[1];
            TrieNode node = root;
            boolean flag = true;
            for (int j = 31; j >= 0; j--) {
                int bit = (x & (1 << j)) == 0 ? 1 : 0;
                if (node.children[bit] != null && node.children[bit].min <= m) {
                    if (bit == 1)
                        xor ^= 1 << j;
                    node = node.children[bit];
                } else if (node.children[1 - bit] != null && node.children[1 - bit].min <= m) {
                    if (bit == 0)
                        xor ^= 1 << j;
                    node = node.children[1 - bit];
                } else {
                    flag = false;
                    break;
                }
            }
            if (node.isEnd && flag)
                maxXors[i] = xor ^ x;
            else
                maxXors[i] = -1;
        }
        return maxXors;
    }
}

class TrieNode {
    int min = Integer.MAX_VALUE;
    boolean isEnd;
    TrieNode[] children;

    public TrieNode() {
        children = new TrieNode[2];
        isEnd = false;
    }
}
```
#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/maximum-xor-with-an-element-from-array/
1703. Leetcode Solution in JAVA
Tags: java
Published: 2022-07-06 14:47:47

```
class Solution {
    public int minMoves(int[] nums, int k) {
        if (k == 1)
            return 0;
        List<Integer> indices = new ArrayList<Integer>();
        int length = nums.length;
        for (int i = 0; i < length; i++) {
            if (nums[i] == 1)
                indices.add(i);
        }
        int ones = indices.size();
        int differencesCount = ones - 1;
        int[] differences = new int[differencesCount];
        for (int i = 1; i < ones; i++)
            differences[i - 1] = indices.get(i) - indices.get(i - 1) - 1;
        int[] prefixSums = new int[differencesCount];
        prefixSums[0] = differences[0];
        for (int i = 1; i < differencesCount; i++)
            prefixSums[i] = prefixSums[i - 1] + differences[i];
        int[] postfixSums = new int[differencesCount];
        postfixSums[differencesCount - 1] = differences[differencesCount - 1];
        for (int i = differencesCount - 2; i >= 0; i--)
            postfixSums[i] = postfixSums[i + 1] + differences[i];
        int moves = getSum(differences, k);
        int minMoves = moves;
        int intervals = k - 1;
        for (int i = intervals; i < differencesCount; i++) {
            moves -= getPrefixSum(prefixSums, i - intervals, i - intervals + k / 2 - 1);
            moves += getPostfixSum(postfixSums, i - k / 2 + 1, i);
            minMoves = Math.min(minMoves, moves);
        }
        return minMoves;
    }

    public int getSum(int[] differences, int k) {
        int sum = 0;
        int left = 0, right = k - 2;
        while (left < right) {
            sum += (differences[left] + differences[right]) * (left + 1);
            left++;
            right--;
        }
        if (left == right)
            sum += differences[left] * (left + 1);
        return sum;
    }

    public int getPrefixSum(int[] prefixSums, int start, int end) {
        if (start == 0)
            return prefixSums[end];
        else
            return prefixSums[end] - prefixSums[start - 1];
    }

    public int getPostfixSum(int[] postfixSums, int start, int end) {
        if (end == postfixSums.length - 1)
            return postfixSums[start];
        else
            return postfixSums[start] - postfixSums[end + 1];
    }
}
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones/
1691. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:46:22

```
class Solution {
public:
    int maxHeight(vector<vector<int>>& cuboids) 
    {
        int n = cuboids.size();
        vector<array<int,4>>p;
        for (int i=0; i<cuboids.size(); i++)
        {
            auto c = cuboids[i];
            p.push_back({c[0], c[1], c[2], i});
            p.push_back({c[0], c[2], c[1], i});
            p.push_back({c[1], c[0], c[2], i});
            p.push_back({c[1], c[2], c[0], i});
            p.push_back({c[2], c[0], c[1], i});
            p.push_back({c[2], c[1], c[0], i});
        }
        sort(p.begin(), p.end());
        vector<int>dp(6*n);
        for (int i=0; i<6*n; i++)
        {
            dp[i] = p[i][2];
            for (int j=0; j<i; j++)
                if (p[j][0]<=p[i][0] && p[j][1]<=p[i][1] && p[j][2]<=p[i][2] && p[j][3]!=p[i][3])
                    dp[i] = max(dp[i], dp[j]+p[i][2]);
        }

        int ret = 0;
        for (int i=0; i<6*n; i++)
            ret = max(ret, dp[i]);        
        return ret;
    }
};
```


#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/maximum-height-by-stacking-cuboids/
1681. Leetcode Solutoin in CPP
Published: 2022-07-06 14:45:18

```
class Solution {
public:
    int minimumIncompatibility(vector<int>& A, int k) {
        int N = A.size(), M = 1 << N, sz = N / k;
        if (k == 1) {
            set<int> s(begin(A), end(A));
            return s.size() == A.size() ? *s.rbegin() - *s.begin() : -1;
        }
        if (k == N) return 0;
        sort(begin(A), end(A));
        int dp[M][N];
        memset(dp, 0x3f, sizeof(dp)); // infinity
        memset(dp[0], 0, sizeof(dp[0]));
        for (int mask = 1; mask < M; ++mask) {
            if (__builtin_popcount(mask) % sz) {
                for (int pre = 0; pre < N; ++pre) {
                    for (int p = pre + 1; p < N; ++p) {
                        if ((mask & (1 << p)) && A[p] > A[pre]) {
                            dp[mask][pre] = min(dp[mask][pre], dp[mask - (1 << p)][p] + A[p] - A[pre]);
                        }
                    }
                }
            } else {
                for (int p = 0; p < N; ++p) {
                    if (mask & (1 << p)) dp[mask][0] = min(dp[mask][0], dp[mask - (1 << p)][p]);
                }
                for (int i = 1; i < N; ++i) dp[mask][i] = dp[mask][0];
            }
        }
        return dp[M - 1][0] > 10000 ? -1 : dp[M - 1][0];
    }
};
```


#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/minimum-incompatibility/
1632. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:41:02

```
class UnionFind {
    vector<int> id;
public:
    UnionFind(int n) : id(n) {
        iota(begin(id), end(id), 0);
    }
    void connect(int a, int b) {
        int x = find(a), y = find(b);
        if (x == y) return;
        id[x] = y;
    }
    int find(int a) {
        return id[a] == a ? a : (id[a] = find(id[a]));
    }
};
class Solution {
public:
    vector<vector<int>> matrixRankTransform(vector<vector<int>>& A) {
        int M = A.size(), N = A[0].size();
        map<int, vector<vector<int>>> m;
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) m[A[i][j]].push_back({ i, j });
        }
        vector<int> rank(M + N, 0); // rank[0..(M-1)] stores the highest ranks of the rows, rank[M..(M+N-1)] stores the highest ranks of the columns.
        vector<vector<int>> ans(M, vector<int>(N));
        for (auto &[val, pos] : m) {
            UnionFind uf(M + N);
            for (auto &p : pos) {
                int x = uf.find(p[0]), y = uf.find(p[1] + M);
                uf.connect(x, y); // for each number, connect its (representative) row and column
                rank[uf.find(x)] = max(rank[x], rank[y]);
            }
            auto next = rank;
            for (auto &p : pos) {
                int x = p[0], y = p[1], r = uf.find(x);
                ans[x][y] = next[x] = next[y + M] = rank[r] + 1;
            }
            rank = move(next);
        }
        return ans;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/rank-transform-of-a-matrix
1622. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:39:36

```
class Fancy {
    typedef unsigned long UL;
    UL mod = 1e9 + 7, len = 0, increment = 0, mul = 1, A[100001];
    UL modPow(UL base, UL exp) {
        UL ans = 1, p = base;
        for (; exp; exp >>= 1) {
            if (exp & 1) ans = (ans * p) % mod;
            p = (p * p) % mod;
        }
        return ans;
    }
public:
    Fancy() {}
    void append(int val) {
        A[len++] = (((val - increment + mod) % mod) * modPow(mul, mod - 2)) % mod;
    }
    void addAll(int inc) {
        increment = (increment + inc) % mod;
    }
    void multAll(int m) {
        mul = (mul * m) % mod;
        increment = (increment * m) % mod;
    }
    int getIndex(int i) {
        return i >= len ? -1 : ((A[i] * mul) % mod + increment) % mod;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/fancy-sequence/
1617. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:38:28

```
class Solution {
    int seen[16] = {}, incl[16] = {}; // incl[i] == 1 if node `i` is included in mask
    vector<int> g[16];
    void dfs(int u, int &cnt) {
        if (!incl[u] || seen[u]) return;
        ++cnt;
        seen[u] = 1;
        for (int v : g[u]) dfs(v, cnt);
    }
public:
    vector<int> countSubgraphsForEachDiameter(int n, vector<vector<int>>& E) {
        int dist[16][16] = {};
        memset(dist, 0x3f, sizeof(dist));
        for (auto &e : E) {
            int u = e[0] - 1, v = e[1] - 1;
            g[u].push_back(v);
            g[v].push_back(u);
            dist[u][v] = dist[v][u] = 1;
        }
        for (int k = 0; k < n; ++k) { // use floyd to get the minimal distance between every two nodes.
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
        }
        vector<int> ans(n - 1);
        for (int mask = 1; mask < (1 << n); ++mask) {
            memset(seen, 0, sizeof(seen));
            memset(incl, 0, sizeof(incl));
            int cnt = __builtin_popcount(mask), start;
            if (cnt < 2) continue;
            for (int i = 0; i < n; ++i) {
                if (mask & (1 << i)) {
                    incl[i] = 1;
                    start = i;
                }
            }
            int c = 0;
            dfs(start, c); // count how many nodes we can visit.
            if (cnt != c) continue; // not all nodes are connected
            int mx = 0;
            for (int i = 0; i < n; ++i) {
                if (!incl[i]) continue;
                for (int j = i + 1; j < n; ++j) {
                    if (!incl[j]) continue;
                    mx = max(mx, dist[i][j]);
                }
            }
            ++ans[mx - 1];
        }
        return ans;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/count-subtrees-with-max-distance-between-cities/
1611. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:37:12

```
class Solution {
    inline unsigned lowbit(unsigned x) { return x & -x; }
public:
    int minimumOneBitOperations(int n) {
        if (n <= 1) return n;
        unsigned first = ~0;
        while (first & n) first <<= 1;
        first = lowbit(first >> 1);
        unsigned second = (first >> 1) & n, rest = minimumOneBitOperations(n & ~first & ~second);
        return second ? first + rest : (first << 1) - 1 - rest;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/minimum-one-bit-operations-to-make-integers-zero/
Web Components at Big Tech Companies: Github
Tags: javascript, webdev, webcomponents, programming
Published: 2022-07-06 14:36:42

![Github and Web Components Logo](https://miro.medium.com/max/1400/1*qXdGMMAGzO36nj5XmoJ5KQ.png)


{% medium https://medium.com/p/812e632e1650 %}
1610. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:36:03

```
class Solution {
public:
    int visiblePoints(vector<vector<int>>& P, int angle, vector<int>& L) {
        int same = 0, cnt = 0;
        vector<double> A;
        for (auto &p : P) {
            if (p == L) ++same;
            else A.push_back(atan2(p[1] - L[1], p[0] - L[0]) / M_PI * 180);
        }
        sort(begin(A), end(A));
        for (int i = 0; i < A.size(); ++i) {
            double bound = A[i] + angle;
            int j = upper_bound(begin(A), end(A), bound) - begin(A), total = j - i;
            if (bound > 180) total += upper_bound(begin(A), end(A), bound - 360) - begin(A);
            cnt = max(cnt, total);
        }
        return same + cnt;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/maximum-number-of-visible-points/
1606. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:34:48

```
class Solution {
public:
    vector<int> busiestServers(int k, vector<int>& A, vector<int>& L) {
        vector<int> cnt(k);
        set<int> free;
        for (int i = 0; i < k; ++i) free.insert(i);
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> busy; // endTime, serverIndex
        for (int i = 0; i < A.size(); ++i) {
            int start = A[i], end = start + L[i];
            while (busy.size() && busy.top().first <= start) {
                int server = busy.top().second;
                busy.pop();
                free.insert(server);
            }
            if (free.empty()) continue;
            auto it = free.lower_bound(i % k);
            if (it == free.end()) it = free.begin();
            cnt[*it]++;
            busy.emplace(end, *it);
            free.erase(*it);
        }
        vector<int> ans;
        int mx = *max_element(begin(cnt), end(cnt));
        for (int i = 0; i < k; ++i) {
            if (cnt[i] == mx) ans.push_back(i);
        }
        return ans;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/find-servers-that-handled-most-number-of-requests/
1601. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:33:01

```
class Solution {
public:
    int maximumRequests(int n, vector<vector<int>>& requests) {
        int m = requests.size(), ans = 0, g[20] = {};
        for (int mask = (1 << m) - 1; mask; mask--) {
            memset(g, 0, sizeof(g));
            int cnt = 0;
            for (int i = 0; i < m; ++i) {
                if (mask & (1 << i)) {
                    cnt++;
                    auto &r = requests[i];
                    int u = r[0], v = r[1];
                    g[u]++;
                    g[v]--;
                }
            }
            bool good = true;
            for (int i = 0; i < n && good; ++i) good = g[i] == 0;
            if (good) ans = max(ans, cnt);
        }
        return ans;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/maximum-number-of-achievable-transfer-requests/
Need to transform Json data
Tags: json
Published: 2022-07-06 14:27:43

I am writing a project where I need to transform Json from one format to another. I am new to json. Any lead will be appreciated. 
Input Data:

```
{
  "sorts": [
    {
      "name": "firstName",
      "sortOrder": "Ascending"
    }
  ],
  "page": "1",
  "pageSize": "10",
  "filters": [
    {
      "name": "studentIds",
      "type": "InList",
      "value": "(S001,S002,S004)"
    },
    {
      "name": "assignmentStatus",
      "type": "InList",
      "value": "(Completed, In Progress)"
    }
  ]
}
```

Target Data:

```
{
    "orderBy": "firstName",
    "sortOrder": "Ascending",
    "page": "1",
    "pageSize": "10",
    "studentIds": [
        "S001",
        "S002",
        "S003"
    ],
    "assignmentStatus": [
        "Completed",
        "In Progress"
    ]
}
```
1595. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:31:51

```
class Solution {
    int dp[13][4096] = {};
    int dfs(vector<vector<int>> &cost, vector<int> &mn, int i, int mask) {
        if (dp[i][mask] != -1) return dp[i][mask];
        int ans = i < cost.size() ? INT_MAX : 0;
        if (i < cost.size()) {
            for (int j = 0; j < cost[0].size(); ++j) {
                ans = min(ans, cost[i][j] + dfs(cost, mn, i + 1, mask | (1 << j))); // for nodes in the 1st group, use DP to get the min cost.
            } 
        } else {
            for (int j = 0; j < cost[0].size(); ++j) {
                if ((mask & (1 << j)) == 0) ans += mn[j]; // for those unconnected nodes in the 2nd group, pick the min cost to connect to the 1st group
            }
        }
        return dp[i][mask] = ans;
    }
public:
    int connectTwoGroups(vector<vector<int>>& cost) {
        memset(dp, -1, sizeof(dp));
        vector<int> mn(cost[0].size(), INT_MAX); // mn[j] is the minimal cost to connect jth node in the 2nd group to a node in the 1st group
        for (int j = 0; j < mn.size(); ++j) {
            for (int i = 0; i < cost.size(); ++i) mn[j] = min(mn[j], cost[i][j]);
        }
        return dfs(cost, mn, 0, 0);
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/minimum-cost-to-connect-two-groups-of-points/
1591. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:30:35

```
// OJ: https://leetcode.com/problems/strange-printer-ii/

// Time: O(C^2 * MN)
// Space: O(MN)
class Solution {
    bool removable(vector<vector<int>> &G, vector<vector<int>> &pos, int c) {
        for (int i = pos[c][0]; i <= pos[c][2]; ++i) {
            for (int j = pos[c][1]; j <= pos[c][3]; ++j) {
                if (G[i][j] != c && G[i][j] != 0) return false;
            }
        }
        for (int i = pos[c][0]; i <= pos[c][2]; ++i) {
            for (int j = pos[c][1]; j <= pos[c][3]; ++j) G[i][j] = 0;
        }
        return true;
    }
public:
    bool isPrintable(vector<vector<int>>& G) {
        int M = G.size(), N = G[0].size();
        vector<vector<int>> pos(61, {M, N, 0, 0});
        unordered_set<int> colors, remove;
        for (int i = 0; i < M; ++i) {
            for (int j = 0; j < N; ++j) {
                int c = G[i][j];
                colors.insert(c);
                pos[c][0] = min(pos[c][0], i);
                pos[c][1] = min(pos[c][1], j);
                pos[c][2] = max(pos[c][2], i);
                pos[c][3] = max(pos[c][3], j);
            }
        }
        while (colors.size()) {
            for (int c : colors) {
                if (removable(G, pos, c)) remove.insert(c);
            }
            if (remove.empty()) return false;
            for (int c : remove) colors.erase(c);
            remove.clear();
        }
        return true;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/strange-printer-ii/
1585. Leetcode Solution in CPP
Tags: cpp
Published: 2022-07-06 14:29:21

```
class Solution {
public:
    bool isTransformable(string s, string t) {
        queue<int> q[10];
        for (int i = 0; i < s.size(); ++i) q[s[i] - '0'].push(i);
        for (int i = 0; i < t.size(); ++i) {
            int d = t[i] - '0';
            if (q[d].empty()) return false;
            for (int j = 0; j < d; ++j) {
                if (q[j].size() && q[j].front() < q[d].front()) return false;
            }
            q[d].pop();
        }
        return true;
    }
};
```

#leetcode
#challenge
here is the link for the problem:
https://leetcode.com/problems/check-if-string-is-transformable-with-substring-sort-operations/