Skip to content

Commit d414458

Browse files
authored
Sri Hari: Batch-10/Added Articles (#4855)
* Added articles * Added articles * Added articles, resolve git-issues * Added articles * Added articles * Added articles * Added articles * Added articles * Added articles
1 parent fbbe45c commit d414458

33 files changed

+4969
-9
lines changed

articles/assign-cookies.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ class Solution {
111111
}
112112
```
113113

114+
```csharp
115+
public class Solution {
116+
public int FindContentChildren(int[] g, int[] s) {
117+
Array.Sort(s);
118+
int res = 0;
119+
120+
foreach (int i in g) {
121+
int minIdx = -1;
122+
for (int j = 0; j < s.Length; j++) {
123+
if (s[j] < i) continue;
124+
125+
if (minIdx == -1 || s[minIdx] > s[j]) {
126+
minIdx = j;
127+
}
128+
}
129+
130+
if (minIdx != -1) {
131+
s[minIdx] = -1;
132+
res++;
133+
}
134+
}
135+
136+
return res;
137+
}
138+
}
139+
```
140+
114141
::tabs-end
115142

116143
### Time & Space Complexity
@@ -210,6 +237,28 @@ class Solution {
210237
}
211238
```
212239

240+
```csharp
241+
public class Solution {
242+
public int FindContentChildren(int[] g, int[] s) {
243+
Array.Sort(g);
244+
Array.Sort(s);
245+
246+
int i = 0, j = 0;
247+
while (i < g.Length) {
248+
while (j < s.Length && g[i] > s[j]) {
249+
j++;
250+
}
251+
if (j == s.Length) {
252+
break;
253+
}
254+
i++;
255+
j++;
256+
}
257+
return i;
258+
}
259+
}
260+
```
261+
213262
::tabs-end
214263

215264
### Time & Space Complexity
@@ -291,6 +340,25 @@ class Solution {
291340
}
292341
```
293342

343+
```csharp
344+
public class Solution {
345+
public int FindContentChildren(int[] g, int[] s) {
346+
Array.Sort(g);
347+
Array.Sort(s);
348+
349+
int i = 0, j = 0;
350+
while (i < g.Length && j < s.Length) {
351+
if (g[i] <= s[j]) {
352+
i++;
353+
}
354+
j++;
355+
}
356+
357+
return i;
358+
}
359+
}
360+
```
361+
294362
::tabs-end
295363

296364
### Time & Space Complexity

articles/binary-search-tree-iterator.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,49 @@ class BSTIterator {
168168
}
169169
```
170170

171+
```csharp
172+
/**
173+
* Definition for a binary tree node.
174+
* public class TreeNode {
175+
* public int val;
176+
* public TreeNode left;
177+
* public TreeNode right;
178+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
179+
* this.val = val;
180+
* this.left = left;
181+
* this.right = right;
182+
* }
183+
* }
184+
*/
185+
public class BSTIterator {
186+
private List<int> arr;
187+
private int itr;
188+
189+
public BSTIterator(TreeNode root) {
190+
arr = new List<int>();
191+
itr = 0;
192+
Dfs(root);
193+
}
194+
195+
private void Dfs(TreeNode node) {
196+
if (node == null) return;
197+
Dfs(node.left);
198+
arr.Add(node.val);
199+
Dfs(node.right);
200+
}
201+
202+
public int Next() {
203+
int val = arr[itr];
204+
itr++;
205+
return val;
206+
}
207+
208+
public bool HasNext() {
209+
return itr < arr.Count;
210+
}
211+
}
212+
```
213+
171214
::tabs-end
172215

173216
### Time & Space Complexity
@@ -350,6 +393,52 @@ class BSTIterator {
350393
}
351394
```
352395

396+
```csharp
397+
/**
398+
* Definition for a binary tree node.
399+
* public class TreeNode {
400+
* public int val;
401+
* public TreeNode left;
402+
* public TreeNode right;
403+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
404+
* this.val = val;
405+
* this.left = left;
406+
* this.right = right;
407+
* }
408+
* }
409+
*/
410+
public class BSTIterator {
411+
private List<int> arr;
412+
private int itr;
413+
414+
public BSTIterator(TreeNode root) {
415+
arr = new List<int>();
416+
itr = 0;
417+
418+
Stack<TreeNode> stack = new Stack<TreeNode>();
419+
while (root != null || stack.Count > 0) {
420+
while (root != null) {
421+
stack.Push(root);
422+
root = root.left;
423+
}
424+
root = stack.Pop();
425+
arr.Add(root.val);
426+
root = root.right;
427+
}
428+
}
429+
430+
public int Next() {
431+
int val = arr[itr];
432+
itr++;
433+
return val;
434+
}
435+
436+
public bool HasNext() {
437+
return itr < arr.Count;
438+
}
439+
}
440+
```
441+
353442
::tabs-end
354443

355444
### Time & Space Complexity
@@ -522,6 +611,47 @@ class BSTIterator {
522611
}
523612
```
524613

614+
```csharp
615+
/**
616+
* Definition for a binary tree node.
617+
* public class TreeNode {
618+
* public int val;
619+
* public TreeNode left;
620+
* public TreeNode right;
621+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
622+
* this.val = val;
623+
* this.left = left;
624+
* this.right = right;
625+
* }
626+
* }
627+
*/
628+
public class BSTIterator {
629+
private Stack<TreeNode> stack;
630+
631+
public BSTIterator(TreeNode root) {
632+
stack = new Stack<TreeNode>();
633+
while (root != null) {
634+
stack.Push(root);
635+
root = root.left;
636+
}
637+
}
638+
639+
public int Next() {
640+
TreeNode node = stack.Pop();
641+
TreeNode cur = node.right;
642+
while (cur != null) {
643+
stack.Push(cur);
644+
cur = cur.left;
645+
}
646+
return node.val;
647+
}
648+
649+
public bool HasNext() {
650+
return stack.Count > 0;
651+
}
652+
}
653+
```
654+
525655
::tabs-end
526656

527657
### Time & Space Complexity
@@ -689,6 +819,45 @@ class BSTIterator {
689819
}
690820
```
691821

822+
```csharp
823+
/**
824+
* Definition for a binary tree node.
825+
* public class TreeNode {
826+
* public int val;
827+
* public TreeNode left;
828+
* public TreeNode right;
829+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
830+
* this.val = val;
831+
* this.left = left;
832+
* this.right = right;
833+
* }
834+
* }
835+
*/
836+
public class BSTIterator {
837+
private TreeNode cur;
838+
private Stack<TreeNode> stack;
839+
840+
public BSTIterator(TreeNode root) {
841+
cur = root;
842+
stack = new Stack<TreeNode>();
843+
}
844+
845+
public int Next() {
846+
while (cur != null) {
847+
stack.Push(cur);
848+
cur = cur.left;
849+
}
850+
TreeNode node = stack.Pop();
851+
cur = node.right;
852+
return node.val;
853+
}
854+
855+
public bool HasNext() {
856+
return cur != null || stack.Count > 0;
857+
}
858+
}
859+
```
860+
692861
::tabs-end
693862

694863
### Time & Space Complexity

articles/binary-subarrays-with-sum.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ class Solution {
8484
}
8585
```
8686

87+
```csharp
88+
public class Solution {
89+
public int NumSubarraysWithSum(int[] nums, int goal) {
90+
int n = nums.Length, res = 0;
91+
92+
for (int i = 0; i < n; i++) {
93+
int curSum = 0;
94+
for (int j = i; j < n; j++) {
95+
curSum += nums[j];
96+
if (curSum == goal) {
97+
res++;
98+
}
99+
}
100+
}
101+
102+
return res;
103+
}
104+
}
105+
```
106+
87107
::tabs-end
88108

89109
### Time & Space Complexity
@@ -173,6 +193,29 @@ class Solution {
173193
}
174194
```
175195

196+
```csharp
197+
public class Solution {
198+
public int NumSubarraysWithSum(int[] nums, int goal) {
199+
int prefixSum = 0, res = 0;
200+
Dictionary<int, int> count = new Dictionary<int, int>();
201+
count[0] = 1;
202+
203+
foreach (int num in nums) {
204+
prefixSum += num;
205+
if (count.ContainsKey(prefixSum - goal)) {
206+
res += count[prefixSum - goal];
207+
}
208+
if (!count.ContainsKey(prefixSum)) {
209+
count[prefixSum] = 0;
210+
}
211+
count[prefixSum]++;
212+
}
213+
214+
return res;
215+
}
216+
}
217+
```
218+
176219
::tabs-end
177220

178221
### Time & Space Complexity
@@ -271,6 +314,27 @@ class Solution {
271314
}
272315
```
273316

317+
```csharp
318+
public class Solution {
319+
public int NumSubarraysWithSum(int[] nums, int goal) {
320+
int n = nums.Length;
321+
int[] count = new int[n + 1];
322+
count[0] = 1;
323+
int prefixSum = 0, res = 0;
324+
325+
foreach (int num in nums) {
326+
prefixSum += num;
327+
if (prefixSum >= goal) {
328+
res += count[prefixSum - goal];
329+
}
330+
count[prefixSum]++;
331+
}
332+
333+
return res;
334+
}
335+
}
336+
```
337+
274338
::tabs-end
275339

276340
### Time & Space Complexity
@@ -377,6 +441,28 @@ class Solution {
377441
}
378442
```
379443

444+
```csharp
445+
public class Solution {
446+
public int NumSubarraysWithSum(int[] nums, int goal) {
447+
int Helper(int x) {
448+
if (x < 0) return 0;
449+
int res = 0, l = 0, cur = 0;
450+
for (int r = 0; r < nums.Length; r++) {
451+
cur += nums[r];
452+
while (cur > x) {
453+
cur -= nums[l];
454+
l++;
455+
}
456+
res += (r - l + 1);
457+
}
458+
return res;
459+
}
460+
461+
return Helper(goal) - Helper(goal - 1);
462+
}
463+
}
464+
```
465+
380466
::tabs-end
381467

382468
### Time & Space Complexity

0 commit comments

Comments
 (0)