# 2554. Maximum Number of Integers to Choose From a Range I ###### tags: `Leetcode` `Medium` `Greedy` Link: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/description/ ## 思路 典型greedy 从最小的元素开始加 加到超过maxSum为止 就能找到答案 ## Code ```java= class Solution { public int maxCount(int[] banned, int n, int maxSum) { Set<Integer> set = new HashSet<>(); for(int ban:banned) set.add(ban); int curSum = 0, cnt = 0; for(int i=1; i<=n; i++){ if(set.contains(i)) continue; curSum += i; if(curSum>maxSum){ return cnt; } cnt++; } return cnt; } } ```