File tree 3 files changed +77
-0
lines changed
October-LeetCoding-Challenge/01-Number-of-Recent-Calls
September-LeetCoding-Challenge/30-First-Missing-Positive
3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
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
+ */
Original file line number Diff line number Diff line change 2
2
3
3
https://leetcode.com
4
4
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
+
5
14
## September LeetCoding Challenge
6
15
Click [ here] ( https://leetcode.com/explore/challenge/card/september-leetcoding-challenge ) for problem descriptions.
7
16
@@ -28,6 +37,7 @@ Solutions in various programming languages are provided. Enjoy it.
28
37
19 . [ Sequential Digits] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/19-Sequential-Digits )
29
38
23 . [ Gas Station] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/23-Gas-Station )
30
39
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 )
31
41
32
42
## August LeetCoding Challenge
33
43
Click [ here] ( https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/ ) for problem descriptions.
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments