teleren/devto huggingface dataset:
1697. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:44:10

```
class UF {
 public:
  UF(int n) : id(n) {
    iota(begin(id), end(id), 0);
  }

  void union_(int u, int v) {
    id[find(u)] = find(v);
  }

  int find(int u) {
    return id[u] == u ? u : id[u] = find(id[u]);
  }

 private:
  vector<int> id;
};

class Solution {
 public:
  vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList,
                                         vector<vector<int>>& queries) {
    vector<bool> ans(queries.size());
    UF uf(n);

    for (int i = 0; i < queries.size(); ++i)
      queries[i].push_back(i);

    sort(begin(queries), end(queries),
         [](const auto& a, const auto& b) { return a[2] < b[2]; });
    sort(begin(edgeList), end(edgeList),
         [](const auto& a, const auto& b) { return a[2] < b[2]; });

    int i = 0;  // i := edgeList's index
    for (const auto& q : queries) {
      // union edges whose distances < limit (q[2])
      while (i < edgeList.size() && edgeList[i][2] < q[2])
        uf.union_(edgeList[i][0], edgeList[i++][1]);
      if (uf.find(q[0]) == uf.find(q[1]))
        ans[q.back()] = true;
    }

    return ans;
  }
};

```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths/
1774. Leetcode Solution in Java
Tags: java
Published: 2022-07-15 06:42:36

```
class Solution {
    public int closestCost(int[] baseCosts, int[] toppingCosts, int target) {
        int closest = Integer.MAX_VALUE;
        for (int baseCost : baseCosts)
            closest = Math.min(closest, baseCost);
        int minDifference = Math.abs(closest - target);
        int toppingsCount = toppingCosts.length;
        int totalToppings = (int) Math.pow(3, toppingsCount);
        for (int i = 0; i < totalToppings; i++) {
            int[] combination = getCombination(toppingsCount, i);
            int toppingCost = getToppingCost(toppingCosts, combination);
            for (int baseCost : baseCosts) {
                int totalCost = toppingCost + baseCost;
                int difference = Math.abs(totalCost - target);
                if (difference < minDifference || difference == minDifference && totalCost < closest) {
                    closest = totalCost;
                    minDifference = difference;
                }
            }
        }
        return closest;
    }

    public int[] getCombination(int length, int index) {
        int[] combination = new int[length];
        for (int i = length - 1; i >= 0 && index > 0; i--) {
            int remainder = index % 3;
            combination[i] = remainder;
            index /= 3;
        }
        return combination;
    }

    public int getToppingCost(int[] toppingCosts, int[] combination) {
        int cost = 0;
        int length = toppingCosts.length;
        for (int i = 0; i < length; i++)
            cost += toppingCosts[i] * combination[i];
        return cost;
    }
}
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/closest-dessert-cost/
Django-Start Script ✨
Tags: django, python, webdev
Published: 2022-07-15 06:41:26

## Hi there 👋
### New version of Django Start v1.0.0-beta.2 🎊

> This script saves time for you to start a new Django project. It creates the project and creates the first app and then updates the project settings file and also updates the project URLs file and then creates a views and configures the application URLs file and also creates the index.html

---

**🐞 Fixes Bugs:**
- Fix the error of repeating the application name within the settings.py file
- Fix the error of repeating the application link within the urls.py file
- Fix the error of re-creating a file views.py
- Fixing the error of fixing unnecessary dependencies
- Hide the required package installation output from the periphery

---

**Features added 🧾:**
- Check to get the latest version of Django-Start
- Automatic Update Pip Package Manager
- Create a file requirements.txt
- Support Linux platform

https://github.com/islam-kamel/django-start
1721. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:40:34

```
class Solution {
public:
    ListNode* swapNodes(ListNode* head, int k) {
        ListNode *x=head, *y=head, *t=head;

        // Until we reach k-th node from beginning
        while(k>1){
            x = x->next;
            t = t->next;
            k--;
        }

        // Until pointer t reach last need 
        while(t->next){
            y=y->next;
            t=t->next;
        }

        // Swap values at both the nodes
        int temp = x->val;
        x->val = y->val;
        y->val = temp;

        return head;
    }
};
```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/swapping-nodes-in-a-linked-list/
1701. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:39:20

```
class Solution {
 public:
  double averageWaitingTime(vector<vector<int>>& customers) {
    double wait = 0;
    double curr = 0;

    for (const auto& c : customers) {
      curr = max(curr, 1.0 * c[0]) + c[1];
      wait += curr - c[0];
    }

    return 1.0 * wait / customers.size();
  }
};

