Medium

A string is said to be beautiful if each letter in the string appears at most as many times as the previous letter in the alphabet within the string; ie: b occurs no more times than a; c occurs no more times than b; etc.

Given a string, check whether it is beautiful.

Example

  • For inputString = "bbbaacdafe", the output should be

     isBeautifulString(inputString) = true.

    This string contains 3 as, 3 bs, 1 c, 1 d, 1 e, and 1 f (and 0 of every other letter), so since there aren't any letters that appear more frequently than the previous letter, this string qualifies as beautiful.

  • For inputString = "aabbb", the output should be 

    isBeautifulString(inputString) = false.

    Since there are more bs than as, this string is not beautiful.

  • For inputString = "bbc", the output should be 

    isBeautifulString(inputString) = false.

    Although there are more bs than cs, this string is not beautiful because there are no as, so therefore there are more bs than as.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string of lowercase English letters.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 50.

  • [output] boolean

    • Return true if the string is beautiful, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isBeautifulString(char *inputString)
{
	int alphabet[26]={0, };

	for(int i=0;i<strlen(inputString);i++)
		alphabet[inputString[i]-'a']++;
	for(int i=1;i<26;i++)
		if(alphabet[i-1]<alphabet[i])
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05

Easy

Given the positions of a white bishop and a black pawn on the standard chess board, determine whether the bishop can capture the pawn in one move.

The bishop has no restrictions in distance for each move, but is limited to diagonal movement. Check out the example below to see how it can move:

Example

  • For bishop = "a1" and pawn = "c3", the output should be
    bishopAndPawn(bishop, pawn) = true.

     

  • For bishop = "h1" and pawn = "h3", the output should be
    bishopAndPawn(bishop, pawn) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string bishop

    Coordinates of the white bishop in the chess notation.

    Guaranteed constraints:
    bishop.length = 2,
    'a' ≤ bishop[0] ≤ 'h',
    1 ≤ bishop[1] ≤ 8.

  • [input] string pawn

    Coordinates of the black pawn in the same notation.

    Guaranteed constraints:
    pawn.length = 2,
    'a' ≤ pawn[0] ≤ 'h',
    1 ≤ pawn[1] ≤ 8.

  • [output] boolean

    • true if the bishop can capture the pawn, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool bishopAndPawn(char *bishop,char *pawn)
{
	return abs(pawn[0]-bishop[0])==abs(pawn[1]-bishop[1]);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05

Medium

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

  • For n = 5, the output should be
    digitDegree(n) = 0;
  • For n = 100, the output should be
    digitDegree(n) = 1.
    1 + 0 + 0 = 1.
  • For n = 91, the output should be
    digitDegree(n) = 2.
    9 + 1 = 10 -> 1 + 0 = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    5 ≤ n ≤ 10^9.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int digitDegree(int n)
{
	int count=0;

	while(n%10!=n)
	{
		int N=0;

		while(n>0)
		{
			N+=n%10;
			n/=10;
		}
		n=N;

		count++;
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05

Easy

Given a string, output its longest prefix which contains only digits.

Example

For inputString = "123aa1", the output should be
longestDigitsPrefix(inputString) = "123".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 100.

  • [output] string

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char *longestDigitsPrefix(char *inputString)
{
	int i=0;

	while(isdigit(inputString[i++]));
	inputString[--i]='\0';

	return inputString;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05
<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05

Easy

You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the second item weighs weight2 and is worth value2. What is the total maximum value of the items you can take with you, assuming that your max weight capacity is maxW and you can't come back for the items later?

Note that there are only two items and you can't bring more than one item of each type, i.e. you can't take two first items or two second items.

Example

  • For value1 = 10, weight1 = 5, value2 = 6, weight2 = 4, and maxW = 8, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 10.

    You can only carry the first item.

  • For value1 = 10, weight1 = 5, value2 = 6, weight2 = 4, and maxW = 9, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 16.

    You're strong enough to take both of the items with you.

  • For value1 = 5, weight1 = 3, value2 = 7, weight2 = 4, and maxW = 6, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 7.

    You can't take both items, but you can take any of them.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer value1

    Guaranteed constraints:
    2 ≤ value1 ≤ 20.

  • [input] integer weight1

    Guaranteed constraints:
    2 ≤ weight1 ≤ 10.

  • [input] integer value2

    Guaranteed constraints:
    2 ≤ value2 ≤ 20.

  • [input] integer weight2

    Guaranteed constraints:
    2 ≤ weight2 ≤ 10.

  • [input] integer maxW

    Guaranteed constraints:
    1 ≤ maxW ≤ 20.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int knapsackLight(int value1,int weight1,int value2,int weight2,int maxW)
{
	return weight1+weight2<=maxW?value1+value2:maxW>=weight1&&value1>=value2?value1:maxW>=weight2&&value2>=value1?value2:maxW>=weight1&&maxW<weight2?value1:maxW>=weight2&&maxW<weight1?value2:0;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05
<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05
<Codesignal> differentSymbolsNaive  (0) 2020.04.05

Easy

Caring for a plant can be hard work, but since you tend to it regularly, you have a plant that grows consistently. Each day, its height increases by a fixed amount represented by the integer upSpeed. But due to lack of sunlight, the plant decreases in height every night, by an amount represented by downSpeed.

Since you grew the plant from a seed, it started at height 0 initially. Given an integer desiredHeight, your task is to find how many days it'll take for the plant to reach this height.

Example

For upSpeed = 100, downSpeed = 10, and desiredHeight = 910, the output should be
growingPlant(upSpeed, downSpeed, desiredHeight) = 10.

# Day Night
1 100 90
2 190 180
3 280 270
4 370 360
5 460 450
6 550 540
7 640 630
8 730 720
9 820 810
10 910 900

The plant first reaches a height of 910 on day 10.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer upSpeed

    A positive integer representing the daily growth of the plant.

    Guaranteed constraints:
    3 ≤ upSpeed ≤ 100.

  • [input] integer downSpeed

    A positive integer representing the nightly decline of the plant.

    Guaranteed constraints:
    2 ≤ downSpeed < upSpeed.

  • [input] integer desiredHeight

    A positive integer representing the goal height.

    Guaranteed constraints:
    4 ≤ desiredHeight ≤ 1000.

  • [output] integer

    • The number of days that it will take for the plant to reach / pass desiredHeight.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int growingPlant(int upSpeed,int downSpeed,int desiredHeight)
{
	return desiredHeight<=upSpeed?1:(desiredHeight-upSpeed)%(upSpeed-downSpeed)==0?(desiredHeight-upSpeed)/(upSpeed-downSpeed)+1:(desiredHeight-upSpeed)/(upSpeed-downSpeed)+2;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05
<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> firstDigit  (0) 2020.04.05

Easy

Given array of integers, find the maximal possible sum of some of its k consecutive elements.

Example

For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should be
arrayMaxConsecutiveSum(inputArray, k) = 8.
All possible sums of 2 consecutive elements are:

  • 2 + 3 = 5;
  • 3 + 5 = 8;
  • 5 + 1 = 6;
  • 1 + 6 = 7.
    Thus, the answer is 8.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Array of positive integers.

    Guaranteed constraints:
    3 ≤ inputArray.length ≤ 10^5,
    1 ≤ inputArray[i] ≤ 1000.

  • [input] integer k

    An integer (not greater than the length of inputArray).

    Guaranteed constraints:
    1 ≤ k ≤ inputArray.length.

  • [output] integer

    • The maximal possible sum.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int arrayMaxConsecutiveSum(arr_integer inputArray,int k)
{
	int max=0;

	for(int i=0;i<inputArray.size-k+1;i++)
	{
		int sum=0;

		for(int j=0;j<k;j++)
			sum+=inputArray.arr[i+j];

		max=sum>max?sum:max;
	}

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05
<Codesignal> differentSymbolsNaive  (0) 2020.04.05
<Codesignal> firstDigit  (0) 2020.04.05
<Codesignal> extractEachKth  (0) 2020.04.05

Easy

Given a string, find the number of different characters in it.

Example

For s = "cabca", the output should be
differentSymbolsNaive(s) = 3.

There are 3 different characters a, b and c.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s

    A string of lowercase English letters.

    Guaranteed constraints:
    3 ≤ s.length ≤ 1000.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int differentSymbolsNaive(char *s)
{
	int count=0;
	bool *alphabet=(bool *)calloc(26,sizeof(bool));

	for(int i=0;i<strlen(s);i++)
		alphabet[s[i]-'a']=true;

	for(int i=0;i<26;i++)
		count+=alphabet[i];

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> growingPlant  (0) 2020.04.05
<Codesignal> arrayMaxConsecutiveSum  (0) 2020.04.05
<Codesignal> firstDigit  (0) 2020.04.05
<Codesignal> extractEachKth  (0) 2020.04.05
<Codesignal> stringsRearrangement  (0) 2020.04.05

+ Recent posts