-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_1498.cs
59 lines (50 loc) · 1.4 KB
/
No_1498.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace LeetCode
{
public class No_1498
{
public int NumSubseq(int[] nums, int target)
{
Array.Sort(nums);
int mod = 1000000007;
int[] sq = new int[nums.Length];
sq[0] = 1;
for (int i = 1; i < nums.Length; i++)
{
sq[i] = (sq[i - 1] * 2) % mod;
}
int sum = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] * 2 > target)
break;
for (int j = nums.Length - 1; j >= i; j--)
{
if (nums[i] + nums[j] <= target)
{
sum = (sum + sq[j - i]) % mod;
break;
}
}
}
return sum;
}
public int NumSubseq1(int[] nums, int target)
{
Array.Sort(nums);
int mod = 1000000007;
int sum = 0;
for (int i = 0; i < nums.Length; i++)
{
for (int j = nums.Length - 1; j >= i; j--)
{
if (nums[i] + nums[j] <= target)
{
sum += (int)((Math.Pow(2, (j - i) + 1) / 2) % mod);
break;
}
}
}
return sum;
}
}
}