File tree Expand file tree Collapse file tree 3 files changed +168
-25
lines changed Expand file tree Collapse file tree 3 files changed +168
-25
lines changed Original file line number Diff line number Diff line change
1
+
2
+ // we know the answer lies between 1 and n+1 inclusive,
3
+ // complexity O(n) and O(1)
4
+
5
+ /* so we simply swap the numbers with their correct correct position numbers, such that i+1=nums[i] for each i
6
+ in case nums[i]<0 || nums[i]>n we simply ignore these values
7
+
8
+ the smallest i where i+1 != nums[i] is our answer
9
+ */
10
+
1
11
class Solution {
2
12
public:
3
13
int firstMissingPositive (vector<int >& nums) {
4
14
5
- int i;
6
- int n=nums.size ();
15
+ int n=nums.size ();
7
16
if (n==0 )
8
17
return 1 ;
9
-
10
-
11
- bool containsOne=false ;
12
- for (i=0 ;i<n;i++)
18
+ for (int i=0 ;i<n;i++)
13
19
{
14
- if (nums[i]==1 )
15
- containsOne=true ;
16
- else if (nums[i]>n||nums[i]<=0 )
17
- nums[i]=1 ;
18
- }
19
-
20
-
21
- if (containsOne==false )
22
- return 1 ;
23
-
24
- for (i=0 ;i<n;i++)
25
- {
26
- int index=abs (nums[i])-1 ;
20
+ if (nums[i]<0 || nums[i]>n)
21
+ continue ;
22
+
23
+ int pos=nums[i]-1 ;
27
24
28
- if (nums[index]>0 )
29
- nums[index]=-nums[index];
25
+ while (nums[i]>=1 && nums[i]<=n && nums[i]!=nums[pos])
26
+ {
27
+ swap (nums[i], nums[pos]);
28
+ pos=nums[i]-1 ;
29
+ }
30
30
}
31
31
32
- for (i=0 ;i<n;i++)
32
+ for (int i=0 ;i<n;i++)
33
33
{
34
- if (nums[i]>=0 )
34
+ if (nums[i]!=i+1 )
35
+ {
35
36
return i+1 ;
37
+ }
38
+
36
39
}
37
40
38
41
return n+1 ;
39
42
40
-
41
43
}
42
- };
44
+ };
Original file line number Diff line number Diff line change
1
+ // dp solution, where we build a dp table and keep track whether the current index is a good pos or not;
2
+
3
+ // time: O(n^2) space O(n)
4
+ class Solution {
5
+ public:
6
+ bool canJump (vector<int >& nums) {
7
+
8
+ int n=nums.size ();
9
+ int dp[n];
10
+
11
+ memset (dp, 0 , sizeof (dp));
12
+
13
+
14
+ int i;
15
+ dp[n-1 ]=1 ;
16
+
17
+ int good_pos=n-1 ;
18
+
19
+ for (i=n-2 ;i>=0 ;i--)
20
+ {
21
+ int maxJump=i+nums[i];
22
+
23
+ for (int j=i+1 ;j<=maxJump; j++)
24
+ {
25
+ if (dp[j]==1 )
26
+ {
27
+ dp[i]=1 ;
28
+ break ;
29
+ }
30
+ }
31
+ }
32
+
33
+
34
+ return dp[0 ]==1 ;
35
+ }
36
+ };
37
+
38
+
39
+
40
+ // greedy solution where we keep track of the closest good pos in the left, if the jump range
41
+ // is >= to this position then current position is also a good position
42
+
43
+ // time complexity: O(n), space O(1)
44
+ class Solution {
45
+ public:
46
+ bool canJump (vector<int >& nums) {
47
+
48
+ int n=nums.size ();
49
+ int dp[n];
50
+
51
+ memset (dp, 0 , sizeof (dp));
52
+
53
+
54
+ int i;
55
+ dp[n-1 ]=1 ;
56
+
57
+ int good_pos=n-1 ;
58
+
59
+ for (i=n-2 ;i>=0 ;i--)
60
+ {
61
+ int maxJump=i+nums[i];
62
+
63
+ if (maxJump>=good_pos)
64
+ {
65
+ good_pos=i;
66
+
67
+ }
68
+ }
69
+
70
+
71
+ return good_pos==0 ;
72
+ }
73
+ };
Original file line number Diff line number Diff line change
1
+ class MedianFinder {
2
+ public:
3
+ /* * initialize your data structure here. */
4
+
5
+ priority_queue<int , vector<int > , greater<int >> highers;
6
+ priority_queue<int > lowers;
7
+
8
+
9
+ MedianFinder () {
10
+
11
+ }
12
+
13
+ void addNum (int num) {
14
+
15
+
16
+ if (lowers.size ()==0 || num<lowers.top ())
17
+ lowers.push (num);
18
+ else
19
+ highers.push (num);
20
+
21
+ // rebalance the two heaps
22
+ rebalanceHeap ();
23
+
24
+
25
+ }
26
+
27
+ void rebalanceHeap ()
28
+ {
29
+ int lowSize=lowers.size (), highSize=highers.size ();
30
+ if (lowSize-highSize>1 )
31
+ {
32
+
33
+ highers.push (lowers.top ());
34
+ lowers.pop ();
35
+ }
36
+ else if (highSize-lowSize>1 )
37
+ {
38
+
39
+ lowers.push (highers.top ());
40
+ highers.pop ();
41
+
42
+ }
43
+ }
44
+
45
+ double findMedian () {
46
+ double median=0 ;
47
+ if (lowers.size ()==highers.size ())
48
+ {
49
+ median=(lowers.top ()+highers.top ())/2.0 ;
50
+ }
51
+ else
52
+ {
53
+ if (lowers.size ()>highers.size ())
54
+ median=lowers.top ();
55
+ else
56
+ median=highers.top ();
57
+ }
58
+
59
+ return median;
60
+ }
61
+ };
62
+
63
+ /* *
64
+ * Your MedianFinder object will be instantiated and called as such:
65
+ * MedianFinder* obj = new MedianFinder();
66
+ * obj->addNum(num);
67
+ * double param_2 = obj->findMedian();
68
+ */
You can’t perform that action at this time.
0 commit comments