'HackerRank.com'에 해당되는 글 4건

  1. 2021.10.04 Repeated String
  2. 2021.10.04 Jumping on the Clouds
  3. 2021.10.04 Counting Valleys
  4. 2021.10.04 Sales by Match
HackerRank.com2021. 10. 4. 22:01

There is a string, , of lowercase English letters that is repeated infinitely many times. Given an integer, , find and print the number of letter a's in the first  letters of the infinite string.

Example

The substring we consider is , the first  characters of the infinite string. There are  occurrences of a in the substring.

Function Description

Complete the repeatedString function in the editor below.

repeatedString has the following parameter(s):

  • s: a string to repeat
  • n: the number of characters to consider

Returns

  • int: the frequency of a in the substringInput Format
  • Sample Input
  • Sample Input 0aba 10Sample Output 07Explanation 0
    The first  letters of the infinite string are abaabaabaa. Because there are  a's, we return .a 1000000000000Sample Output 11000000000000Explanation 1
    Because all of the first  letters of the infinite string are a, we return .
  • Sample Input 1
  • The first line contains a single string, .
    The second line contains an integer, 

 

-- Answer

    public static long repeatedString(string s, long n)

    {

        if(s == "a"return n;

        long result = 0;

        long length = s.Length;

        result = s.Count(f => (f == 'a'));        

        long repeat = n / length;

        result = result * repeat;

        long remain = n % length;

        for(int i=0; i <  remain; i++){

            if('a' == s[i]){

                result++;

            }

        }

        

       return result;  

    }

'HackerRank.com' 카테고리의 다른 글

Jumping on the Clouds  (0) 2021.10.04
Counting Valleys  (0) 2021.10.04
Sales by Match  (0) 2021.10.04
Posted by yuwol
HackerRank.com2021. 10. 4. 22:00

There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus  or . The player must avoid the thunderheads. Determine the minimum number of jumps it will take to jump from the starting postion to the last cloud. It is always possible to win the game.

For each game, you will get an array of clouds numbered  if they are safe or  if they must be avoided.

Example

Index the array from . The number on each cloud is its index in the list so the player must avoid the clouds at indices  and . They could follow these two paths:  or . The first path takes  jumps while the second takes . Return .

Function Description

Complete the jumpingOnClouds function in the editor below.

jumpingOnClouds has the following parameter(s):

  • int c[n]: an array of binary integers

Returns

  • int: the minimum number of jumps requiredInput Format
  • Output Format
  • Print the minimum number of jumps needed to win the game.7 0 0 1 0 0 1 0Sample Output 04Explanation 0:
    The player must avoid  and . The game can be won with a minimum of  jumps:Sample Input 16 0 0 0 0 1 0Sample Output 13Explanation 1:
    The only thundercloud to avoid is . The game can be won in  jumps:
  • Sample Input 0
  • The first line contains an integer , the total number of clouds. The second line contains  space-separated binary integers describing clouds  where .

 

-- Answer

public static int jumpingOnClouds(List<int> c)
    {
        int jumps = 0;
        int currentLocation = 0;
                
        while(currentLocation < c.Count-1){
            if(currentLocation+2 <= c.Count-1 && c[currentLocation+2] == 0) currentLocation = currentLocation+2;
            else if (currentLocation+1 <= c.Count-1 && c[currentLocation+1] == 0) currentLocation = currentLocation+1;
            jumps++;
            if(currentLocation == c.Count-1){
                break;
            }
        }
        
        return jumps;
    }

'HackerRank.com' 카테고리의 다른 글

Repeated String  (0) 2021.10.04
Counting Valleys  (0) 2021.10.04
Sales by Match  (0) 2021.10.04
Posted by yuwol
HackerRank.com2021. 10. 4. 21:58

An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly  steps, for every step it was noted if it was an uphill, , or a downhill,  step. Hikes always start and end at sea level, and each step up or down represents a  unit change in altitude. We define the following terms:

  • A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level.

Given the sequence of up and down steps during a hike, find and print the number of valleys walked through.

Example

 

The hiker first enters a valley  units deep. Then they climb out and up onto a mountain  units high. Finally, the hiker returns to sea level and ends the hike.

Function Description

Complete the countingValleys function in the editor below.

countingValleys has the following parameter(s):

  • int steps: the number of steps on the hike
  • string path: a string describing the path

Returns

  • int: the number of valleys traversedInput FormatSample Output
  • 1
  • Explanation
  • If we represent _ as sea level, a step up as /, and a step down as \, the hike can be drawn as:_/\ _ \ / \/\/The hiker enters and leaves one valley.
  • Sample Input
  • 8 UDDDUDUU
  • The first line contains an integer , the number of steps in the hike.
    The second line contains a single string , of  characters that describe the path.
  • Constraints

 

-- Answer

    public static int countingValleys(int steps, string path)
    {
        int ans = 0;
        int level = 0;
        for(int i=0; i < steps; i++){
            if(path[i] == 'U'){
                level++;
            }else{
                level--;
            }
            if((level == 0) && path[i] == 'U') ans++;
        }
        
        return ans;
    }

'HackerRank.com' 카테고리의 다른 글

Repeated String  (0) 2021.10.04
Jumping on the Clouds  (0) 2021.10.04
Sales by Match  (0) 2021.10.04
Posted by yuwol
HackerRank.com2021. 10. 4. 21:57

There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

Example

There is one pair of color  and one of color . There are three odd socks left, one of each color. The number of pairs is .

Function Description

Complete the sockMerchant function in the editor below.

sockMerchant has the following parameter(s):

  • int n: the number of socks in the pile
  • int ar[n]: the colors of each sock

Returns

  • int: the number of pairsInput FormatSample Output
  • 3
  • Explanation
  • There are three pairs of socks.
  • Sample Input
  • STDIN Function ----- -------- 9 n = 9 10 20 20 10 10 30 50 10 20 ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]
  • The first line contains an integer , the number of socks represented in .
    The second line contains  space-separated integers, , the colors of the socks in the pile.

 

-- Answer

    public static int sockMerchant(int n, List<int> ar)
    {
        int[] pairsToSell = new int[n];
        int countToSell = 0;
        foreach(int sock in ar) {
            var ind = sock % n;
            pairsToSell[ind] +=1;
            if (pairsToSell[ind] == 2) {
                countToSell++;
                pairsToSell[ind] = 0;
            }
        }
        return countToSell;
    }

'HackerRank.com' 카테고리의 다른 글

Repeated String  (0) 2021.10.04
Jumping on the Clouds  (0) 2021.10.04
Counting Valleys  (0) 2021.10.04
Posted by yuwol