[Hacker Rank] Organizing Containers of Balls (C++)

2023. 10. 9. 15:59ยท๐Ÿ–ฅ๏ธ Study Note/Coding Test

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
'๐Ÿ–ฅ๏ธ Study Note/Coding Test' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [๋ฐฑ์ค€ 1931] ํšŒ์˜์‹ค ๋ฐฐ์ •(C++)
  • [๋ฐฑ์ค€ 1003] ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜ (C++)
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]level.3 - ์นด๋“œ ์ง ๋งž์ถ”๊ธฐ(C++)
  • [ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]level.3 - ๋ถ€๋Œ€๋ณต๊ท€ (C++)
Beankong_
Beankong_
์ฃผ๋‹ˆ์–ด ํด๋ผ์ด์–ธํŠธ ํ”„๋กœ๊ทธ๋ž˜๋จธ ๊ณต๋ถ€ ๊ธฐ๋ก
  • Beankong_
    Beankong's Devlog
    Beankong_
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ์ „์ฒด ๊ธ€ (146)
      • โ›… Daily (0)
      • ๐Ÿ–ฅ๏ธ Study Note (2)
        • C++ (1)
        • Unreal Engine (5)
        • Coding Test (123)
        • Design Patteren (5)
        • VCS (Git..) (1)
        • Server (1)
      • ๐Ÿงญ Devlog (8)
        • ์˜ค๋‹ต๋…ธํŠธ (4)
        • UE5 GameLift Server Test Project (1)
        • TIL (3)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ๋งํฌ

    • ๊ณต์ง€์‚ฌํ•ญ

    • ์ธ๊ธฐ ๊ธ€

    • ํƒœ๊ทธ

      ํ—ฌํ…Œ์ด์ปค
      OnlineSubsystem
      ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
      programmers
      unrealengine build system
      ๊ฒŒ์ž„ ๊ฐœ๋ฐœ
      ๊ฒŒ์ž„ ๋ชจ์ž‘
      ์•Œ๊ณ ๋ฆฌ์ฆ˜
      ๊ทธ๋ž˜ํ”„ ์ˆœํšŒ
      UnrealEngine5
      ํ”„๋ฃŒ๊ทธ๋ž˜๋จธ์Šค
      propertyaccess
      UnrealEngine
      cpp
      unrealengine module
      ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
      ๊ฒŒ์ž„ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      ๊ทธ๋ฆฌ๋””(greedy)
      ๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    • ์ตœ๊ทผ ๋Œ“๊ธ€

    • ์ตœ๊ทผ ๊ธ€

    • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
    Beankong_
    [Hacker Rank] Organizing Containers of Balls (C++)
    ์ƒ๋‹จ์œผ๋กœ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”