Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit214cbc5

Browse files
authored
Sri Hari: Batch-6/Neetcode-All/Added-articles (neetcode-gh#4042)
* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles* Batch-6/Neetcode-ALL/Added-articles
1 parent0a2d881 commit214cbc5

File tree

38 files changed

+11446
-1
lines changed

38 files changed

+11446
-1
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
##1. Two Pointers
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defappendCharacters(self,s:str,t:str) ->int:
8+
i, j=0,0
9+
10+
while i<len(s)and j<len(t):
11+
if s[i]== t[j]:
12+
i+=1
13+
j+=1
14+
else:
15+
i+=1
16+
returnlen(t)- j
17+
```
18+
19+
```java
20+
publicclassSolution {
21+
publicintappendCharacters(Strings,Stringt) {
22+
int i=0, j=0;
23+
24+
while (i< s.length()&& j< t.length()) {
25+
if (s.charAt(i)== t.charAt(j)) {
26+
i++;
27+
j++;
28+
}else {
29+
i++;
30+
}
31+
}
32+
return t.length()- j;
33+
}
34+
}
35+
```
36+
37+
```cpp
38+
classSolution {
39+
public:
40+
int appendCharacters(string s, string t) {
41+
int i = 0, j = 0;
42+
43+
while (i < s.length() && j < t.length()) {
44+
if (s[i] == t[j]) {
45+
i++;
46+
j++;
47+
} else {
48+
i++;
49+
}
50+
}
51+
return t.length() - j;
52+
}
53+
};
54+
```
55+
56+
```javascript
57+
classSolution {
58+
/**
59+
*@param{string}s
60+
*@param{string}t
61+
*@return{number}
62+
*/
63+
appendCharacters(s,t) {
64+
let i=0, j=0;
65+
66+
while (i<s.length&& j<t.length) {
67+
if (s[i]=== t[j]) {
68+
i++;
69+
j++;
70+
}else {
71+
i++;
72+
}
73+
}
74+
returnt.length- j;
75+
}
76+
}
77+
```
78+
79+
::tabs-end
80+
81+
###Time & Space Complexity
82+
83+
* Time complexity: $O(n + m)$
84+
* Space complexity: $O(1)$
85+
86+
>Where $n$ and $m$ are the lengths of the strings $s$ and $t$, respectively.
87+
88+
---
89+
90+
##2. Index Jumping
91+
92+
::tabs-start
93+
94+
```python
95+
classSolution:
96+
defappendCharacters(self,s:str,t:str) ->int:
97+
n, m=len(s),len(t)
98+
store= [[n+1]*26for _inrange(n)]
99+
store[n-1][ord(s[n-1])-ord('a')]= n-1
100+
101+
for iinrange(n-2,-1,-1):
102+
store[i]= store[i+1][:]
103+
store[i][ord(s[i])-ord('a')]= i
104+
105+
i, j=0,0
106+
while i< nand j< m:
107+
if store[i][ord(t[j])-ord('a')]== n+1:
108+
break
109+
110+
i= store[i][ord(t[j])-ord('a')]+1
111+
j+=1
112+
113+
return m- j
114+
```
115+
116+
```java
117+
publicclassSolution {
118+
publicintappendCharacters(Strings,Stringt) {
119+
int n= s.length(), m= t.length();
120+
int[][] store=newint[n][26];
121+
for (int[] row: store) {
122+
Arrays.fill(row, n+1);
123+
}
124+
store[n-1][s.charAt(n-1)-'a']= n-1;
125+
126+
for (int i= n-2; i>=0; i--) {
127+
store[i]= store[i+1].clone();
128+
store[i][s.charAt(i)-'a']= i;
129+
}
130+
131+
int i=0, j=0;
132+
while (i< n&& j< m) {
133+
if (store[i][t.charAt(j)-'a']== n+1) {
134+
break;
135+
}
136+
i= store[i][t.charAt(j)-'a']+1;
137+
j++;
138+
}
139+
140+
return m- j;
141+
}
142+
}
143+
```
144+
145+
```cpp
146+
classSolution {
147+
public:
148+
int appendCharacters(string s, string t) {
149+
int n = s.length(), m = t.length();
150+
vector<vector<int>> store(n, vector<int>(26, n + 1));
151+
store[n - 1][s[n - 1] - 'a'] = n - 1;
152+
153+
for (int i = n - 2; i >= 0; i--) {
154+
store[i] = store[i + 1];
155+
store[i][s[i] - 'a'] = i;
156+
}
157+
158+
int i =0, j =0;
159+
while (i < n && j < m) {
160+
if (store[i][t[j] - 'a'] == n + 1) {
161+
break;
162+
}
163+
i = store[i][t[j] - 'a'] + 1;
164+
j++;
165+
}
166+
167+
return m - j;
168+
}
169+
};
170+
```
171+
172+
```javascript
173+
classSolution {
174+
/**
175+
*@param{string}s
176+
*@param{string}t
177+
*@return{number}
178+
*/
179+
appendCharacters(s,t) {
180+
constn=s.length, m=t.length;
181+
conststore=Array.from({ length: n }, ()=>Array(26).fill(n+1));
182+
store[n-1][s.charCodeAt(n-1)-97]= n-1;
183+
184+
for (let i= n-2; i>=0; i--) {
185+
store[i]= store[i+1].slice();
186+
store[i][s.charCodeAt(i)-97]= i;
187+
}
188+
189+
let i=0, j=0;
190+
while (i< n&& j< m) {
191+
if (store[i][t.charCodeAt(j)-97]=== n+1) {
192+
break;
193+
}
194+
i= store[i][t.charCodeAt(j)-97]+1;
195+
j++;
196+
}
197+
198+
return m- j;
199+
}
200+
}
201+
```
202+
203+
::tabs-end
204+
205+
###Time & Space Complexity
206+
207+
* Time complexity: $O(n + m)$
208+
* Space complexity: $O(n)$
209+
210+
>Where $n$ and $m$ are the lengths of the strings $s$ and $t$, respectively.

‎articles/average-waiting-time.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
##1. Simulation - I
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defaverageWaitingTime(self,customers: List[List[int]]) ->float:
8+
t=0
9+
total=0
10+
11+
for arrival, orderin customers:
12+
if t> arrival:
13+
total+= t- arrival
14+
else:
15+
t= arrival
16+
total+= order
17+
t+= order
18+
19+
return total/len(customers)
20+
```
21+
22+
```java
23+
publicclassSolution {
24+
publicdoubleaverageWaitingTime(int[][]customers) {
25+
long t=0, total=0;
26+
27+
for (int[] c: customers) {
28+
int arrival= c[0], order= c[1];
29+
if (t> arrival) {
30+
total+= t- arrival;
31+
}else {
32+
t= arrival;
33+
}
34+
total+= order;
35+
t+= order;
36+
}
37+
38+
return (double) total/ customers.length;
39+
}
40+
}
41+
```
42+
43+
```cpp
44+
classSolution {
45+
public:
46+
double averageWaitingTime(vector<vector<int>>& customers) {
47+
long long t = 0, total = 0;
48+
49+
for (auto& c : customers) {
50+
int arrival = c[0], order = c[1];
51+
if (t > arrival) {
52+
total += t - arrival;
53+
} else {
54+
t = arrival;
55+
}
56+
total += order;
57+
t += order;
58+
}
59+
60+
return (double) total / customers.size();
61+
}
62+
};
63+
```
64+
65+
```javascript
66+
classSolution {
67+
/**
68+
*@param{number[][]}customers
69+
*@return{number}
70+
*/
71+
averageWaitingTime(customers) {
72+
let t=0, total=0;
73+
74+
for (let [arrival, order]of customers) {
75+
if (t> arrival) {
76+
total+= t- arrival;
77+
}else {
78+
t= arrival;
79+
}
80+
total+= order;
81+
t+= order;
82+
}
83+
84+
return total/customers.length;
85+
}
86+
}
87+
```
88+
89+
::tabs-end
90+
91+
###Time & Space Complexity
92+
93+
* Time complexity: $O(n)$
94+
* Space complexity: $O(1)$
95+
96+
---
97+
98+
##2. Simulation - II
99+
100+
::tabs-start
101+
102+
```python
103+
classSolution:
104+
defaverageWaitingTime(self,customers: List[List[int]]) ->float:
105+
t= total=0
106+
for arrival, orderin customers:
107+
t=max(t, arrival)+ order
108+
total+= t- arrival
109+
return total/len(customers)
110+
```
111+
112+
```java
113+
publicclassSolution {
114+
publicdoubleaverageWaitingTime(int[][]customers) {
115+
long t=0, total=0;
116+
117+
for (int[] c: customers) {
118+
int arrival= c[0], order= c[1];
119+
t=Math.max(t, arrival)+ order;
120+
total+= t- arrival;
121+
}
122+
123+
return (double) total/ customers.length;
124+
}
125+
}
126+
```
127+
128+
```cpp
129+
classSolution {
130+
public:
131+
double averageWaitingTime(vector<vector<int>>& customers) {
132+
long long t = 0, total = 0;
133+
134+
for (auto& c : customers) {
135+
int arrival = c[0], order = c[1];
136+
t = max(t, (long long)arrival) + order;
137+
total += t - arrival;
138+
}
139+
140+
return (double) total / customers.size();
141+
}
142+
};
143+
```
144+
145+
```javascript
146+
classSolution {
147+
/**
148+
*@param{number[][]}customers
149+
*@return{number}
150+
*/
151+
averageWaitingTime(customers) {
152+
let t=0, total=0;
153+
154+
for (let [arrival, order]of customers) {
155+
t=Math.max(t, arrival)+ order;
156+
total+= t- arrival;
157+
}
158+
159+
return total/customers.length;
160+
}
161+
}
162+
```
163+
164+
::tabs-end
165+
166+
###Time & Space Complexity
167+
168+
* Time complexity: $O(n)$
169+
* Space complexity: $O(1)$

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp