We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Greedy
  4. Candies
  5. Discussions

Candies

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 648 Discussions, By:

recency

Please Login in order to post a comment

  • timothyalban
    4 days ago+ 0 comments

    My Solution in Java 8, O(n) time and O(1) space

        public static long candies(int n, List<Integer> arr) {
            int streakUp = 1, streakDown = 1, lastStreakUp = 1;
            long total = 1; 
            for(int i = 0; i<n-1; i++){            
                if(arr.get(i) > arr.get(i+1)){					
                    streakDown++;							
                    if(lastStreakUp > streakDown-1){							
                        total += streakDown-1;									
                    }else{ 
                        total += streakDown;
                    }								
                    streakUp = 1;
                }						
                else if(arr.get(i) < arr.get(i+1)){						
                    streakUp++;
                    streakDown = 1;
                    total += streakUp;
                    lastStreakUp = streakUp;								
                }
                else{					
                    streakUp = streakDown = lastStreakUp = 1;
                    total++;							
                }
            }
            return total;
        }
    
    0|
    Permalink
  • dehaka
    1 month ago+ 0 comments
    long long candies(int n, vector<int> arr) {
        vector<pair<int, int>> v;
        vector<int> candy(arr.size());
        long long total = 0;
        for (int i=0; i < arr.size(); i++) {
            v.push_back({arr[i], i});
        }
        sort(v.begin(), v.end());
        for (int i=0; i < v.size(); i++) {
            int p = v[i].second;
            if ((p == 0 or arr[p] <= arr[p-1]) and (p == arr.size()-1 or arr[p] <= arr[p+1])){
                candy[p] = 1;
                total++;
                continue;
            } else if (p != 0 and arr[p] > arr[p-1] and (p == arr.size()-1 or arr[p] <= arr[p+1])) {
                candy[p] = candy[p-1] + 1;
            } else if (p != arr.size()-1 and (p ==0 or arr[p] <= arr[p-1]) and arr[p] > arr[p+1]) {
                candy[p] = candy[p+1] + 1;
            } else if ((p == 0 or arr[p] > arr[p-1]) and (p == arr.size()-1 or arr[p] > arr[p+1])) {
                if (p == 0) candy[p] = candy[p+1] + 1;
                else if (p == arr.size()-1) candy[p] = candy[p-1] + 1;
                else candy[p] = max(candy[p-1], candy[p+1]) + 1;
            }
            total = total + candy[p];
        }
        return total;
    }
    
    0|
    Permalink
  • dev_tonmoy
    1 month ago+ 0 comments

    Checkout this:

    # Author : Tonmoy M
    # Author URL : https://qinetique.github.io
    # Problem : Candies
    # Sub-Domain : Interview Preparation kit | Dynamic Programming
    # Domain : HackerRank
    # Problem URL : https://www.hackerrank.com/challenges/max-array-sum/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dynamic-programming
    import sys
    
    
    def candies(n, arr):
        c = [1] * n
        for i in range(1, n):
            if arr[i] > arr[i - 1]:
                c[i] = c[i - 1] + 1
        for i in range(n - 2, -1, -1):
            if arr[i] > arr[i + 1] and c[i] <=c[i+1]:
                c[i] = c[i + 1] + 1
        return sum(c)
    
    
    if __name__ == '__main__':
        fptr = sys.stdout
        n = int(input().strip())
        arr = []
        for _ in range(n):
            arr_item = int(input().strip())
            arr.append(arr_item)
        result = candies(n, arr)
        fptr.write(str(result) + '\n')
        fptr.close()
    
    1|
    Permalink
  • nuowen0612
    1 month ago+ 0 comments

    My solution in python:

    def candies(n, arr):
        # Write your code here
        results = [1 for _ in range(n)]
        
        # ascending
        for i in range(1, n):
            if arr[i] > arr[i - 1]:
                results[i] = results[i - 1] + 1
        
        for i in range(n - 2, -1, -1):
            if arr[i] > arr[i + 1]:
                results[i] = max(results[i + 1] + 1, results[i])
            
        return sum(results)
    
    1|
    Permalink
  • marinvioana
    4 months ago+ 0 comments

    My solution in Java 8:

    public static long candies(int n, List<Integer> arr) {
            int[] result = new int[n];
            Arrays.fill(result, 1);
    
            // crescator
            for (int i = 1; i < n; i++) {
                if (arr.get(i) > arr.get(i - 1)) {
                    result[i] = result[i - 1] + 1;
                }
            }
    
            // descrescator
            for (int i = n - 1; i > 0; i--) {
                if (arr.get(i) < arr.get(i - 1) && (result[i - 1] <= result[i])) {
                    result[i - 1] = result[i] + 1;
                }
            }
    
            long candies = 0;
            for (int i = 0; i < n; i++) {
                candies += result[i];
            }
    
            return candies;
        }
    
    1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy