Java one line solution with stream


  • 1
    public class Solution {
        public int distributeCandies(int[] candies) {
            return Math.min(candies.length/2, (int)IntStream.of(candies).distinct().count());
        }
    }
    

    The distinct() method returns a stream with distinct elements, and the count() returns a long value representing the count of the elements.


  • 1

    The drawback for the stream solution is we cannot apply early termination trick here.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.