teleren/devto huggingface dataset:
977. Squares of a Sorted Array [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:54:50

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 977. Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
	   // Brute Force Solution Time O(NlogN) & Auxiliary Space O(1)
       int len=nums.size();
        for(int i=0;i<len;i++)
            nums[i]=nums[i]*nums[i];
        sort(nums.begin(),nums.end());
        return nums;
    }
};
```

**_Efficient Solution:_**

```javascript
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        // Efficient Solution Time O(N) & Auxiliary Space O(1)
		// Two Pointer Method
        int len=nums.size(), lt=0, rt=len;
        vector<int> res;
	    // Find index of first positive vector element & position right pointer
        for(int i=0;i<len;i++){
            if(nums[i]>=0){
                rt=i;
                break;
            }
        }
	    // Position left pointer at smallest negative vector element
        lt=rt-1;
        while(lt>=0 && rt<=len-1){
	    // Compare elements at left & right index and push the square of  
	    // the smaller element in the resultant vector in ascending order
	    // and reposition the left(decrement) & right(increment) pointers
            if(abs(nums[lt])<nums[rt]){
                res.push_back(nums[lt]*nums[lt]);
                lt--;
            }
            else if(abs(nums[lt])>nums[rt]){
                res.push_back(nums[rt]*nums[rt]);
                rt++;
            }
            else{
                res.push_back(nums[lt]*nums[lt]);
                lt--;
                res.push_back(nums[rt]*nums[rt]);
                rt++;
            }  
        }
	    // Push the square of the element in the resultant vector for the
	    // case of unvisited elements
        while(lt>-1){
              res.push_back(nums[lt]*nums[lt]);
              lt--;
            } 
        while(rt<len){
              res.push_back(nums[rt]*nums[rt]);
              rt++;
            }
        return res;
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
724. Find Pivot Index [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:42:11

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 724. Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    int pivotIndex(vector<int>& nums) {
    // Brute Force Solution Time O(N^2) & Auxiliary Space O(1)
    // Calculate the sum of elements to the left & right
    // of each index of nums vector and compare them to check equivalence
    int len=nums.size();
    if(len==1)
        return nums[0];
    for(int i=0;i<len;i++){
		 // accumulate() takes O(N) time and for loop runs for n iterations.
		 // So time taken by for loop is O(n*n)=O(N^2)
          int left=accumulate(nums.begin(),nums.begin()+i,0); 
          int right=accumulate(nums.begin()+i+1,nums.end(),0);
          if(left==right)
              return i;
    }
      return -1;
    }
};

```

**_Efficient Solution:_**

```javascript
class Solution {
public:
    int pivotIndex(vector<int>& nums) {
    // Efficient Solution Time O(N) & Auxiliary Space O(1)
    // Create a sum array in which each element stores 
    // the sum of elements of nums vector up to that index.
    // Calculate left & right sum. For example-
    // nums[]=[1,7,3,6,5,6]
    // sum[]= [1,8,11,17,22,28]
    // left[3]=17-6=11
    // right[3]=28-17=11
    // return 3
    int len=nums.size();
    if(len==1)
        return 0;
    int sum[len];
    sum[0]=nums[0];
    for(int i=1;i<len;i++){
        sum[i]=nums[i]+sum[i-1];
    }
    for(int i=0;i<len;i++)
    {
        int left=sum[i]-nums[i];
        int right=sum[len-1]-sum[i];
            
    if(left==right)
        return i;
    }
      return -1;
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
169. Majority Element [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:33:33

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 169. Majority Element](https://leetcode.com/problems/majority-element/)

---

**_Brute Force Solution : Time O(NlogN) & Auxiliary Space O(1)_**

```javascript
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        // Brute Force Solution
        sort(nums.begin(),nums.end());
        return nums[nums.size()/2];
    }
};


```
**_Better Solution : Time O(N) & Auxiliary Space O(N)_**

```javascript
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        // Better Solution
        map<int,int> m;
        int len=nums.size(), pos;
        for(int i=0;i<len;i++){
            m[nums[i]]++;
        if(m[nums[i]]>(len/2))
            pos=i;
        }
        return nums[pos];
    }
};

```

**_Optimal Solution : Time O(N) & Auxiliary Space O(1)_**

```javascript
class Solution {
public:
    int majorityElement(vector<int>& nums) {
        // Optimal Solution
        // Boyer–Moore Majority Voting Algorithm
        int len=nums.size(), maj=nums[0], count=1;
        for(auto i=1;i<len;i++){
            if(nums[i]==maj)
                count++;
            else
                count--;
            if(count==0){
                maj=nums[i];
                count=1;
            }
        }
        return maj;
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
122. Best Time to Buy and Sell Stock II [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:23:45

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 122. Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/)

---
e.g. Array=[7,1,5,3,6,4]
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9er53snk93s52eat62zx.png)
**_Valley to Peak approach : Add all gains from positive slopes i.e. profit = (5-1) + (6-3) = 7_**

**_Efficient Solution:_**

```javascript
class Solution {
public:
    int maxProfit(vector<int>& prices) {
		// Optimal Solution Time O(N) & Auxiliary Space O(1)
        int res = 0;
        for (int i = 1; i < prices.size(); ++i)
            res += max(prices[i] - prices[i - 1], 0);
        return res;
    }
};
```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
121. Best Time to Buy and Sell Stock [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:18:27

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 121. Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    int maxProfit(vector<int>& prices) { 
    // Brute Force Solution Time O(N^2) & Auxiliary Space O(1)
    int len=prices.size(),profit=0;
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if(prices[j]>prices[i]){
                    int pro=prices[j]-prices[i];
                    profit=max(profit,pro);
                }
            }
        }
    return profit; 
  }
};

```

**_Efficient Solution:_**

```javascript
class Solution {
public:
    int maxProfit(vector<int>& prices) {
    // Efficient Solution Time O(N) & Auxiliary Space O(1)
    int min_val=INT_MAX,profit=0,len=prices.size();
    for(int i=0;i<len;i++){
          min_val=min(min_val,prices[i]);
          profit=max(profit,prices[i]-min_val);
        }
    return profit;
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
Import Oracle DB Schema via Data Pump
Tags: oracle11g
Published: 2021-12-24 01:17:33

Continue from [Export Oracle DB Schema via Data Pump](https://dev.to/nhisyamj/export-oracle-db-schema-via-data-pump-13a6). Now we want to import DB dumpfile.

1. `cd /u01/app/oracle/product/11.2.0.4/dbhome_1/bin`
2. Run this command to import dump file
`impdp directory=<dir.name> dumpfile=<dumpfile.name>.dmp logfile=<logfile.name>.log remap_schema='<schema.name>:<schema.name>'`
118. Pascal's Triangle [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:16:29

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 118. Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    vector<vector<int>> generate(int numRows) {
	// Brute Force Solution Time O(N^2) & Auxiliary Space O(1)
	vector<vector<int>> ret(numRows);
	for (int i = 0; i < numRows; i++) {
	ret[i].resize(i+1); // Resize number of elements in each row from from numRows to i+1 
        ret[i][0]=1; ret[i][i]=1; // Put 1's at first & last element of each row
	for (int j = 1; j < i; j++) {
		// Assigning element values in row from second element to second last element
		// by adding corresponding jth & (j-1)th element in previous row
		// as shown in question animation
		ret[i][j] = ret[i - 1][j] + ret[i - 1][j - 1];
	}
	}
	return ret;
}
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
88. Merge Sorted Array [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:13:42

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 88. Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { 
    // Brute force approach Time O(NlogN) & Auxiliary Space O(1)
    // Just overwrite nums2 on nums1 starting 
    // from the point where non zero element ends in nums1 
    for(int i=m;i<m+n;i++){
        nums1[i]=nums2[i-m];
    }
    // Sort function has O(NlogN) time complexity
        sort(nums1.begin(),nums1.end()); 
  }
};

```

**_Efficient Solution:_**

```javascript
class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { 
    // Optimal Solution Time O(N) & Auxiliary Space O(1)
    // Two pointer approach 
    int p1 = m - 1, p2 = n - 1, curr=m+n-1;
    for (int i = curr; i >= 0 && p2 >=0 ;i--) {  
            if (p1 >= 0 && nums1[p1] > nums2[p2]) 
                nums1[i] = nums1[p1--]; 
            else 
                nums1[i] = nums2[p2--];
        }  
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
56. Merge Intervals [Leetcode][C++]
Tags: cpp, algorithms, leetcode, programming
Published: 2021-12-24 01:11:46

**_All suggestions are welcome. Please upvote if you like it. Thank you._**

---

[Leetcode Problem Link: 56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)

---

**_Brute Force Solution:_**

```javascript
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        // Brute Force Solution Time O(N^2) & Auxiliary Space O(1)
        int len=intervals.size();
        if(len<=1)
            return intervals;
        sort(intervals.begin(),intervals.end()); // Time taken by sort() is O(NlogN)
        vector<vector<int>> res;
        for(int i=0;i<len;i++){
            int a=intervals[i][0];
            int b=intervals[i][1];
            // for loop inside for loop takes time of O(N^2)
            for(int j=i+1;j<len;j++){
                int c=intervals[j][0];
                int d=intervals[j][1];
                if(b>=c){ 
                    // Comparing pairs : (a,b) & (c,d)
                    // Interval overlap condition example - a=3,b=7,c=5,d=9
                    //  Real Line---a(3)------c(5)******b(7)-------d(9)----
                    // (a,max(b,d)) should be inserted in result(res) vector.
                    // b will only get updated if there is an overlap 
                    // so as to merge maximum number of intervals
                    b=max(b,d);
                    // i pointer should now point to the pair pointed by j 
                    // and in next iteration of j loop, j will point to the 
                    // pair next to the one pointed by this i
                    i=j;
                }
            } 
            res.push_back({a,b});
        }
        return res;
    }
};

```

**_Efficient Solution:_**

```javascript
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        // Optimal Solution Time O(NlogN) & Auxiliary Space O(1)
        int len=intervals.size();
        if(len<=1)
            return intervals;
        sort(intervals.begin(),intervals.end());
        vector<vector<int>> res; // result vector
        // insert the first element into the result vector
        res.push_back(intervals[0]);
        for(int i=1;i<len;i++){
            if(res.back()[1]>=intervals[i][0])
                // back() points to the final element of the vector.
                // Update the endpoint of final element of result 
                // vector if there is an overlap with intervals[i]
                res.back()[1]=max(res.back()[1], intervals[i][1]);
            else
                // If no overlap, insert intervals[i]
                res.push_back(intervals[i]);
        }
        return res;
    }
};

