teleren/devto huggingface dataset:
1311. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 06:09:48

```
class Solution {
 public:
  vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos,
                                        vector<vector<int>>& friends, int id,
                                        int level) {
    vector<string> ans;
    vector<bool> visited(friends.size());
    visited[id] = true;
    queue<int> queue{{id}};
    unordered_map<string, int> count;
    set<pair<int, string>> freqAndVideo;

    for (int i = 0; i < level; ++i)
      for (int j = queue.size(); j > 0; --j) {
        for (int f : friends[queue.front()])
          if (visited[f] == false) {
            visited[f] = true;
            queue.push(f);
          }
        queue.pop();
      }

    for (int i = queue.size(); i > 0; --i) {
      for (const string& video : watchedVideos[queue.front()])
        ++count[video];
      queue.pop();
    }

    for (const auto& [video, freq] : count)
      freqAndVideo.insert({freq, video});

    for (const auto& [_, video] : freqAndVideo)
      ans.push_back(video);

    return ans;
  }
};

```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/get-watched-videos-by-your-friends/
1329. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 06:08:34

```
class Solution {
 public:
  vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
    const int m = mat.size();
    const int n = mat[0].size();

    unordered_map<int, priority_queue<int>> count;

    for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) count[i - j].push(mat[i][j]);

    for (int i = m - 1; i >= 0; --i)
      for (int j = n - 1; j >= 0; --j)
        mat[i][j] = count[i - j].top(), count[i - j].pop();

    return mat;
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/sort-the-matrix-diagonally
672. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 06:05:49

```
class Solution {
 public:
  int flipLights(int n, int m) {
    n = min(n, 3);

    if (m == 0)
      return 1;
    if (m == 1)
      return vector{2, 3, 4}[n - 1];
    if (m == 2)
      return vector{2, 4, 7}[n - 1];

    return pow(2, n);
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/bulb-switcher-ii/
1041. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 06:02:28

```
class Solution {
 public:
  bool isRobotBounded(string instructions) {
    int x = 0;
    int y = 0;
    int d = 0;
    vector<vector<int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    for (char instruction : instructions) {
      if (instruction == 'G') {
        x += directions[d][0];
        y += directions[d][1];
      } else if (instruction == 'L')
        d = (d + 3) % 4;
      else
        d = (d + 1) % 4;
    }

    return x == 0 && y == 0 || d > 0;
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/robot-bounded-in-circle
1155. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:58:52

```
class Solution {
    vector<unordered_map<int, int>> m;
    long mod = 1e9+7;
    int dfs(int d, int f, int target) {
        if (target < d || target > d * f) return 0;
        if (d == 1) return 1; 
        if (m[d].count(target)) return m[d][target];
        long cnt = 0;
        for (int j = 1; j <= f && j <= target; ++j) {
            int c = dfs(d - 1, f, target - j);
            cnt = (cnt + c) % mod;
        }
        return m[d][target] = cnt;
    }
public:
    int numRollsToTarget(int d, int f, int target) {
        m.assign(d + 1, {});
        return dfs(d, f, target);
    }
};
```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/
1191. Leetcode Solution in Java
Tags: java
Published: 2022-07-18 05:57:12

```
class Solution {
    public int kConcatenationMaxSum(int[] arr, int k) {
        final int MODULO = 1000000007;
        long maxSubArraySum = maxSubArraySum(arr);
        if (k == 1)
            return (int) (maxSubArraySum % MODULO);
        long sum = 0;
        long maxPrefixSum = 0;
        int length = arr.length;
        for (int i = 0; i < length; i++) {
            sum += arr[i];
            maxPrefixSum = Math.max(maxPrefixSum, sum);
        }
        long postfixSum = 0, maxPostfixSum = 0;
        for (int i = length - 1; i >= 0; i--) {
            postfixSum += arr[i];
            maxPostfixSum = Math.max(maxPostfixSum, postfixSum);
        }
        long maxSum = Math.max(maxSubArraySum, maxPostfixSum + maxPrefixSum);
        if (sum > 0) {
            maxSum = Math.max(maxSum, sum * k);
            maxSum = Math.max(maxSum, maxPostfixSum + sum * (k - 2) + maxPrefixSum);
            maxSum = Math.max(maxSum, Math.max(sum * (k - 1) + maxPrefixSum, maxPostfixSum + sum * (k - 1)));
        }
        return (int) (maxSum % MODULO);
    }

    public long maxSubArraySum(int[] arr) {
        int length = arr.length;
        long newSum = arr[0], max = arr[0];
        for (int i = 1; i < length; i++) {
            newSum = Math.max(newSum + arr[i], arr[i]);
            max = Math.max(max, newSum);
        }
        max = Math.max(max, 0);
        return max;
    }
}
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/k-concatenation-maximum-sum/
1291. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:55:57

```
class Solution {
public:
    vector<int> sequentialDigits(int low, int high) {
        int len_low = log10(low)+1;
        int len_high = log10(high)+1;
        
        vector<int> ans;
        
        for(int len = len_low; len <= len_high; ++len){
            int head_start = (len == len_low) ? low/pow(10, len_low-1) : 1;
            
            int cur = 0;
            for(int head = head_start; head + len - 1 <= 9; ++head){
                cur = 0;
                for(int i = 0; i < len; ++i){
                    cur += pow(10, len-1-i) * (head+i);
                }
                if(cur >= low && cur <= high) ans.push_back(cur);
            }
        }
        
        return ans;
    }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/sequential-digits/
1286. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:54:30

```
class CombinationIterator {
public:
    
    vector<string> comb;
    int top=0;
    void preprocess(string characters, int combinationLength, int start, string output)
    {
        if(characters.length()==0)
            return;
        if(output.length()==combinationLength)
        {
            comb.push_back(output);
            return;
        }
        for(int j = start;characters[j];j++)
            preprocess(characters,combinationLength,j+1,output+characters[j]);   
    }
    CombinationIterator(string characters, int combinationLength) {
        preprocess(characters,combinationLength,0,"");
    }
    
    string next() {
     return comb[top++];   
    }
    
    bool hasNext() {
        return (comb.size()>top);
    }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/iterator-for-combination/
1328. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:53:17

```
class Solution {
public:
    string breakPalindrome(string palindrome) {
        int N = palindrome.size(), end = N / 2;
        for (int i = 0; i < end; ++i) {
            if (palindrome[i] != 'a') {
                palindrome[i] = 'a';
                return palindrome;
            }
        }
        if (N > 1) {
            palindrome[N - 1] = 'b';
            return palindrome;
        }
        return "";
    }
};
```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/break-a-palindrome/
1813. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:51:22

```
class Solution {
 public:
  bool areSentencesSimilar(string sentence1, string sentence2) {
    if (sentence1.length() == sentence2.length())
      return sentence1 == sentence2;

    vector<string> words1 = split(sentence1);
    vector<string> words2 = split(sentence2);
    const int m = words1.size();
    const int n = words2.size();
    if (m > n)
      return areSentencesSimilar(sentence2, sentence1);

    int i = 0;  // words1's index
    while (i < m && words1[i] == words2[i])
      ++i;
    while (i < m && words1[i] == words2[i + n - m])
      ++i;

    return i == m;
  }

 private:
  vector<string> split(const string& sentence) {
    vector<string> words;
    istringstream iss(sentence);

    for (string s; iss >> s;)
      words.push_back(s);

    return words;
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/sentence-similarity-iii/
1252. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:49:43

```
class Solution {
public:
    int oddCells(int n, int m, vector<vector<int>>& indices) {
        vector<int> row(n, 0), col(m, 0);
        for (auto &v : indices) {
            row[v[0]]++;
            col[v[1]]++;
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if ((row[i] + col[j]) % 2) ans++;
            }
        }
        return ans;
    }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/
1446. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:48:17

```
class Solution {
public:
    int maxPower(string s) {
        if(s.size() == 0) return 0;
        
        int ans = 1; //need to initialize as 1, not 0!(imagine the case "j")
        int cur = 1; //need to initialize as 1, not 0!
        
        char last = s[0];
        for(int i = 1; i < s.size(); i++){
            if(s[i] == last){
                cur++;
            }
            
            if(s[i] != last || i == s.size()-1){
                ans = max(ans, cur);
                cur = 1;
            }
            
            last = s[i];
        }
        
        return ans;
    }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/consecutive-characters
Being a Web Developer - Getting Started
Tags: html, css, javascript, typescript
Published: 2022-07-18 05:47:26

## Do you know that:

When building a website, you work with three languages: HTML, CSS and JavaScript (JS). Broadly speaking: HTML defines the content which will appear on the page, CSS defines the visual style of the page, and JS defines the interactive behaviors of the page.

---

We describe having these sets of skills as being a "front-end" developer. You use three languages to create pages inside a web browser like Safari, Firefox, Edge or Chrome. Given how popular the web is for commerce and information sharing, there is a massive demand for people who are good at using these three languages.

---

Related to the role of being a "front-end" developer is the set of skills for the "back-end" developers, which are to create computer services that communicate either to a web browser (by passing it HTML/CSS/JS) or to another service (by sending data more directly.) You don't need to use HTML, CSS or JS to write this type of code, but it's usually an end-product of your work because it is likely to be presented in a web browser.

---


## Paul Ishaili C.

` Traditional Creative Director, Software Engineer and Tech Writer`
2011. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:46:51

```
class Solution {
 public:
  int finalValueAfterOperations(vector<string>& operations) {
    int ans = 0;

    for (const auto& op : operations)
      ans += op[1] == '+' ? 1 : -1;

    return ans;
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/final-value-of-variable-after-performing-operations/
1189. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:45:46

```
class Solution {
 public:
  int maxNumberOfBalloons(string text) {
    int ans = INT_MAX;
    vector<int> count(26);

    for (char c : text)
      ++count[c - 'a'];

    for (char c : string("ban"))
      ans = min(ans, count[c - 'a']);

    for (char c : string("lo"))
      ans = min(ans, count[c - 'a'] / 2);

    return ans;
  }
};

```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/maximum-number-of-balloons/
1089. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:44:52

```
class Solution {
 public:
  void duplicateZeros(vector<int>& arr) {
    int zeros = count_if(begin(arr), end(arr), [](int a) { return a == 0; });

    for (int i = arr.size() - 1, j = arr.size() + zeros - 1; i < j; --i, --j) {
      if (j < arr.size())
        arr[j] = arr[i];
      if (arr[i] == 0)
        if (--j < arr.size())
          arr[j] = arr[i];
    }
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/duplicate-zeros/
2006. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:43:03

```
class Solution {
 public:
  int countKDifference(vector<int>& nums, int k) {
    constexpr int kMax = 100;
    int ans = 0;
    vector<int> count(kMax + 1);

    for (const int num : nums)
      ++count[num];

    for (int i = k + 1; i <= kMax; ++i)
      ans += count[i] * count[i - k];

    return ans;
  }
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/count-number-of-pairs-with-absolute-difference-k/
2034. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:41:52

```
class StockPrice {
 public:
  void update(int timestamp, int price) {
    if (timestampToPrice.count(timestamp)) {
      const int prevPrice = timestampToPrice[timestamp];
      if (--pricesCount[prevPrice] == 0)
        pricesCount.erase(prevPrice);
    }
    timestampToPrice[timestamp] = price;
    ++pricesCount[price];
  }

  int current() {
    return rbegin(timestampToPrice)->second;
  }

  int maximum() {
    return rbegin(pricesCount)->first;
  }

  int minimum() {
    return begin(pricesCount)->first;
  }

 private:
  map<int, int> timestampToPrice;
  map<int, int> pricesCount;
};

```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/stock-price-fluctuation/
2075. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:41:03

```
class Solution {
    
public:
    string decodeCiphertext(string encodedText, int rows) {
    
        vector<string> matrix;
        string ret = "";
        int cols = encodedText.size() / rows;
        int i, j;
        int nowCol;
        for(i = 0; i < rows; ++i) {
    
            string matrixRow;
            for(j = 0; j < cols; ++j) {
    
                matrixRow.push_back( encodedText[i *cols + j] );
            }
            matrix.push_back(matrixRow);
        }
        for(nowCol = 0; nowCol < cols; ++nowCol) {
    
            for(i = 0; i < rows; ++i) {
    
                int r = i;
                int c = nowCol + i;
                if(c >= cols) break;
                ret.push_back (matrix[r][c]);
            }
        }
        while(ret.back() == ' ') {
    
            ret.pop_back();
        }
        return ret;

    }
};
```
#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/decode-the-slanted-ciphertext/
2344. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:39:12

```
class Solution {
public:
    int gcd(int a,int b)
    {
        if(a==0)
        {
            return b;
        }
        return gcd(b%a,a);
    }
    int minOperations(vector<int>& nums, vector<int>& numsDivide) {
        sort(numsDivide.begin(),numsDivide.end());
        int gc=numsDivide[0];
        for(int i=1;i<numsDivide.size();i++)
        {
            gc=gcd(numsDivide[i],gc);
            if(gc==1)
            {
                break;
            }
        }
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size();i++)
        {
            if(gc%nums[i]==0)
            {
                return i;
            }
        }
        return -1;
    }
};
```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/minimum-deletions-to-make-array-divisible/
815. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:35:52

```
class Solution {
 public:
  int numBusesToDestination(vector<vector<int>>& routes, int S, int T) {
    if (S == T)
      return 0;

    unordered_map<int, vector<int>> graph;  // {route: [buses]}
    unordered_set<int> usedBuses;

    for (int i = 0; i < routes.size(); ++i)
      for (const int route : routes[i])
        graph[route].push_back(i);

    int ans = 0;
    queue<int> q{{S}};

    while (!q.empty()) {
      ++ans;
      for (int sz = q.size(); sz > 0; --sz) {
        const int route = q.front();
        q.pop();
        for (const int bus : graph[route])
          if (usedBuses.insert(bus).second)
            for (const int nextRoute : routes[bus])
              if (nextRoute == T)
                return ans;
              else
                q.push(nextRoute);
      }
    }

    return -1;
  }
};

```

#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/bus-routes/
210. Leetcode Solution in Cpp
Tags: cpp
Published: 2022-07-18 05:34:31

```
enum class State { INIT, VISITING, VISITED };

class Solution {
 public:
  vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
    vector<int> ans;
    vector<vector<int>> graph(numCourses);
    vector<State> state(numCourses);

    for (const auto& p : prerequisites)
      graph[p[1]].push_back(p[0]);

    for (int i = 0; i < numCourses; ++i)
      if (hasCycle(graph, i, state, ans))
        return {};

    reverse(begin(ans), end(ans));
    return ans;
  }

 private:
  bool hasCycle(const vector<vector<int>>& graph, int u, vector<State>& state,
                vector<int>& ans) {
    if (state[u] == State::VISITING)
      return true;
    if (state[u] == State::VISITED)
      return false;

    state[u] = State::VISITING;
    for (const int v : graph[u])
      if (hasCycle(graph, v, state, ans))
        return true;
    state[u] = State::VISITED;
    ans.push_back(u);

    return false;
  }
};

```


#leetcode
#challenge
Here is the link for the problem:
https://leetcode.com/problems/course-schedule-ii/
Team Today - The simplest way to manage and plan hybrid working
Tags: remote
Published: 2022-07-18 05:29:26

Team Today is a web app and MS Teams app that lets people set and see when and where their colleagues are working from - if it's at home, in the office (and whereabouts in the office), on leave, offsite, or if it's their non-working day. This makes it easier for people to plan when to be in the office and to see when their friends and colleagues are working.

In addition to this key feature, Team Today also offers optional features that let companies handle office or desk bookings, show office floor plans with hot spots, manage annual leave (with or without an approval process), add key responsibilities to users (fire wardens, first aiders, etc), and add custom whereabouts to personalize the app to your company.

It’s these features and our approach to prioritizing the experience of a typical user which has made Team Today as popular as it is in the companies that use it.

https://team-today.com
How I reduced clients website loading speed from 10 to 2 sec? Time: 2 hours, Charged: $500
Tags: webdev, tutorial, computerscience, wordpress
Published: 2022-07-18 04:41:17

Few days back...

I optimized a client's website loading speed from 10 sec to < 2 sec

Time spent: 2 hours
Charged client: $500

Here are 8 steps I did which You can follow them to optimize website made with any CMS or technologies:

First, why should you make any website faster?
- ✔️ Decrease bounce rate by 57% (as per study)
- ✔️ Increases google ranking
- ✔️ Happy Visitors/Customers

let's check the steps 👇

## 1/ Analysing website:

➡ Go to http://gtmetrix.com 

➡ Enter the URL and hit "Test your site"

➡ In few secs you'll see complete report containing GTmetrix Grade, Performance & structure.

(GT metric report)
![GT metric report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7n4rjihr8p2kvnxgv88p.png)

➡ In GT metric report scroll down to "Top Issues"

➡ Which will show issues related to JS, CSS, Images, too many requests, page size & more

👉 GTmetrix will show most of the improvements, BUT NOT ALL

➡ Let's resolve issues pointed by GT metric

## 2/ Image size optimization:

➡ Reduce all image sizes from MB to KB

➡ Go to http://squoosh.app
➡ Upload original images
➡ Download compressed images
➡ Replace in your website

## 3/ Lazy load images:

➡ Lazy loads defer loading of images until user scrolls near them

➡ Do only if there are lot of images

➡ Just add loading="lazy" in img tags

(check screenshot)

![Lazy load images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3z8sqf95a2gnl8fore0.jpg)

## 4/ Caching js & css

➡ Caching assets loads them once & stores them in browser cache

➡ This reduces loading time of subsequent pages

➡ Cache them by adding an asset version

(check screenshot)
 
![Caching js & css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cc2b544zjmwa4t8ryqra.jpg)

## 5/ Minify JS & CSS:

➡ Minifying JS & CSS will reduce their size by almost 70%

➡ Go to http://smalldev.tools, search js/css minifier 

➡ Minify all js & CSS

PS: keep a backup of non-minified files in case it may be required later

## 6/ Move JS to end of document:

➡ JS blocks all other resources
➡ This increases "First Contentful Paint" time
➡ Move them to end of HTML document

## 7/ Brotli compression:

➡ It reduces HTML size by almost 80%

➡ Go to http://giftofspeed.com/gzip-test/ & check if enabled or not

➡ If not, contact hosting to enable it

➡ Or use cloudflare to use this for free

**Now we're done with optimization suggested by GTmetrix**

➡ Re-test your website & you'll see a good improvement 🤩

➡ but not yet 100% optimized

Let's optimize more...

## 8/ Database optimization:

Unoptimized DB can reduce speed by a lot

DB optimization can be a big topic, but doing this basic will help

➡ Add indexing to fields used for searching, sorting & table joins (basic)

---

This is my first post. Hope this post helps! 

Comment below for an questions you have.

_Connect me on [Twitter ](https://twitter.com/HeyNikhila) and [LinkedIn](https://www.linkedin.com/in/nikhil-agrawal-21b31833/)._