Skip to content

Sri Hari: Batch-6/Neetcode-All/Added-articles #4042

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

Merged
merged 9 commits into from
Apr 15, 2025
Merged
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
210 changes: 210 additions & 0 deletions articles/append-characters-to-string-to-make-subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
## 1. Two Pointers

::tabs-start

```python
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
i, j = 0, 0

while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1
j += 1
else:
i += 1
return len(t) - j
```

```java
public class Solution {
public int appendCharacters(String s, String t) {
int i = 0, j = 0;

while (i < s.length() && j < t.length()) {
if (s.charAt(i) == t.charAt(j)) {
i++;
j++;
} else {
i++;
}
}
return t.length() - j;
}
}
```

```cpp
class Solution {
public:
int appendCharacters(string s, string t) {
int i = 0, j = 0;

while (i < s.length() && j < t.length()) {
if (s[i] == t[j]) {
i++;
j++;
} else {
i++;
}
}
return t.length() - j;
}
};
```

```javascript
class Solution {
/**
* @param {string} s
* @param {string} t
* @return {number}
*/
appendCharacters(s, t) {
let i = 0, j = 0;

while (i < s.length && j < t.length) {
if (s[i] === t[j]) {
i++;
j++;
} else {
i++;
}
}
return t.length - j;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n + m)$
* Space complexity: $O(1)$

> Where $n$ and $m$ are the lengths of the strings $s$ and $t$, respectively.

---

## 2. Index Jumping

::tabs-start

```python
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
n, m = len(s), len(t)
store = [[n + 1] * 26 for _ in range(n)]
store[n - 1][ord(s[n - 1]) - ord('a')] = n - 1

for i in range(n - 2, -1, -1):
store[i] = store[i + 1][:]
store[i][ord(s[i]) - ord('a')] = i

i, j = 0, 0
while i < n and j < m:
if store[i][ord(t[j]) - ord('a')] == n + 1:
break

i = store[i][ord(t[j]) - ord('a')] + 1
j += 1

return m - j
```

```java
public class Solution {
public int appendCharacters(String s, String t) {
int n = s.length(), m = t.length();
int[][] store = new int[n][26];
for (int[] row : store) {
Arrays.fill(row, n + 1);
}
store[n - 1][s.charAt(n - 1) - 'a'] = n - 1;

for (int i = n - 2; i >= 0; i--) {
store[i] = store[i + 1].clone();
store[i][s.charAt(i) - 'a'] = i;
}

int i = 0, j = 0;
while (i < n && j < m) {
if (store[i][t.charAt(j) - 'a'] == n + 1) {
break;
}
i = store[i][t.charAt(j) - 'a'] + 1;
j++;
}

return m - j;
}
}
```

```cpp
class Solution {
public:
int appendCharacters(string s, string t) {
int n = s.length(), m = t.length();
vector<vector<int>> store(n, vector<int>(26, n + 1));
store[n - 1][s[n - 1] - 'a'] = n - 1;

for (int i = n - 2; i >= 0; i--) {
store[i] = store[i + 1];
store[i][s[i] - 'a'] = i;
}

int i = 0, j = 0;
while (i < n && j < m) {
if (store[i][t[j] - 'a'] == n + 1) {
break;
}
i = store[i][t[j] - 'a'] + 1;
j++;
}

return m - j;
}
};
```

```javascript
class Solution {
/**
* @param {string} s
* @param {string} t
* @return {number}
*/
appendCharacters(s, t) {
const n = s.length, m = t.length;
const store = Array.from({ length: n }, () => Array(26).fill(n + 1));
store[n - 1][s.charCodeAt(n - 1) - 97] = n - 1;

for (let i = n - 2; i >= 0; i--) {
store[i] = store[i + 1].slice();
store[i][s.charCodeAt(i) - 97] = i;
}

let i = 0, j = 0;
while (i < n && j < m) {
if (store[i][t.charCodeAt(j) - 97] === n + 1) {
break;
}
i = store[i][t.charCodeAt(j) - 97] + 1;
j++;
}

return m - j;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n + m)$
* Space complexity: $O(n)$

> Where $n$ and $m$ are the lengths of the strings $s$ and $t$, respectively.
169 changes: 169 additions & 0 deletions articles/average-waiting-time.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
## 1. Simulation - I

::tabs-start

```python
class Solution:
def averageWaitingTime(self, customers: List[List[int]]) -> float:
t = 0
total = 0

for arrival, order in customers:
if t > arrival:
total += t - arrival
else:
t = arrival
total += order
t += order

return total / len(customers)
```

```java
public class Solution {
public double averageWaitingTime(int[][] customers) {
long t = 0, total = 0;

for (int[] c : customers) {
int arrival = c[0], order = c[1];
if (t > arrival) {
total += t - arrival;
} else {
t = arrival;
}
total += order;
t += order;
}

return (double) total / customers.length;
}
}
```

```cpp
class Solution {
public:
double averageWaitingTime(vector<vector<int>>& customers) {
long long t = 0, total = 0;

for (auto& c : customers) {
int arrival = c[0], order = c[1];
if (t > arrival) {
total += t - arrival;
} else {
t = arrival;
}
total += order;
t += order;
}

return (double) total / customers.size();
}
};
```

```javascript
class Solution {
/**
* @param {number[][]} customers
* @return {number}
*/
averageWaitingTime(customers) {
let t = 0, total = 0;

for (let [arrival, order] of customers) {
if (t > arrival) {
total += t - arrival;
} else {
t = arrival;
}
total += order;
t += order;
}

return total / customers.length;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(1)$

---

## 2. Simulation - II

::tabs-start

```python
class Solution:
def averageWaitingTime(self, customers: List[List[int]]) -> float:
t = total = 0
for arrival, order in customers:
t = max(t, arrival) + order
total += t - arrival
return total / len(customers)
```

```java
public class Solution {
public double averageWaitingTime(int[][] customers) {
long t = 0, total = 0;

for (int[] c : customers) {
int arrival = c[0], order = c[1];
t = Math.max(t, arrival) + order;
total += t - arrival;
}

return (double) total / customers.length;
}
}
```

```cpp
class Solution {
public:
double averageWaitingTime(vector<vector<int>>& customers) {
long long t = 0, total = 0;

for (auto& c : customers) {
int arrival = c[0], order = c[1];
t = max(t, (long long)arrival) + order;
total += t - arrival;
}

return (double) total / customers.size();
}
};
```

```javascript
class Solution {
/**
* @param {number[][]} customers
* @return {number}
*/
averageWaitingTime(customers) {
let t = 0, total = 0;

for (let [arrival, order] of customers) {
t = Math.max(t, arrival) + order;
total += t - arrival;
}

return total / customers.length;
}
}
```

::tabs-end

### Time & Space Complexity

* Time complexity: $O(n)$
* Space complexity: $O(1)$
Loading