Skip to content

Commit 1243c6f

Browse files
committed
Add C# solutions
1 parent 7e24e8b commit 1243c6f

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class RecentCounter {
2+
3+
private Queue<int> queue;
4+
private int count;
5+
public RecentCounter() {
6+
count = 0;
7+
queue = new Queue<int>();
8+
}
9+
10+
public int Ping(int t) {
11+
queue.Enqueue(t);
12+
while(t - queue.FirstOrDefault() > 3000)
13+
{
14+
queue.Dequeue();
15+
}
16+
return queue.Count();
17+
}
18+
}
19+
20+
/**
21+
* Your RecentCounter object will be instantiated and called as such:
22+
* RecentCounter obj = new RecentCounter();
23+
* int param_1 = obj.Ping(t);
24+
*/

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
https://leetcode.com
44

5+
6+
## October LeetCoding Challenge
7+
Click [here](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/) for problem descriptions.
8+
9+
Solutions in various programming languages are provided. Enjoy it.
10+
11+
1. [Number of Recent Calls](https://github.com/AlgoStudyGroup/Leetcode/tree/master/October-LeetCoding-Challenge/01-Number-of-Recent-Calls) : Queue
12+
13+
514
## September LeetCoding Challenge
615
Click [here](https://leetcode.com/explore/challenge/card/september-leetcoding-challenge) for problem descriptions.
716

@@ -28,6 +37,7 @@ Solutions in various programming languages are provided. Enjoy it.
2837
19. [Sequential Digits](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/19-Sequential-Digits)
2938
23. [Gas Station](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/23-Gas-Station)
3039
28. [Subarray Product Less Than K](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/28-Subarray-Product-Less-Than-K)
40+
30. [First Missing Positive](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/30-First-Missing-Positive)
3141

3242
## August LeetCoding Challenge
3343
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Solution {
2+
public int FirstMissingPositive(int[] nums) {
3+
int n = nums.Length;
4+
5+
// 1. mark numbers (num < 0) and (num > n) with a special marker number (n+1)
6+
// (we can ignore those because if all number are > n then we'll simply return 1)
7+
for (int i = 0; i < n; i++)
8+
{
9+
if (nums[i] <= 0 || nums[i] > n)
10+
{
11+
nums[i] = n + 1;
12+
}
13+
}
14+
// note: all number in the array are now positive, and on the range 1..n+1
15+
16+
// 2. mark each cell appearing in the array, by converting the index for that number to negative
17+
for (int i = 0; i < n; i++)
18+
{
19+
int num = Math.Abs(nums[i]);
20+
if (num > n)
21+
{
22+
continue;
23+
}
24+
num--; // -1 for zero index based array (so the number 1 will be at pos 0)
25+
if (nums[num] > 0)
26+
{ // prevents double negative operations
27+
nums[num] = -1 * nums[num];
28+
}
29+
}
30+
31+
// 3. find the first cell which isn't negative (doesn't appear in the array)
32+
for (int i = 0; i < n; i++)
33+
{
34+
if (nums[i] >= 0)
35+
{
36+
return i + 1;
37+
}
38+
}
39+
40+
// 4. no positive numbers were found, which means the array contains all numbers 1..n
41+
return n + 1;
42+
}
43+
}

0 commit comments

Comments
 (0)