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

Commitd414458

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 parentfbbe45c commitd414458

File tree

33 files changed

+4969
-9
lines changed

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+
publicclassSolution {
116+
publicintFindContentChildren(int[]g,int[]s) {
117+
Array.Sort(s);
118+
intres=0;
119+
120+
foreach (intiing) {
121+
intminIdx=-1;
122+
for (intj=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+
returnres;
137+
}
138+
}
139+
```
140+
114141
::tabs-end
115142

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

240+
```csharp
241+
publicclassSolution {
242+
publicintFindContentChildren(int[]g,int[]s) {
243+
Array.Sort(g);
244+
Array.Sort(s);
245+
246+
inti=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+
returni;
258+
}
259+
}
260+
```
261+
213262
::tabs-end
214263

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

343+
```csharp
344+
publicclassSolution {
345+
publicintFindContentChildren(int[]g,int[]s) {
346+
Array.Sort(g);
347+
Array.Sort(s);
348+
349+
inti=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+
returni;
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+
publicclassBSTIterator {
186+
privateList<int>arr;
187+
privateintitr;
188+
189+
publicBSTIterator(TreeNoderoot) {
190+
arr=newList<int>();
191+
itr=0;
192+
Dfs(root);
193+
}
194+
195+
privatevoidDfs(TreeNodenode) {
196+
if (node==null)return;
197+
Dfs(node.left);
198+
arr.Add(node.val);
199+
Dfs(node.right);
200+
}
201+
202+
publicintNext() {
203+
intval=arr[itr];
204+
itr++;
205+
returnval;
206+
}
207+
208+
publicboolHasNext() {
209+
returnitr<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+
publicclassBSTIterator {
411+
privateList<int>arr;
412+
privateintitr;
413+
414+
publicBSTIterator(TreeNoderoot) {
415+
arr=newList<int>();
416+
itr=0;
417+
418+
Stack<TreeNode>stack=newStack<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+
publicintNext() {
431+
intval=arr[itr];
432+
itr++;
433+
returnval;
434+
}
435+
436+
publicboolHasNext() {
437+
returnitr<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+
publicclassBSTIterator {
629+
privateStack<TreeNode>stack;
630+
631+
publicBSTIterator(TreeNoderoot) {
632+
stack=newStack<TreeNode>();
633+
while (root!=null) {
634+
stack.Push(root);
635+
root=root.left;
636+
}
637+
}
638+
639+
publicintNext() {
640+
TreeNodenode=stack.Pop();
641+
TreeNodecur=node.right;
642+
while (cur!=null) {
643+
stack.Push(cur);
644+
cur=cur.left;
645+
}
646+
returnnode.val;
647+
}
648+
649+
publicboolHasNext() {
650+
returnstack.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+
publicclassBSTIterator {
837+
privateTreeNodecur;
838+
privateStack<TreeNode>stack;
839+
840+
publicBSTIterator(TreeNoderoot) {
841+
cur=root;
842+
stack=newStack<TreeNode>();
843+
}
844+
845+
publicintNext() {
846+
while (cur!=null) {
847+
stack.Push(cur);
848+
cur=cur.left;
849+
}
850+
TreeNodenode=stack.Pop();
851+
cur=node.right;
852+
returnnode.val;
853+
}
854+
855+
publicboolHasNext() {
856+
returncur!=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+
publicclassSolution {
89+
publicintNumSubarraysWithSum(int[]nums,intgoal) {
90+
intn=nums.Length,res=0;
91+
92+
for (inti=0;i<n;i++) {
93+
intcurSum=0;
94+
for (intj=i;j<n;j++) {
95+
curSum+=nums[j];
96+
if (curSum==goal) {
97+
res++;
98+
}
99+
}
100+
}
101+
102+
returnres;
103+
}
104+
}
105+
```
106+
87107
::tabs-end
88108

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

196+
```csharp
197+
publicclassSolution {
198+
publicintNumSubarraysWithSum(int[]nums,intgoal) {
199+
intprefixSum=0,res=0;
200+
Dictionary<int,int>count=newDictionary<int,int>();
201+
count[0]=1;
202+
203+
foreach (intnuminnums) {
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+
returnres;
215+
}
216+
}
217+
```
218+
176219
::tabs-end
177220

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

317+
```csharp
318+
publicclassSolution {
319+
publicintNumSubarraysWithSum(int[]nums,intgoal) {
320+
intn=nums.Length;
321+
int[]count=newint[n+1];
322+
count[0]=1;
323+
intprefixSum=0,res=0;
324+
325+
foreach (intnuminnums) {
326+
prefixSum+=num;
327+
if (prefixSum>=goal) {
328+
res+=count[prefixSum-goal];
329+
}
330+
count[prefixSum]++;
331+
}
332+
333+
returnres;
334+
}
335+
}
336+
```
337+
274338
::tabs-end
275339

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

444+
```csharp
445+
publicclassSolution {
446+
publicintNumSubarraysWithSum(int[]nums,intgoal) {
447+
intHelper(intx) {
448+
if (x<0)return0;
449+
intres=0,l=0,cur=0;
450+
for (intr=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+
returnres;
459+
}
460+
461+
returnHelper(goal)-Helper(goal-1);
462+
}
463+
}
464+
```
465+
380466
::tabs-end
381467

382468
###Time & Space Complexity

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp