Medium

In the popular Minesweeper game you have a board with some mines and those cells that don't contain a mine have a number in it that indicates the total number of mines in the neighboring cells. Starting off with some arrangement of mines we want to create a Minesweeper game setup.

Example

For

matrix = [[true, false, false],

                   [false, true, false],

                   [false, false, false]]

the output should be

minesweeper(matrix) = [[1, 2, 1],

                                                [2, 1, 1],

                                                [1, 1, 1]]

Check out the image below for better understanding:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.boolean matrix

    A non-empty rectangular matrix consisting of boolean values - true if the corresponding cell contains a mine, false otherwise.

    Guaranteed constraints:
    2 ≤ matrix.length ≤ 100,
    2 ≤ matrix[0].length ≤ 100.

  • [output] array.array.integer

    • Rectangular matrix of the same size as matrix each cell of which contains an integer equal to the number of mines in the neighboring cells. Two cells are called neighboring if they share at least one corner.

[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_arr_integer minesweeper(arr_arr_boolean matrix)
{
	arr_arr_integer mine=alloc_arr_arr_integer(matrix.size);
	for(int i=0;i<mine.size;i++)
		mine.arr[i]=alloc_arr_integer(matrix.arr[0].size);

	for(int i=0;i<mine.size;i++)
		for(int j=0;j<mine.arr[0].size;j++)
		{
			mine.arr[i].arr[j]=0;

			for(int k=-1;k<=1;k++)
				for(int l=-1;l<=1;l++)
					if(!(i+k<0 || j+l<0 || (k==0&&l==0) || i+k>=mine.size || j+l>=mine.arr[0].size))
						mine.arr[i].arr[j]+=matrix.arr[i+k].arr[j+l]==true;
		}

	return mine;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> evenDigitsOnly  (0) 2020.04.05
<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05

Medium

Last night you partied a little too hard. Now there's a black and white photo of you that's about to go viral! You can't let this ruin your reputation, so you want to apply the box blur algorithm to the photo to hide its content.

The pixels in the input image are represented as integers. The algorithm distorts the input image in the following way: Every pixel x in the output image has a value equal to the average value of the pixel values from the 3 × 3 square that has its center at x, including x itself. All the pixels on the border of x are then removed.

Return the blurred image as an integer, with the fractions rounded down.

Example

For

image = [[1, 1, 1],

                   [1, 7, 1],

                   [1, 1, 1]]

the output should be boxBlur(image) = [[1]].

To get the value of the middle pixel in the input 3 × 3 square: (1 + 1 + 1 + 1 + 7 + 1 + 1 + 1 + 1) = 15 / 9 = 1.66666 = 1. The border pixels are cropped from the final result.

For

image = [[7, 4, 0, 1],

                   [5, 6, 2, 2],

                 [6, 10, 7, 8],

                   [1, 4, 2, 0]]

the output should be

boxBlur(image) = [[5, 4],

                                      [4, 4]]

There are four 3 × 3 squares in the input image, so there should be four integers in the blurred output. To get the first value: (7 + 4 + 0 + 5 + 6 + 2 + 6 + 10 + 7) = 47 / 9 = 5.2222 = 5. The other three integers are obtained the same way, then the surrounding integers are cropped from the final result.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer image

    An image, stored as a rectangular matrix of non-negative integers.

    Guaranteed constraints:
    3 ≤ image.length ≤ 100,
    3 ≤ image[0].length ≤ 100,
    0 ≤ image[i][j] ≤ 255.

  • [output] array.array.integer

    • A blurred image represented as integers, obtained through the process in the description.

[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_arr_integer boxBlur(arr_arr_integer image)
{
	arr_arr_integer returnBox=alloc_arr_arr_integer(image.size-2);
	for(int i=0;i<returnBox.size;i++)
		returnBox.arr[i]=alloc_arr_integer(image.arr[0].size-2);

	for(int i=0;i<returnBox.size;i++)
		for(int j=0;j<returnBox.arr[0].size;j++)
			returnBox.arr[i].arr[j]=0;

	for(int i=1;i<image.size-1;i++)
		for(int j=1;j<image.arr[0].size-1;j++)
		{
			for(int k=-1;k<=1;k++)
				for(int l=-1;l<=1;l++)
					returnBox.arr[i-1].arr[j-1]+=image.arr[i+k].arr[j+l];
			returnBox.arr[i-1].arr[j-1]/=9;
		}

	return returnBox;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Array Replace  (0) 2020.04.05
<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05

Easy

You are given an array of integers representing coordinates of obstacles situated on a straight line.

Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.

Find the minimal length of the jump enough to avoid all the obstacles.

Example

For inputArray = [5, 3, 6, 7, 9], the output should be
avoidObstacles(inputArray) = 4.

Check out the image below for better understanding:

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    Non-empty array of positive integers.

    Guaranteed constraints:
    2 ≤ inputArray.length ≤ 1000,
    1 ≤ inputArray[i] ≤ 1000.

  • [output] integer

    • The desired length.

[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 avoidObstacles(arr_integer inputArray)
{
	int max=0, N=1;

	for(int i=0;i<inputArray.size;i++)
		max=inputArray.arr[i]>max?inputArray.arr[i]:max;

	while(N++)
	{
		bool noObstacle=true;
		for(int i=0;i<=max;i+=N)
		{
			for(int j=0;j<inputArray.size;j++)
				if(i==inputArray.arr[j])
				{
					noObstacle=false;
					break;
				}
			if(!noObstacle)
				break;
		}
		if(noObstacle)
			return N;
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Minesweeper  (0) 2020.04.05
<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05

Medium

An IP address is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. There are two versions of the Internet protocol, and thus two versions of addresses. One of them is the IPv4 address.

Given a string, find out if it satisfies the IPv4 address naming rules.

Example

  • For inputString = "172.16.254.1", the output should be
    isIPv4Address(inputString) = true;

  • For inputString = "172.316.254.1", the output should be
    isIPv4Address(inputString) = false.

    316 is not in range [0, 255].

  • For inputString = ".254.255.0", the output should be
    isIPv4Address(inputString) = false.

    There is no first number.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string consisting of digits, full stops and lowercase English letters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 30.

  • [output] boolean

    • true if inputString satisfies the IPv4 address naming rules, 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 isIPv4Address(char *inputString)
{
	int dotCount=0, N=0;

	for(int i=0;i<strlen(inputString);i++)
	{
		if(!isdigit(inputString[i]) && inputString[i]!='.')
			return false;

		if(isdigit(inputString[i]))
		{
			N*=10;
			N+=inputString[i]-'0';

			if(N==0 && inputString[i]=='0' && inputString[i+1]!='.' && i!=strlen(inputString)-1)
				return false;
			if(N>255)
				return false;
		}

		if(inputString[i]=='.')
		{
			dotCount++;
			if(!isdigit(inputString[i-1]) || i==0 || N>255)
				return false;
			N=0;
		}

		if(i==strlen(inputString)-1 && inputString[i]=='.')
			return false;
	}

	return dotCount==3 && N<256;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Box Blur  (0) 2020.04.05
<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> arrayMaximalAdjacentDifference  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05

Easy

Given an array of integers, find the maximal absolute difference between any two of its adjacent elements.

Example

For inputArray = [2, 4, 1, 0], the output should be
arrayMaximalAdjacentDifference(inputArray) = 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

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

  • [output] integer

    • The maximal absolute difference.

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

	for(int i=1;i<inputArray.size;i++)
		max=abs(inputArray.arr[i]-inputArray.arr[i-1])>max?abs(inputArray.arr[i]-inputArray.arr[i-1]):max;

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> avoidObstacles  (0) 2020.04.05
<Codesignal> isIPv4Address  (0) 2020.04.05
<Codesignal> areEquallyStrong  (0) 2020.04.05
<Codesignal> palindromeRearranging  (0) 2020.04.05
<Codesignal> arrayChange  (0) 2020.04.05

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

+ Recent posts