문제풀이 2021 01 07
2021.01.07 연습
- 사용언어 : java
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int largest = 0;
List<Boolean> result = new ArrayList<Boolean>();
for (int i : candies) {
if (largest <= i) {
largest = i;
}
//if문 대신 max = Math.max(num, max);
}
for (int j : candies) {
if (largest <= (j + extraCandies)) {
result.add(true);
} else {
result.add(false);
}
}
return result;
}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Kids With the Greatest Number of Candies. Memory Usage: 39.1 MB, less than 46.24% of Java online submissions for Kids With the Greatest Number of Candies.
- 다른 코드 참고
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int maxCandies = Arrays.stream(candies).max().getAsInt();
List<Boolean> l = new ArrayList<Boolean>();
for(int j=0;j<candies.length;j++){
if(candies[j]+extraCandies>= maxCandies){
l.add(true);
}
else{
l.add(false);
}
}
return l;
}
}
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max = -1;
for (int i = 0; i < candies.length; i++) {
max = Math.max(candies[i], max);
}
List<Boolean> b = new ArrayList<>();
for (int i = 0; i < candies.length; i++) {
b.add(candies[i] + extraCandies >= max);
}
return b;
}
}
class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int maximum = Arrays.stream(candies).max().orElse(0);
return Arrays.stream(candies).mapToObj(x -> x + extraCandies >= maximum).collect(Collectors.toList());
}
}