```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/average-waiting-time/
1770. Leetcode Solution in Java
Tags: java
Published: 2022-07-15 06:37:11

```
class Solution {
  public int maximumScore(int[] nums, int[] multipliers) {
    // dp[s][i] := max score of nums[s..e] and multipliers[i],
    dp = new Integer[multipliers.length][multipliers.length];
    return maximumScore(nums, 0, multipliers, 0);
  }

  private Integer[][] dp;

  int maximumScore(int[] nums, int s, int[] multipliers, int i) {
    if (i == multipliers.length)
      return 0;
    if (dp[s][i] != null)
      return dp[s][i];

    // # of nums picked on the start side = s,
    // # of nums picked on the end side = i - s,
    // so e = n - (i - s) - 1
    final int e = nums.length - (i - s) - 1;
    final int pickStart = nums[s] * multipliers[i] + maximumScore(nums, s + 1, multipliers, i + 1);
    final int pickEnd = nums[e] * multipliers[i] + maximumScore(nums, s, multipliers, i + 1);
    return dp[s][i] = Math.max(pickStart, pickEnd);
  }
}

```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/
1834. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:33:28

```
class Solution {
 public:
  vector<int> getOrder(vector<vector<int>>& tasks) {
    const int n = tasks.size();

    // add index information
    for (int i = 0; i < tasks.size(); ++i)
      tasks[i].push_back(i);

    vector<int> ans;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> minHeap;
    int i = 0;      // tasks' pointer
    long time = 0;  // current time

    sort(begin(tasks), end(tasks));
    while (i < n || !minHeap.empty()) {
      if (minHeap.empty())
        time = max(time, static_cast<long>(tasks[i][0]));
      while (i < n && time >= tasks[i][0]) {
        minHeap.emplace(tasks[i][1], tasks[i][2]);
        ++i;
      }
      const auto [procTime, index] = minHeap.top();
      minHeap.pop();
      time += procTime;
      ans.push_back(index);
    }

    return ans;
  }
};

```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/single-threaded-cpu/
1850. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:31:54

```
class Solution {
 public:
  int getMinSwaps(string num, int k) {
    string perm = num;

    while (k--)
      next_permutation(begin(perm), end(perm));

    return countSteps(num, perm);
  }

 private:
  int countSteps(const string& A, string& B) {
    int count = 0;

    for (int i = 0, j = 0; i < A.length(); ++i) {
      j = i;
      while (A[i] != B[j])
        ++j;
      while (i < j) {
        swap(B[j], B[j - 1]);
        --j;
        ++count;
      }
    }

    return count;
  }
};

```


#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/
1895. Leetcode Solution in Java
Tags: java
Published: 2022-07-15 06:30:50

```
class Solution {
    public int largestMagicSquare(int[][] grid) {
        int m = grid.length, n = grid[0].length;
        for (int side = Math.min(m, n); side > 1; side--) {
            if (existMagicSquare(grid, m, n, side))
                return side;
        }
        return 1;
    }

    public boolean existMagicSquare(int[][] grid, int m, int n, int side) {
        int maxRow = m - side, maxColumn = n - side;
        for (int i = 0; i <= maxRow; i++) {
            for (int j = 0; j <= maxColumn; j++) {
                if (isMagicSquare(grid, i, j, side))
                    return true;
            }
        }
        return false;
    }

