Skip to content
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
68 changes: 68 additions & 0 deletions articles/assign-cookies.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ class Solution {
}
```

```csharp
public class Solution {
public int FindContentChildren(int[] g, int[] s) {
Array.Sort(s);
int res = 0;

foreach (int i in g) {
int minIdx = -1;
for (int j = 0; j < s.Length; j++) {
if (s[j] < i) continue;

if (minIdx == -1 || s[minIdx] > s[j]) {
minIdx = j;
}
}

if (minIdx != -1) {
s[minIdx] = -1;
res++;
}
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -210,6 +237,28 @@ class Solution {
}
```

```csharp
public class Solution {
public int FindContentChildren(int[] g, int[] s) {
Array.Sort(g);
Array.Sort(s);

int i = 0, j = 0;
while (i < g.Length) {
while (j < s.Length && g[i] > s[j]) {
j++;
}
if (j == s.Length) {
break;
}
i++;
j++;
}
return i;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -291,6 +340,25 @@ class Solution {
}
```

```csharp
public class Solution {
public int FindContentChildren(int[] g, int[] s) {
Array.Sort(g);
Array.Sort(s);

int i = 0, j = 0;
while (i < g.Length && j < s.Length) {
if (g[i] <= s[j]) {
i++;
}
j++;
}

return i;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
169 changes: 169 additions & 0 deletions articles/binary-search-tree-iterator.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,49 @@ class BSTIterator {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class BSTIterator {
private List<int> arr;
private int itr;

public BSTIterator(TreeNode root) {
arr = new List<int>();
itr = 0;
Dfs(root);
}

private void Dfs(TreeNode node) {
if (node == null) return;
Dfs(node.left);
arr.Add(node.val);
Dfs(node.right);
}

public int Next() {
int val = arr[itr];
itr++;
return val;
}

public bool HasNext() {
return itr < arr.Count;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -350,6 +393,52 @@ class BSTIterator {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class BSTIterator {
private List<int> arr;
private int itr;

public BSTIterator(TreeNode root) {
arr = new List<int>();
itr = 0;

Stack<TreeNode> stack = new Stack<TreeNode>();
while (root != null || stack.Count > 0) {
while (root != null) {
stack.Push(root);
root = root.left;
}
root = stack.Pop();
arr.Add(root.val);
root = root.right;
}
}

public int Next() {
int val = arr[itr];
itr++;
return val;
}

public bool HasNext() {
return itr < arr.Count;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -522,6 +611,47 @@ class BSTIterator {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class BSTIterator {
private Stack<TreeNode> stack;

public BSTIterator(TreeNode root) {
stack = new Stack<TreeNode>();
while (root != null) {
stack.Push(root);
root = root.left;
}
}

public int Next() {
TreeNode node = stack.Pop();
TreeNode cur = node.right;
while (cur != null) {
stack.Push(cur);
cur = cur.left;
}
return node.val;
}

public bool HasNext() {
return stack.Count > 0;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -689,6 +819,45 @@ class BSTIterator {
}
```

```csharp
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class BSTIterator {
private TreeNode cur;
private Stack<TreeNode> stack;

public BSTIterator(TreeNode root) {
cur = root;
stack = new Stack<TreeNode>();
}

public int Next() {
while (cur != null) {
stack.Push(cur);
cur = cur.left;
}
TreeNode node = stack.Pop();
cur = node.right;
return node.val;
}

public bool HasNext() {
return cur != null || stack.Count > 0;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
86 changes: 86 additions & 0 deletions articles/binary-subarrays-with-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumSubarraysWithSum(int[] nums, int goal) {
int n = nums.Length, res = 0;

for (int i = 0; i < n; i++) {
int curSum = 0;
for (int j = i; j < n; j++) {
curSum += nums[j];
if (curSum == goal) {
res++;
}
}
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -173,6 +193,29 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumSubarraysWithSum(int[] nums, int goal) {
int prefixSum = 0, res = 0;
Dictionary<int, int> count = new Dictionary<int, int>();
count[0] = 1;

foreach (int num in nums) {
prefixSum += num;
if (count.ContainsKey(prefixSum - goal)) {
res += count[prefixSum - goal];
}
if (!count.ContainsKey(prefixSum)) {
count[prefixSum] = 0;
}
count[prefixSum]++;
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -271,6 +314,27 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumSubarraysWithSum(int[] nums, int goal) {
int n = nums.Length;
int[] count = new int[n + 1];
count[0] = 1;
int prefixSum = 0, res = 0;

foreach (int num in nums) {
prefixSum += num;
if (prefixSum >= goal) {
res += count[prefixSum - goal];
}
count[prefixSum]++;
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -377,6 +441,28 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumSubarraysWithSum(int[] nums, int goal) {
int Helper(int x) {
if (x < 0) return 0;
int res = 0, l = 0, cur = 0;
for (int r = 0; r < nums.Length; r++) {
cur += nums[r];
while (cur > x) {
cur -= nums[l];
l++;
}
res += (r - l + 1);
}
return res;
}

return Helper(goal) - Helper(goal - 1);
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading