LeetCode 2230: The Users That Are Eligible for Discount
Problem Description
Explanation:
We are given a list of integers representing the purchase amounts of users. Users are eligible for a discount if their purchase amount is greater than or equal to $100. We need to return a list of users who are eligible for a discount.
To solve this problem, we iterate through the list of purchase amounts and check if each amount is greater than or equal to $100. If it is, we add the index of that user to our result list.
- Time complexity: O(n) where n is the number of users
- Space complexity: O(1) if we ignore the space used for the result list
:
Solutions
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> usersWithDiscount(List<Integer> purchaseAmounts) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < purchaseAmounts.size(); i++) {
if (purchaseAmounts.get(i) >= 100) {
result.add(i);
}
}
return result;
}
}
Loading editor...