    public boolean isMagicSquare(int[][] grid, int startRow, int startColumn, int side) {
        Set<Integer> set = new HashSet<Integer>();
        int[] rowSums = new int[side];
        int[] columnSums = new int[side];
        int diagonal1 = 0, diagonal2 = 0;
        for (int i = 0; i < side; i++) {
            for (int j = 0; j < side; j++) {
                rowSums[i] += grid[startRow + i][startColumn + j];
                columnSums[j] += grid[startRow + i][startColumn + j];
                if (i == j)
                    diagonal1 += grid[startRow + i][startColumn + j];
                if (i + j == side - 1)
                    diagonal2 += grid[startRow + i][startColumn + j];
            }
        }
        for (int i = 0; i < side; i++) {
            set.add(rowSums[i]);
            set.add(columnSums[i]);
        }
        set.add(diagonal1);
        set.add(diagonal2);
        return set.size() == 1;
    }
}
```


#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/largest-magic-square/
44. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:28:07

```
class Solution {
 public:
  bool isMatch(string s, string p) {
    const int m = s.length();
    const int n = p.length();
    // dp[i][j] := true if s[0..i) matches p[0..j)
    vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
    dp[0][0] = true;

    auto isMatch = [&](int i, int j) -> bool {
      return j >= 0 && p[j] == '?' || s[i] == p[j];
    };

    for (int j = 0; j < p.length(); ++j)
      if (p[j] == '*')
        dp[0][j + 1] = dp[0][j];

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j)
        if (p[j] == '*') {
          const bool matchEmpty = dp[i + 1][j];
          const bool matchSome = dp[i][j + 1];
          dp[i + 1][j + 1] = matchEmpty || matchSome;
        } else if (isMatch(i, j)) {
          dp[i + 1][j + 1] = dp[i][j];
        }

    return dp[m][n];
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/wildcard-matching/
23. Leetcode Solution in cpp
Tags: cpp
Published: 2022-07-15 06:27:01

```
class Solution {
 public:
  ListNode* mergeKLists(vector<ListNode*>& lists) {
    ListNode dummy(0);
    ListNode* curr = &dummy;
    auto compare = [](ListNode* a, ListNode* b) { return a->val > b->val; };
    priority_queue<ListNode*, vector<ListNode*>, decltype(compare)> minHeap(
        compare);

    for (ListNode* list : lists)
      if (list)
        minHeap.push(list);

    while (!minHeap.empty()) {
      ListNode* minNode = minHeap.top();
      minHeap.pop();
      if (minNode->next)
        minHeap.push(minNode->next);
      curr->next = minNode;
      curr = curr->next;
    }

    return dummy.next;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/merge-k-sorted-lists/
4. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:19:58

```
class Solution {
 public:
  double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    const int n1 = nums1.size();
    const int n2 = nums2.size();
    if (n1 > n2)
      return findMedianSortedArrays(nums2, nums1);

    int l = 0;
    int r = n1;

    while (l <= r) {
      const int partition1 = (l + r) / 2;
      const int partition2 = (n1 + n2 + 1) / 2 - partition1;
      const int maxLeft1 = partition1 == 0 ? INT_MIN : nums1[partition1 - 1];
      const int maxLeft2 = partition2 == 0 ? INT_MIN : nums2[partition2 - 1];
      const int minRight1 = partition1 == n1 ? INT_MAX : nums1[partition1];
      const int minRight2 = partition2 == n2 ? INT_MAX : nums2[partition2];
      if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1)
        return (n1 + n2) % 2 == 0
                   ? (max(maxLeft1, maxLeft2) + min(minRight1, minRight2)) * 0.5
                   : max(maxLeft1, maxLeft2);
      else if (maxLeft1 > minRight2)
        r = partition1 - 1;
      else
        l = partition1 + 1;
    }

    throw;
  }
};
```
#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/median-of-two-sorted-arrays/
2248. Leetcode Solution in Cpp
Tags: challenge, cpp
Published: 2022-07-15 06:17:12

```
class Solution {
 public:
  vector<int> intersection(vector<vector<int>>& nums) {
    vector<int> ans;
    vector<int> count(1001);

    for (const auto& A : nums)
      for (const int a : A)
        ++count[a];

    for (int i = 1; i < 1001; ++i)
      if (count[i] == nums.size())
        ans.push_back(i);

    return ans;
  }
};