```
**_All suggestions are welcome. Please upvote if you like it. Thank you._**
Happy Holidays! 🎁
Published: 2021-12-24 21:14:18

---
title: Happy Holidays! 🎁
published: true
date: 2021-12-24 00:15:00 UTC
tags: 
canonical_url: https://www.benjaminrancourt.ca/happy-holidays-2021
cover_image: https://www.benjaminrancourt.ca/images/pexels-brett-sayles-1708601.jpg
---
I don't know about you, but I've been busy at work this week with the [Apache Log4j vulnerability](https://cyber.gc.ca/en/alerts/active-exploitation-apache-log4j-vulnerability), trying to make sure our major system vendors had secured them properly! It tired me a little bit at the end of the day, so I didn't have the energy to write you a technical article this week! 😴

But wait, I still have two hours before 7.15 PM to write something pretty interesting... Well, I have no ideas so I'm going to take a break and show you what I bought for Christmas instead! 🎄

![](https://www.benjaminrancourt.ca/images/2021-12-23-personal-desktop.jpg)
_Merry Christmas to me! 🎅_

These are two [Samsung 27" monitors](https://www.samsung.com/us/computing/monitors/flat/27--led-monitor-with-borderless-design-lf27t350fhnxza/)! As I now work remotely full time, I needed to have monitors with better resolutions, because the code was really small in my "[New Latitude 7210 2-in-1 Business Detachable Laptop](https://www.dell.com/en-ca/work/shop/laptops-ultrabooks/new-latitude-7210-2-in-1-business-detachable-laptop/spd/latitude-12-7210-2-in-1-laptop)" or my previous monitors! 🔍

So far, these monitors seem ok. We'll see if it's still the case in the long term! 😬

And that ends this article! I hope to take advantage of the next few weeks to recharge my batteries! 🤖

Stay safe and happy holidays! 😷
How to handle schedule slippage on an agile project
Published: 2022-02-03 21:32:52

---
title: How to handle schedule slippage on an agile project
published: true
date: 2021-12-24 00:00:00 UTC
tags: 
canonical_url: https://jhall.io/archive/2021/12/24/how-to-handle-schedule-slippage-on-an-agile-project/
---

“I feel schedule slippage and Scrum work against each other,” someone recently said to me. When I asked for some more context, I was told “If you already have a feature set to be built, and a specific date by which it must be completed, I feel like Scrum isn’t going to work very well.”

Yeah, that’s true.

If you have a fixed scope and a fixed timeline, you simply don’t have an agile project. So trying to use Scrum, or any other agile approach, will only lead to frustration.

* * *
_If you enjoyed this message, [subscribe](https://jhall.io/daily) to <u>The Daily Commit</u> to get future messages to your inbox._
Excel - How to get a substring from a text
Tags: excel, substring
Published: 2021-12-23 23:10:15

---
title: Excel - How to get a substring from a text
published: true
description: Getting a substring from a cell in excel
tags: 'excel, substring'
cover_image: 'https://raw.githubusercontent.com/campelo/documentation/master/posts/excel/assets/cover.png'
canonical_url: null
id: 935082
date: '2021-12-23T23:10:15Z'
---

###### :postbox: Contact :brazil: :us: :fr:

[Twitter](https://twitter.com/campelo87)
[LinkedIn](https://www.linkedin.com/in/flavio-campelo/?locale=en_US)

---

If you're trying to get a part of a text from a excel's cell, this post can help you with a few samples.
For all examples we need a delimited char to split our text in two parts, nedeed and needless.

## Text on the left
For this example, the comma <strong>,</strong> is our delimited char. Now, you can eliminate all right side (needless text).

```
=FIND(G2,FIND(",",G2)-1)
```
![Image 1](https://raw.githubusercontent.com/campelo/documentation/master/posts/excel/how-to-get-a-substring-from-a-text/assets/img1.png)


## Text on the right

For this example, the comma <strong>,</strong> is our delimited char.

### First method

```
=TRIM(RIGHT(F2,LEN(F2)-LOOKUP("@",SUBSTITUE(F2,",","@",LEN(F2)-LEN(SUBSTITUE(F2,",",""))),1)))
```

### Second method

```
=FIND(",",F2)
```

Get all right text (needed text) from that char.
```
=RIGHT(F2,LEN(F2)-FIND(",",F2))
```

Use <strong>trim</strong> function to remove needless spaces
```
=TRIM(RIGHT(F2,LEN(F2)-FIND(",",F2)))
```

![Image 3](https://raw.githubusercontent.com/campelo/documentation/master/posts/excel/how-to-get-a-substring-from-a-text/assets/img3.png)


## Text on the middle
For that, you have to combine both of previous actions (left + right).
![Image 4](https://raw.githubusercontent.com/campelo/documentation/master/posts/excel/how-to-get-a-substring-from-a-text/assets/img4.png)

First of all you could eliminate the [right needless part](#text-on-the-left) of the text.

Then we will eliminate all left side from searched text. On our example, we will use slash char <strong>/</strong> to split our text.
```
=RIGHT(F2,LEN(F2)-FIND("@",SUBSTITUTE(F2,"/","@",LEN(F2)-LEN(SUBSTITUTE(F2,"/",""))),1))
```
![Image 2](https://raw.githubusercontent.com/campelo/documentation/master/posts/excel/how-to-get-a-substring-from-a-text/assets/img2.png)

## Sources:

[Substring](https://www.excel-easy.com/examples/substring.html#:~:text=To%20extract%20the%20leftmost%20characters,correct%20number%20of%20leftmost%20characters.)

[Getting the last position of a character using excel formula](https://trumpexcel.com/find-characters-last-position/)

## Typos or suggestions?

If you've found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. If you feel comfortable with github, instead of posting a comment, please go directly to https://github.com/campelo/documentation and open a new pull request with your changes.
Auto-redirect connection to port 4201 and fail
Tags: angular, browser, port, 4200
Published: 2021-12-23 22:58:42

I am creating a website with Angular on WSL2 Ubuntu running on a Windows 10 computer. When I launch the application with the _ng serve_ command, I get _** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **_ and _✔ Compiled successfully_. on the command line. 
But when I click on the link, the connection is redirected to http://localhost:4201/ and it fails. 
By changing 4201 to 4200 in the browser address field, the desired page is displayed. 
I did some research without finding any mention of an exactly similar problem, but I nevertheless saw that the easiest way to fix it was to run the _ng serve —port 4201_ command. It does work, but obviously not anymore when I replace 4201 with 4200 in the browser address field. 
Please, see also https://itectec.com/superuser/network-problems-using-wsl2/
Error encountered while setting up React Native using Apple's M1 chip system
Published: 2021-12-23 22:43:43

First of all, having read a lot of information online - i knew it was not gonna be an easy ride running my existing project on Apple's M1 chip

The first thing I did was to create an empty project after following the React Native environment setup documentation on https://reactnative.dev/docs/environment-setup

Thereafter, I ran 

npx react-native init AwesomeTSProject --template react-native-template-typescript

to create a broilerplate project. The initailization was successful until I ran npm run ios

1st Error: Failed to build iOS project. We ran "xcodebuild" command but it exited with error code 65.

Solved that by following this Stackoverflow post at https://stackoverflow.com/questions/65458086/failed-to-build-ios-project-we-ran-xcodebuild-command-but-it-exited-with-erro

At this stage, my project launched successfully.

Funny enough, I have answered a Stackoverflow question before relating to this at https://stackoverflow.com/questions/66062663/react-native-npx-react-native-run-ios-doesnt-work-after-initializing-the-proj/66143494#66143494 but yet my solution didnt come to me at the moment i had the issue.

After applying the fix, I was able to run my app successfully.

The second stage involves bringing in my existing codebas.