-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily172.java
More file actions
56 lines (50 loc) · 1.37 KB
/
Copy pathdaily172.java
File metadata and controls
56 lines (50 loc) · 1.37 KB
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
// Solution 1
class Solution {
public int maxCount(int[] banned, int n, int maxSum) {
/*
have array from 1 - n, remove all elements present in banned
add numbers in sorted order to find max numbers summed <= max sum
*/
Set<Integer> banned_set = new HashSet<>();
for (int num : banned) {
banned_set.add(num);
}
List<Integer> nums = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if (!banned_set.contains(i))
nums.add(i);
}
System.out.println(nums.toString());
int sum = 0;
int count = 0;
for (int num : nums) {
sum += num;
count++;
System.out.println(sum);
if (sum > maxSum)
return count == 0 ? 0 : count - 1;
}
return count;
}
}
// Solution 2
class Solution {
public int maxCount(int[] banned, int n, int maxSum) {
int sum = 0;
int result = 0;
boolean[] isBanned = new boolean[10001];
for (int ban : banned) {
isBanned[ban] = true;
}
for (int i = 1; i <= n; i++) {
if (sum + i > maxSum) {
break;
}
if (!isBanned[i]) {
sum += i;
result++;
}
}
return result;
}
}