```
#leetcode
#weekly contest
Here is the link for the problem.....
https://leetcode.com/contest/weekly-contest-290/problems/intersection-of-multiple-arrays/
2250. Leetcode Solution in Cpp
Tags: challenge, cpp
Published: 2022-07-15 06:16:13

```
class Solution {
public:
    const int HMAX = 100;
    vector<int> countRectangles(vector<vector<int>>& rects, vector<vector<int>>& points) {
        vector<vector<int>> vec(HMAX + 1);
        for (auto &r : rects)
        {
            int x = r[0], y = r[1];
            vec[y].emplace_back(x);
        }
        for (auto &v : vec)
            sort(begin(v), end(v));
        
        int n = points.size();
        vector<int> res(n, 0);
        for (int i = 0; i < n; ++i)
        {
            int x = points[i][0], y = points[i][1];
            for (int h = y; h <= HMAX; ++h)
            {
                auto itor = lower_bound(begin(vec[h]), end(vec[h]), x);
                res[i] += std::distance(itor, end(vec[h]));
            }
        }
        return res;
    }
};
```
#leetcode
#weekly contest
Here is the link for the problem.....
https://leetcode.com/contest/weekly-contest-290/problems/count-number-of-rectangles-containing-each-point/
2249. Leetcode Solution in Cpp
Tags: challenge, cpp
Published: 2022-07-15 06:15:30

```
class Solution {
 public:
  int countLatticePoints(vector<vector<int>>& circles) {
    int ans = 0;

    for (int x = 0; x < 201; ++x)
      for (int y = 0; y < 201; ++y)
        ans += any_of(begin(circles), end(circles), [&](const auto& c) {
          return (c[0] - x) * (c[0] - x) + (c[1] - y) * (c[1] - y) <=
                 c[2] * c[2];
        });

    return ans;
  }
};

```

#leetcode
#weekly contest
Here is the link for the problem.....
https://leetcode.com/contest/weekly-contest-290/problems/count-lattice-points-inside-a-circle/
2251. Leetcode Solution in Cpp
Tags: challenge, cpp
Published: 2022-07-15 06:09:28

```
class Solution {
 public:
  vector<int> fullBloomFlowers(vector<vector<int>>& flowers,
                               vector<int>& persons) {
    vector<int> ans;
    vector<int> starts;
    vector<int> ends;

    for (const auto& f : flowers) {
      starts.push_back(f[0]);
      ends.push_back(f[1]);
    }

    sort(begin(starts), end(starts));
    sort(begin(ends), end(ends));

    for (const int p : persons) {
      const int started =
          upper_bound(begin(starts), end(starts), p) - begin(starts);
      const int ended = lower_bound(begin(ends), end(ends), p) - begin(ends);
      ans.push_back(started - ended);
    }

    return ans;
  }
};

```
#leetcode
#weekly contest
Here is the link for the problem.....
https://leetcode.com/contest/weekly-contest-290/problems/number-of-flowers-in-full-bloom/
DDEV meets vite3
Tags: ddev, vite, devops, php
Published: 2022-07-15 06:01:34

That was my private dev challenge of the week: Getting vite running with the awesome open source PHP docker tool [DDEV](http://ddev.com/). 

![Screenshot of example page with devtools open, devtools console reads vite hot updated: /styles/example.css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2v2kwnvnjqt34mdtio8d.png)  

Repo: https://github.com/mandrasch/vite-php-setup-ddev-test

I'm glad that this works now!

For the new laravel vite integration in the starter kits there is some more work needed, I keep you posted ;-)

Don't forget to join [DDEV discord](https://discord.gg/hCZFfAMc5k) & meet the community!
DevOps Tools To Be Used
Tags: devops, programming, webdev, beginners
Published: 2022-07-15 04:40:38

### BitBucket
Like GitHub, BitBucket is also an application that helps to manage projects' code throughout the development process. Although GitHub is still regarded as the most popular repository, many users are switching to BitBucket due to its lower costs and the ability to use the private repository (a feature that is only available in the paid version of GitHub).While the fundamental features of BitBucket has a lot in common with GitHub and features like simple Integration with Jira and Trello and built-in CI/CD capabilities can give this tool from Atlassian an advantage.

## GitHub
It was launched in 2000, GitHub remains as one of the most popular DevOps tools for collaboration that is easy. With the tool developers are able to perform rapid revisions to code, and the notification of that is instantly sent to the team members. In the event of an error or fallout, instant rollbacks are possible to the previous version in moments, thanks to the history of changes that is branched that are saved continuously within the program.

## Docker
Docker is an application that is at the heart of containerization, a movement which is quickly growing in popularity within the IT world. Docker provides secure packaging, deployment and running of apps regardless of the operating environment. Each container for an application contains source code, the supporting files including runtime, configuration files as well as other files. that are responsible for execution of the application.

Utilizing the Docker Engine as a platform, containers can be accessed , which allows applications to be executed remotely. This application has helped businesses to lower their the cost of infrastructure. According to a study 2/3 companies that have used the app have embraced it within the first 30 days of implementing this app.

## Jenkins
A continuous integration server that is open source, Jenkins automates the complete process of building an software project. Jenkins' USP of this program lies in the Pipeline feature that it provides that can be used by developers to automate commit code to the repository, tests, as well in fetching reports generated following the testing.

The highly configurable tool gives instant feedback , and can inform you when a specific stage is causing the destruction of a build or harming another. The majority of tasks and tools used within SDLC could be automated by Jenkins and allows team members to boost the speed of their work.

## Sentry
Sentry, a tool utilized by companies like Uber and Microsoft is among the top DevOps tools for bug or error detection. The tool is free and is compatible with languages like Ruby, IOS, JavaScript and more. as well as has built-in SDKs that can be customized to work with all frameworks and languages.

The tool constantly scans code lines throughout the entire system and will notify you if it detects an error or a problem. It not only identifies the issue, but also provides various solutions that are incorporated by just a click.
33. Leetcode Solution in cpp
Tags: cpp
Published: 2022-07-15 06:01:06

```
class Solution {
 public:
  int search(vector<int>& nums, int target) {
    int l = 0;
    int r = nums.size() - 1;

    while (l <= r) {
      const int m = (l + r) / 2;
      if (nums[m] == target)
        return m;
      if (nums[l] <= nums[m]) {  // nums[l..m] are sorted
        if (nums[l] <= target && target < nums[m])
          r = m - 1;
        else
          l = m + 1;
      } else {  // nums[m..n - 1] are sorted
        if (nums[m] < target && target <= nums[r])
          l = m + 1;
        else
          r = m - 1;
      }
    }

    return -1;
  }
};

