Easy

Call two arms equally strong if the heaviest weights they each are able to lift are equal.

Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms.

Given your and your friend's arms' lifting capabilities find out if you two are equally strong.

Example

  • For yourLeft = 10, yourRight = 15, friendsLeft = 15, and friendsRight = 10, the output should be
    areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = true;
  • For yourLeft = 15, yourRight = 10, friendsLeft = 15, and friendsRight = 10, the output should be
    areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = true;
  • For yourLeft = 15, yourRight = 10, friendsLeft = 15, and friendsRight = 9, the output should be
    areEquallyStrong(yourLeft, yourRight, friendsLeft, friendsRight) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer yourLeft

    A non-negative integer representing the heaviest weight you can lift with your left arm.

    Guaranteed constraints:
    0 ≤ yourLeft ≤ 20.

  • [input] integer yourRight

    A non-negative integer representing the heaviest weight you can lift with your right arm.

    Guaranteed constraints:
    0 ≤ yourRight ≤ 20.

  • [input] integer friendsLeft

    A non-negative integer representing the heaviest weight your friend can lift with his or her left arm.

    Guaranteed constraints:
    0 ≤ friendsLeft ≤ 20.

  • [input] integer friendsRight

    A non-negative integer representing the heaviest weight your friend can lift with his or her right arm.

    Guaranteed constraints:
    0 ≤ friendsRight ≤ 20.

  • [output] boolean

    • true if you and your friend are equally strong, 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 areEquallyStrong(int yourLeft,int yourRight,int friendsLeft,int friendsRight)
{
	if(yourLeft>yourRight)
	{
		int temp=yourLeft;
		yourLeft=yourRight;
		yourRight=temp;
	}

	if(friendsLeft>friendsRight)
	{
		int temp=friendsLeft;
		friendsLeft=friendsRight;
		friendsRight=temp;
	}

	return yourLeft==friendsLeft && yourRight==friendsRight;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05

Medium

Given a string, find out if its characters can be rearranged to form a palindrome.

Example

For inputString = "aabb", the output should be
palindromeRearranging(inputString) = true.

We can rearrange "aabb" to make "abba", which is a palindrome.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 50.

  • [output] boolean

    • true if the characters of the inputString can be rearranged to form a palindrome, 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 palindromeRearranging(char *inputString)
{
	int alphabet[26]={0, };
	bool isOdd=false;

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

	for(int i=0;i<26;i++)
		if(alphabet[i]%2==1)
		{
			if(isOdd)
				return false;
			else
				isOdd=true;
		}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05

Easy

You are given an array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.

Example

For inputArray = [1, 1, 1], the output should be
arrayChange(inputArray) = 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

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

  • [output] integer

    • The minimal number of moves needed to obtain a strictly increasing sequence from inputArray.
      It's guaranteed that for the given test cases the answer always fits signed 32-bit integer type.

[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 arrayChange(arr_integer inputArray)
{
	int count=0;

	for(int i=1;i<inputArray.size;i++)
		if(inputArray.arr[i]<=inputArray.arr[i-1])
		{
			count+=inputArray.arr[i-1]-inputArray.arr[i]+1;
			inputArray.arr[i]=inputArray.arr[i-1]+1;
		}

	return count;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05

Medium

Two arrays are called similar if one can be obtained from another by swapping at most one pair of elements in one of the arrays.

Given two arrays a and b, check whether they are similar.

Example

  • For a = [1, 2, 3] and b = [1, 2, 3], the output should be
    areSimilar(a, b) = true.

    The arrays are equal, no need to swap any elements.

  • For a = [1, 2, 3] and b = [2, 1, 3], the output should be
    areSimilar(a, b) = true.

    We can obtain b from a by swapping 2 and 1 in b.

  • For a = [1, 2, 2] and b = [2, 1, 1], the output should be
    areSimilar(a, b) = false.

    Any swap of any two elements either in a or in b won't make a and b equal.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Array of integers.

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

  • [input] array.integer b

    Array of integers of the same length as a.

    Guaranteed constraints:
    b.length = a.length,
    1 ≤ b[i] ≤ 1000.

  • [output] boolean

    • true if a and b are similar, 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 1

// 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;
// }
//
//
bool areSimilar(arr_integer a,arr_integer b)
{
	int index;
	bool isSame=true;

	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=b.arr[i])
		{
			isSame=false;
			index=i;
			break;
		}

	if(isSame)
		return true;

	for(int i=index+1;i<a.size;i++)
		if(a.arr[i]!=b.arr[i])
		{
			int temp=a.arr[i];
			a.arr[i]=a.arr[index];
			a.arr[index]=temp;
			break;
		}

	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=b.arr[i])
			return false;

	return true;
}

Solution 2

// 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;
// }
//
//
bool areSimilar(arr_integer a,arr_integer b)
{
	bool different=false;

	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=b.arr[i])
		{
			int j=i+1, temp;
			if(different)
				return false;
			different=true;
			while(b.arr[j]!=a.arr[i] || b.arr[i]!=a.arr[j])
			{
				j++;
				if(j>=b.size)
					return false;
			}
			temp=b.arr[i];
			b.arr[i]=b.arr[j];
			b.arr[j]=temp;
		}

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05
<Codesignal> reverseInParentheses  (0) 2020.04.05

Easy

Given a rectangular matrix of characters, add a border of asterisks(*) to it.

Example

For

picture = ["abc",

                    "ded"]

the output should be

addBorder(picture) = ["*****",

                                              "*abc*",

                                              "*ded*",

                                               "*****"]

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.string picture

    A non-empty array of non-empty equal-length strings.

    Guaranteed constraints:
    1 ≤ picture.length ≤ 100,
    1 ≤ picture[i].length ≤ 100.

  • [output] array.string

    • The same matrix of characters, framed with a border of asterisks of width 1.

[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;
// }
//
//
arr_string addBorder(arr_string picture)
{
	arr_string returnPicture=alloc_arr_string(picture.size+2);
	for(int i=0;i<returnPicture.size;i++)
		returnPicture.arr[i]=(char *)malloc((strlen(picture.arr[0])+3)*sizeof(char));

	for(int i=0;i<strlen(picture.arr[0])+2;i++)
		returnPicture.arr[0][i]=returnPicture.arr[returnPicture.size-1][i]='*';

	for(int i=0;i<picture.size;i++)
		sprintf(returnPicture.arr[i+1],"*%s*", picture.arr[i]);

	return returnPicture;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> arrayChange  (0) 2020.04.05
<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05
<Codesignal> reverseInParentheses  (0) 2020.04.05
<Codesignal> Sort by Height  (0) 2020.04.05

Easy

Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.

You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.

Example

For a = [50, 60, 60, 45, 70], the output should be
alternatingSums(a) = [180, 105].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    Guaranteed constraints:
    1 ≤ a.length ≤ 10^5,
    45 ≤ a[i] ≤ 100.

  • [output] array.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

// 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;
// }
//
//
arr_integer alternatingSums(arr_integer a)
{
	arr_integer b=alloc_arr_integer(2);
	b.arr[0]=b.arr[1]=0;

	for(int i=0;i<a.size;i++)
		b.arr[i%2]+=a.arr[i];

	return b;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Are Similar?  (0) 2020.04.05
<Codesignal> Add Border  (0) 2020.04.05
<Codesignal> reverseInParentheses  (0) 2020.04.05
<Codesignal> Sort by Height  (0) 2020.04.05
<Codesignal> isLucky  (0) 2020.04.05

Medium

Write a function that reverses characters in (possibly nested) parentheses in the input string.

Input strings will always be well-formed with matching ()s.

Example

  • For inputString = "(bar)", the output should be
    reverseInParentheses(inputString) = "rab";
  • For inputString = "foo(bar)baz", the output should be
    reverseInParentheses(inputString) = "foorabbaz";
  • For inputString = "foo(bar)baz(blim)", the output should be
    reverseInParentheses(inputString) = "foorabbazmilb";
  • For inputString = "foo(bar(baz))blim", the output should be
    reverseInParentheses(inputString) = "foobazrabblim".
    Because "foo(bar(baz))blim" becomes "foo(barzab)blim" and then "foobazrabblim".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string consisting of lowercase English letters and the characters ( and ). It is guaranteed that all parentheses in inputString form a regular bracket sequence.

    Guaranteed constraints:
    0 ≤ inputString.length ≤ 50.

  • [output] string

    • Return inputString, with all the characters that were in parentheses reversed.

[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 *reverseInParentheses(char *inputString)
{
	int N=strlen(inputString);

	for(int i=0;i<N;i++)
		if(inputString[i]==')')
		{
			int j, k, l;
			if(i==N-1)
				inputString[i]='\0';
			else
			{
				for(j=i;j<N-1;j++)
					inputString[j]=inputString[j+1];
				inputString[j]='\0';
			}
			N--;
			for(j=i-1;inputString[j]!='(';j--);
			for(k=j;k<N-1;k++)
				inputString[k]=inputString[k+1];
			inputString[k]='\0';
			N--;
			i-=2;
			l=(i+j)/2;
			while(j<=l)
			{
				char temp=inputString[i];
				inputString[i--]=inputString[j];
				inputString[j++]=temp;
			}
		}

	return inputString;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Add Border  (0) 2020.04.05
<Codesignal> alternatingSums  (0) 2020.04.05
<Codesignal> Sort by Height  (0) 2020.04.05
<Codesignal> isLucky  (0) 2020.04.05
<Codesignal> commonCharacterCount  (0) 2020.04.05

Easy

Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees. People can be very tall!

Example

For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be
sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer a

    If a[i] = -1, then the ith position is occupied by a tree. Otherwise a[i] is the height of a person standing in the ith position.

    Guaranteed constraints:
    1 ≤ a.length ≤ 1000,
    -1 ≤ a[i] ≤ 1000.

  • [output] array.integer

    • Sorted array a with all the trees untouched.

[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;
// }
//
//
arr_integer sortByHeight(arr_integer a)
{
	int count=0;
	arr_integer b;

	for(int i=0;i<a.size;i++)
		count+=a.arr[i]!=-1;
	b=alloc_arr_integer(count);

	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=-1)
			b.arr[--count]=a.arr[i];

	for(int i=0;i<b.size-1;i++)
		for(int j=i+1;j<b.size;j++)
			if(b.arr[i]>b.arr[j])
			{
				int temp=b.arr[i];
				b.arr[i]=b.arr[j];
				b.arr[j]=temp;
			}

	for(int i=0;i<a.size;i++)
		if(a.arr[i]!=-1)
			a.arr[i]=b.arr[count++];

	return a;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> alternatingSums  (0) 2020.04.05
<Codesignal> reverseInParentheses  (0) 2020.04.05
<Codesignal> isLucky  (0) 2020.04.05
<Codesignal> commonCharacterCount  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05

+ Recent posts