위장
Algorithm 2019. 8. 20. 14:58- 위장
-
darklight
sublimevimemacs
C#
문제 설명
스파이들은 매일 다른 옷을 조합하여 입어 자신을 위장합니다.
예를 들어 스파이가 가진 옷이 아래와 같고 오늘 스파이가 동그란 안경, 긴 코트, 파란색 티셔츠를 입었다면 다음날은 청바지를 추가로 입거나 동그란 안경 대신 검정 선글라스를 착용하거나 해야 합니다.
종류이름
얼굴 | 동그란 안경, 검정 선글라스 |
상의 | 파란색 티셔츠 |
하의 | 청바지 |
겉옷 | 긴 코트 |
스파이가 가진 의상들이 담긴 2차원 배열 clothes가 주어질 때 서로 다른 옷의 조합의 수를 return 하도록 solution 함수를 작성해주세요.
제한사항
- clothes의 각 행은 [의상의 이름, 의상의 종류]로 이루어져 있습니다.
- 스파이가 가진 의상의 수는 1개 이상 30개 이하입니다.
- 같은 이름을 가진 의상은 존재하지 않습니다.
- clothes의 모든 원소는 문자열로 이루어져 있습니다.
- 모든 문자열의 길이는 1 이상 20 이하인 자연수이고 알파벳 소문자 또는 '_' 로만 이루어져 있습니다.
- 스파이는 하루에 최소 한 개의 의상은 입습니다.
입출력 예
clothesreturn
[[yellow_hat, headgear], [blue_sunglasses, eyewear], [green_turban, headgear]] | 5 |
[[crow_mask, face], [blue_sunglasses, face], [smoky_makeup, face]] | 3 |
입출력 예 설명
예제 #1
headgear에 해당하는 의상이 yellowhat, greenturban이고 eyewear에 해당하는 의상이 blue_sunglasses이므로 아래와 같이 5개의 조합이 가능합니다.
1. yellow_hat 2. blue_sunglasses 3. green_turban 4. yellow_hat + blue_sunglasses 5. green_turban + blue_sunglasses
예제 #2
face에 해당하는 의상이 crowmask, bluesunglasses, smoky_makeup이므로 아래와 같이 3개의 조합이 가능합니다.
1. crow_mask 2. blue_sunglasses 3. smoky_makeup
javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
function solution(clothes){
var answer = 1;
var obj = {};
clothes.forEach(element => {
console.log(element, element[1]);
if(obj[element[1]] >= 1){
obj[element[1]] += 1;
}else{
obj[element[1]] = 1;
}
});
console.log(obj);
for(var x in obj){
console.log(x, obj[x]);
answer *= (obj[x] + 1);
}
return answer - 1;
}
var clothes = [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]];
var result = solution(clothes);
console.log(result);
//(동일 부위 A의 의상 개수 + 1) * (동일 부위 B의 의상 개수 + 1) * ... -1
console.log((2+1) * (1+1) -1);
console.log((3+1) -1);
|
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
using System;
using System.Collections;
using System.Collections.Generic;
namespace _10
{
class Program
{
static void Main(string[] args)
{
string[,] clothes = new string[3,2]{{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"}, {"green_turban", "headgear"}};
//string[,] clothes = {{"crow_mask", "face"}, {"blue_sunglasses", "face"}, {"smoky_makeup", "face"}};
var p = new Problem();
var result = p.Solution(clothes);
Console.WriteLine("{0}", result);
}
}
public class Problem {
private Dictionary<string, int> dic = new Dictionary<string, int>();
int answer = 1;
public int Solution(string[,] clothes){
for(int i = 0; i<3; i++){
if(dic.ContainsKey(clothes[i, 1])){
if(dic[clothes[i, 1]] >= 1){
dic[clothes[i, 1]] += 1;
}
}else{
}
}
}
}
}
}
|
에....?!??!?!????
c# 성공
2차원 배열의 길이가 달라지면 대응 하지 못했기 때문...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace _10
{
class Program
{
static void Main(string[] args)
{
string[,] clothes = new string[,]{{"yellow_hat", "headgear"}, {"blue_sunglasses", "eyewear"}, {"green_turban", "headgear"}};
//string[,] clothes = {{"crow_mask", "face"}, {"blue_sunglasses", "face"}, {"smoky_makeup", "face"}};
var p = new Problem();
var result = p.Solution(clothes);
Console.WriteLine("{0}", result);
}
}
public class Problem {
private Dictionary<string, int> dic = new Dictionary<string, int>();
int answer = 1;
public int Solution(string[,] clothes){
try{
//Console.WriteLine("{0} {1}", i, clothes[i, 1]);
var key = clothes[i, 1];
if(dic.ContainsKey(key)){
if(dic[key] >= 1){
dic[key] += 1;
}
}else{
dic.Add(key, 1);
}
}catch{
}
}
}
}
}
}
|
다른 사람들 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(string[,] clothes)
{
int answer = 1;
Dictionary<string, int> dic = new Dictionary<string, int>();
// 같은 종류끼리 건수 카운트
for (int i = 0; i < clothes.GetLength(0); i++)
{
string strName = clothes[i, 0];
string strType = clothes[i, 1];
if (dic.ContainsKey(strType)) dic[strType]++;
}
foreach (var item in dic)
{
}
return answer - 1; // 의상을 한개는 걸쳐야 한다
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
using System;
using System.Collections.Generic;
public class Solution
{
public int solution(string[,] clothes)
{
Dictionary <string, int> dictionaryCloth = new Dictionary <string, int>();
int answer = 1;
for (int i = 0; i < clothes.GetLength(0); ++i)
{
string clothType = clothes[i, 1];
if (dictionaryCloth.ContainsKey(clothType) == false)
{
}
else
{
++dictionaryCloth[clothType];
}
}
foreach (var item in dictionaryCloth)
{
}
return --answer;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
using System;
using System.Collections.Generic;
public class Solution {
public int solution(string[,] clothes) {
int answer = 0;
Dictionary<string, int> counter = new Dictionary<string, int>();
for(int i = 0; i < clothes.GetLength(0); i++){
string equipSlot = clothes[i,1];
if(counter.ContainsKey(equipSlot)){
counter[equipSlot]++;
}
else{
}
}
return answer;
answer = 1;
foreach(int count in counter.Values){
answer *= count;
}
return answer-1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(string[,] clothes)
{
int answer = 1;
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < clothes.GetLength(0); i++)
{
string type = clothes[i, 1];
if (dic.ContainsKey(type))
dic[type]++;
}
answer *= (arr[i] + 1);
return answer - 1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
public class Solution {
public int solution(string[,] clothes) {
int answer = 0;
Dictionary<string, HashSet<string>> dic = new Dictionary<string,HashSet<string>>();
{
string cloth = clothes[n, 0];
string type = clothes[n, 1];
if (!dic.ContainsKey(type))
if (!dic[type].Contains(cloth))
dic[type].Add(cloth);
}
int nCnt = 0;
{
{
}
nCnt += nTempCnt;
}
answer = nCnt;
return answer;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Solution {
public int solution(string[,] clothes)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
int answer = 1;
if (dic.ContainsKey(clothes[i, 1]))
{
dic[clothes[i, 1]] += 1;
}
else {
}
}
answer = answer * (dic[key] + 1);
}
answer = answer - 1;
return answer;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
using System;
using System.Collections.Generic;
public class Solution {
public int solution(string[,] clothes)
{
int answer = 1;
Dictionary<string, int> dic = new Dictionary<string, int>();
List<int> list = new List<int>();
for(int i = 0;i<clothes.GetLength(0);i++)
{
if (!dic.ContainsKey(clothes[i, 1]))
{
dic[clothes[i, 1]] = 1;
}
else
{
dic[clothes[i, 1]]++;
}
}
var buffer = dic.GetEnumerator();
while(buffer.MoveNext())
{
var pair = buffer.Current;
}
foreach(var buf in list)
{
answer *= buf;
}
return answer-1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
using System;
using System.Collections.Generic;
public class Solution {
public int solution(string[,] clothes) {
Dictionary<string, List<string>> thiefClothes = new Dictionary<string, List<string>>();
for (int i = 0; i < count; i++)
{
if(thiefClothes.ContainsKey(clothes[i, 1]))
{
thiefClothes[clothes[i, 1]].Add(clothes[i, 0]);
}
else
{
List<string> list = new List<string>();
list.Add(clothes[i, 0]);
thiefClothes.Add(clothes[i, 1], list);
}
}
int answer = 1;
{
answer *=( thiefClothes[info].Count + 1);
}
return answer - 1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
using System;
using System.Collections.Generic;
public class Solution {
public int solution(string[,] clothes) {
int answer = 1;
Dictionary<String, int> dic = new Dictionary<String, int>();
for(int i=0; i<clothes.GetLength(0); i++) {
var typeName = clothes[i,1];
if(!dic.ContainsKey(typeName)) {
} else {
dic[typeName]++;
}
}
foreach(var item in dic) {
}
return answer - 1;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
using System;
public class Solution {
public int solution(string[,] clothes) {
int answer = 1;
string[] clothType = new string[clothes.GetLength(0)];
int[] clothNum = new int[clothes.GetLength(0)];
int validNum = 0; //의상 종류 수
for(int i = 0; i < clothes.GetLength(0); i++) //의상 종류와 그 수량 추려내기
{
bool isOverlap = false;
for(int j = 0; j < validNum; j++)
{
if(clothType[j] == clothes[i, 1])
{
clothNum[j]++;
isOverlap = true;
}
}
if(!isOverlap)
{
clothType[validNum] = clothes[i, 1];
clothNum[validNum] = 1;
validNum++;
}
}
answer *= (clothNum[i] + 1);
answer -= 1;
return answer;
}
}
|
'Algorithm' 카테고리의 다른 글
LeetCode | Two Sum (0) | 2019.08.21 |
---|---|
베스트앨범 (0) | 2019.08.20 |
전화번호 목록 (0) | 2019.08.20 |
완주하지 못한 선수 (0) | 2019.08.20 |
[js] 소수 구하기 (0) | 2019.08.20 |