Easy

After becoming famous, the CodeBots decided to move into a new building together. Each of the rooms has a different cost, and some of them are free, but there's a rumour that all the free rooms are haunted! Since the CodeBots are quite superstitious, they refuse to stay in any of the free rooms, or any of the rooms below any of the free rooms.

Given matrix, a rectangular matrix of integers, where each value represents the cost of the room, your task is to return the total sum of all rooms that are suitable for the CodeBots (ie: add up all the values that don't appear below a 0).

Example

  • For

    matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]]

    the output should be
    matrixElementsSum(matrix) = 9.

    There are several haunted rooms, so we'll disregard them as well as any rooms beneath them. Thus, the answer is 1 + 5 + 1 + 2 = 9.

  • For

    matrix = [[1, 1, 1, 0], [0, 5, 0, 1], [2, 1, 3, 10]]

    the output should be
    matrixElementsSum(matrix) = 9.

    Note that the free room in the final column makes the full column unsuitable for bots (not just the room directly beneath it). Thus, the answer is 1 + 1 + 1 + 5 + 1 = 9.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.array.integer matrix

    A 2-dimensional array of integers representing the cost of each room in the building. A value of 0 indicates that the room is haunted.

    Guaranteed constraints:
    1 ≤ matrix.length ≤ 5,
    1 ≤ matrix[i].length ≤ 5,
    0 ≤ matrix[i][j] ≤ 10.

  • [output] integer

    • The total price of all the rooms that are suitable for the CodeBots to live in.

[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 matrixElementsSum(arr_arr_integer matrix)
{
	int sum=0;

	for(int i=0;i<matrix.size;i++)
		for(int j=0;j<matrix.arr[0].size;j++)
			if(matrix.arr[i].arr[j]==0)
				for(int k=i+1;k<matrix.size;k++)
					matrix.arr[k].arr[j]=0;

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

	return sum;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> commonCharacterCount  (0) 2020.04.05
<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05

Easy

Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.

Note: sequence a0, a1, ..., an is considered to be a strictly increasing if a0 < a1 < ... < an. Sequence containing only one element is also considered to be strictly increasing.

Example

  • For sequence = [1, 3, 2, 1], the output should be
    almostIncreasingSequence(sequence) = false.

    There is no one element in this array that can be removed in order to get a strictly increasing sequence.

  • For sequence = [1, 3, 2], the output should be
    almostIncreasingSequence(sequence) = true.

    You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer sequence

    Guaranteed constraints:
    2 ≤ sequence.length ≤ 10^5,
    -10^5 ≤ sequence[i] ≤ 10^5.

  • [output] boolean

    • Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.

[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;
// }
//
//
bool almostIncreasingSequence(arr_integer sequence)
{
	bool notIncreased=false;
	int index;
	arr_integer removed=alloc_arr_integer(sequence.size-1);

	for(int i=0;i<sequence.size-1;i++)
		if(sequence.arr[i+1]<=sequence.arr[i])
		{
			notIncreased=true;
			index=i;
			break;
		}
	if(!notIncreased)
		return true;

	for(int i=0;i<removed.size;i++)
		removed.arr[i]=sequence.arr[i<index?i:i+1];
	notIncreased=false;
	for(int i=0;i<removed.size-1;i++)
		if(removed.arr[i+1]<=removed.arr[i])
		{
			notIncreased=true;
			break;
		}
	if(!notIncreased)
		return true;

	for(int i=0;i<removed.size;i++)
		removed.arr[i]=sequence.arr[i<index+1?i:i+1];
	notIncreased=false;
	for(int i=0;i<removed.size-1;i++)
		if(removed.arr[i+1]<=removed.arr[i])
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> All Longest Strings  (0) 2020.04.05
<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05

Easy

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.

Example

  • For n = 2, the output should be
    shapeArea(n) = 5;
  • For n = 3, the output should be
    shapeArea(n) = 13.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n < 10^4.

  • [output] integer

    • The area of the n-interesting polygon.

[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 shapeArea(int n)
{
	int size=1;

	for(int i=1;i<n;i++)
		size+=i*4;

	return size;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05

Easy

Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3], the output should be
adjacentElementsProduct(inputArray) = 21.

7 and 3 produce the largest product.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer inputArray

    An array of integers containing at least two elements.

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

  • [output] integer

    • The largest product of adjacent elements.

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

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

	return max;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

Easy

Given the string, check if it is a palindrome.

Example

  • For inputString = "aabaa", the output should be
    checkPalindrome(inputString) = true;
  • For inputString = "abac", the output should be
    checkPalindrome(inputString) = false;
  • For inputString = "a", the output should be
    checkPalindrome(inputString) = true.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A non-empty string consisting of lowercase characters.

    Guaranteed constraints:
    1 ≤ inputString.length ≤ 10^5.

  • [output] boolean

    • true if inputString is 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 checkPalindrome(char *inputString)
{
	for(int i=0;i<strlen(inputString)/2;i++)
		if(inputString[i]!=inputString[strlen(inputString)-i-1])
			return false;
	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

Easy

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

Example

  • For year = 1905, the output should be
    centuryFromYear(year) = 20;
  • For year = 1700, the output should be
    centuryFromYear(year) = 17.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer year

    A positive integer, designating the year.

    Guaranteed constraints:
    1 ≤ year ≤ 2005.

  • [output] integer

    • The number of the century the year is in.

[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 centuryFromYear(int year)
{
	return year%100==0?year/100:year/100+1;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> add  (0) 2020.04.05

Easy

Write a function that returns the sum of two numbers.

Example

For param1 = 1 and param2 = 2, the output should be
add(param1, param2) = 3.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer param1

    Guaranteed constraints:
    -1000 ≤ param1 ≤ 1000.

  • [input] integer param2

    Guaranteed constraints:
    -1000 ≤ param2 ≤ 1000.

  • [output] integer

    • The sum of the two inputs.

[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 add(int param1,int param2)
{
	return param1+param2;
}

 

728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> shapeArea  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05

+ Recent posts