```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/search-in-rotated-sorted-array/
74. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 06:00:13

```
class Solution {
 public:
  bool searchMatrix(vector<vector<int>>& matrix, int target) {
    if (matrix.empty())
      return false;

    const int m = matrix.size();
    const int n = matrix[0].size();
    int l = 0;
    int r = m * n;

    while (l < r) {
      const int mid = (l + r) / 2;
      const int i = mid / n;
      const int j = mid % n;
      if (matrix[i][j] == target)
        return true;
      if (matrix[i][j] < target)
        l = mid + 1;
      else
        r = mid;
    }

    return false;
  }
};

```


#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/search-a-2d-matrix/
My Leetcode Challenge
Tags: javascript, beginners, algorithms
Published: 2022-07-15 05:57:01

Helloworld from **Taiwan**! This is [Michael](https://github.com/miccwan). I'm a **junior JS fullstack engineer** who just graduated from university. I'm going to practice my English writing skill and programming skill before being enlisted in the army(mid October).

Starting today, I will solve some leetcode problems (with JS) about three times a week, one hour at a time, and post the progress here. Any suggestions or corrections are welcome!

The code will be collected in this Github repository:

{% embed https://github.com/MiccWan/leetcode %}

### Posts

1. [Binary Search](https://dev.to/miccwan/leetcode-practice-day-1-1k) (7/15)
1. [Two Pointers #1](https://dev.to/miccwan/leetcode-practice-2-51pg) (7/17)
1. [Two Pointers #2](https://dev.to/miccwan/leetcode-challenge-3-1ki0) (7/20)
695. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-15 05:37:04

```
class Solution {
 public:
  int maxAreaOfIsland(vector<vector<int>>& grid) {
    int ans = 0;

    for (int i = 0; i < grid.size(); ++i)
      for (int j = 0; j < grid[0].size(); ++j)
        ans = max(ans, dfs(grid, i, j));

    return ans;
  }

 private:
  int dfs(vector<vector<int>>& grid, int i, int j) {
    if (i < 0 || i == grid.size() || j < 0 || j == grid[0].size())
      return 0;
    if (grid[i][j] != 1)
      return 0;

    grid[i][j] = 2;

    return 1 + dfs(grid, i + 1, j) + dfs(grid, i - 1, j)
             + dfs(grid, i, j + 1) + dfs(grid, i, j - 1);
  }
};

```

#leetcode
#challenge
Here is the link for the problem.....
https://leetcode.com/problems/max-area-of-island/