https://www.hackerrank.com/challenges/organizing-containers-of-balls/problem?isFullScreen=true
๋ฌธ์ ์ค๋ช
David has several containers, each with a number of balls in it. He has just enough containers to sort each type of ball he has into its own container. David wants to sort the balls using his sort method.
David wants to perform some number of swap operations such that:
- Each container contains only balls of the same type.
- No two balls of the same type are located in different containers.
Example
containers = [[1, 4], [2, 3]]
David has n = 2 containers and 2 different types of balls, both of which are numbered from 0 to n-1 =1 . The distribution of ball types per container are shown in the following diagram.
In a single operation, David can swap two balls located in different containers.
The diagram below depicts a single swap operation:
In this case, there is no way to have all green balls in one container and all red in the other using only swap operations. Return Impossible.
You must perform q queries where each query is in the form of a matrix, M. For each query, print Possible on a new line if David can satisfy the conditions above for the given matrix. Otherwise, print Impossible.
Function Description
Complete the organizingContainers function in the editor below.
organizingContainers has the following parameter(s):
- int containter[n][m]: a two-dimensional array of integers that represent the number of balls of each color in each container
Returns
- string: either Possible or Impossible
๋ด ํ์ด
์ปจํ ์ด๋ ๋น ํ ์ข ๋ฅ์ ๊ณต๋ค๋ง ๋ด์ ์ ์๋ ๊ฒ์ด ํฌ์ธํธ์ด๋ค.
์ปจํ ์ด๋๋ผ๋ฆฌ ๊ณต์ ๊ตํํ ๋ 1๋1 ๊ตํ๋ง ๊ฐ๋ฅํ๋ฏ๋ก,
์ปจํ ์ด๋ ๋น ๊ณต ๊ฐ์์ ์ข ๋ฅ ๋น ๊ณต ๊ฐ์๊ฐ ์ผ์นํ๋ฉด ์ปจํ ์ด๋ ๋น ํ ์ข ๋ฅ์ ๊ณต๋ค๋ง ๋ด์ ์ ์๋ค.
- Possible
- Impossible
string organizingContainers(vector<vector<int>> container)
{
int n = container.size();
set<int> numOfBallsByContainer, numOfBallsByType;
for(int i=0;i<n;i++){
int bt=0; // Number of balls by type
int bc=0; // Number of balls by container
for(int j=0;j<n;j++){
bc+=container[i][j];
bt+=container[j][i];
}
numOfBallsByContainer.insert(bc);
numOfBallsByType.insert(bt);
}
if(numOfBallsByContainer==numOfBallsByType){
return "Possible";
}
else{
return "Impossible";
}
}
'๐ฅ๏ธ Study Note > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 1931] ํ์์ค ๋ฐฐ์ (C++) (0) | 2023.10.26 |
---|---|
[๋ฐฑ์ค 1003] ํผ๋ณด๋์น ํจ์ (C++) (0) | 2023.10.25 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.3 - ์นด๋ ์ง ๋ง์ถ๊ธฐ(C++) (1) | 2023.10.05 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.3 - ๋ถ๋๋ณต๊ท (C++) (0) | 2023.10.04 |
[ํ๋ก๊ทธ๋๋จธ์ค]level.2 - ๊ฐ์ฅ ํฐ ์ ์ฌ๊ฐํ ์ฐพ๊ธฐ(C++) (0) | 2023.09.28 |