Skip to content

Commit 0e54250

Browse files
committed
new soln
1 parent cf340a3 commit 0e54250

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

2529.MaximumCount.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// 2529. Maximum Count of Positive Integer and Negative Integer
2+
// Given an array nums sorted in non-decreasing order, return the maximum between the number of positive integers and the number of negative integers.
3+
// In other words, if the number of positive integers in nums is pos and the number of negative integers is neg, then return the maximum of pos and neg.
4+
// Note that 0 is neither positive nor negative.
5+
// Example 1:
6+
7+
// Input: nums = [-2,-1,-1,1,2,3]
8+
// Output: 3
9+
// Explanation: There are 3 positive integers and 3 negative integers. The maximum count among them is 3.
10+
// Example 2:
11+
12+
// Input: nums = [-3,-2,-1,0,0,1,2]
13+
// Output: 3
14+
// Explanation: There are 2 positive integers and 3 negative integers. The maximum count among them is 3.
15+
// Example 3:
16+
17+
// Input: nums = [5,20,66,1314]
18+
// Output: 4
19+
// Explanation: There are 4 positive integers and 0 negative integers. The maximum count among them is 4.
20+
21+
//Linear Search
22+
public class Solution {
23+
public int MaximumCount(int[] nums) {
24+
int pos = 0, neg = 0;
25+
foreach(int num in nums){
26+
if(num < 0) neg++;
27+
if(num > 0) pos++;
28+
}
29+
return Math.Max(neg,pos);
30+
}
31+
}
32+
33+
//Binary Search
34+
public class Solution {
35+
public int MaximumCount(int[] nums) {
36+
int pos = 0, neg = 0;
37+
pos = nums.Length - FindFirstPos(nums);
38+
pos = pos > nums.Length ? 0 : pos;
39+
neg = FindLastNeg(nums) + 1;
40+
return Math.Max(neg,pos);
41+
}
42+
public int FindLastNeg(int[] nums){
43+
int left, right, mid;
44+
left = 0;
45+
right = nums.Length-1;
46+
while(left <= right){
47+
mid = left + (right-left)/2;
48+
if(nums[mid] < 0 &&
49+
((nums.Length > mid + 1 && nums[mid + 1] >= 0)
50+
|| nums.Length == mid + 1)){
51+
return mid;
52+
}
53+
else if(nums[mid] < 0)
54+
left = mid + 1;
55+
else
56+
right = mid - 1;
57+
}
58+
return -1;
59+
}
60+
public int FindFirstPos(int[] nums){
61+
int left, right, mid;
62+
left = 0;
63+
right = nums.Length-1;
64+
while(left <= right){
65+
mid = left + (right-left)/2;
66+
if(nums[mid] > 0 &&
67+
((mid > 0 && nums[mid - 1] <= 0)
68+
|| mid == 0)){
69+
return mid;
70+
}
71+
else if(nums[mid] <= 0)
72+
left = mid + 1;
73+
else
74+
right = mid - 1;
75+
}
76+
return -1;
77+
}
78+
}

350.Intersect.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 350. Intersection of Two Arrays II
2+
// Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
3+
4+
// Example 1:
5+
6+
// Input: nums1 = [1,2,2,1], nums2 = [2,2]
7+
// Output: [2,2]
8+
// Example 2:
9+
10+
// Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
11+
// Output: [4,9]
12+
// Explanation: [9,4] is also accepted.
13+
14+
public class Solution {
15+
public int[] Intersect(int[] nums1, int[] nums2) {
16+
Array.Sort(nums1);
17+
Array.Sort(nums2);
18+
List<int> res = new List<int>();
19+
int p1 = 0, p2 = 0;
20+
while(p1 < nums1.Length && p2 < nums2.Length){
21+
if(nums1[p1] == nums2[p2]){
22+
res.Add(nums1[p1]);
23+
p1++;
24+
p2++;
25+
}
26+
else{
27+
if(nums1[p1] < nums2[p2])
28+
p1++;
29+
else
30+
p2++;
31+
}
32+
}
33+
return res.ToArray();
34+
}
35+
}

485.FindMaxConsecutiveOnes.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,19 @@ public int FindMaxConsecutiveOnes(int[] nums)
2020
}
2121
return max;
2222
}
23+
}
24+
25+
public class Solution {
26+
public int FindMaxConsecutiveOnes(int[] nums) {
27+
int max = 0, lmax = 0;
28+
foreach(int num in nums){
29+
if(num == 0){
30+
max = Math.Max(max,lmax);
31+
lmax = 0;
32+
}
33+
else
34+
lmax++;
35+
}
36+
return Math.Max(max,lmax);
37+
}
2338
}

704.BinarySearch.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// 704. Binary Search
2+
// Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
3+
// You must write an algorithm with O(log n) runtime complexity.
4+
// Example 1:
5+
6+
// Input: nums = [-1,0,3,5,9,12], target = 9
7+
// Output: 4
8+
// Explanation: 9 exists in nums and its index is 4
9+
// Example 2:
10+
11+
// Input: nums = [-1,0,3,5,9,12], target = 2
12+
// Output: -1
13+
// Explanation: 2 does not exist in nums so return -1
14+
15+
public class Solution {
16+
public int Search(int[] nums, int target) {
17+
int left, right, mid;
18+
left = 0;
19+
right = nums.Length-1;
20+
while(left <= right){
21+
mid = left + (right-left)/2;
22+
if(target == nums[mid]){
23+
return mid;
24+
}
25+
else if(target > nums[mid])
26+
left = mid + 1;
27+
else
28+
right = mid - 1;
29+
}
30+
return -1;
31+
}
32+
}

0 commit comments

Comments
 (0)