카카오_해커랭크_Easy 30p_Ice Cream Parlor_purmutations

RostoryT·2022년 7월 16일
0

Brute force

목록 보기
12/18



테스트케이스 수
내가 가진 돈
아이스크림 가격 수
아이스크림 금액 금액 금액 금액 [ ]

출력은 인덱스+1를 반환하는 거임 (가격이 아니라 가격의 번호)

아이스크림 가격을 조합해서
내가 원하는 금액 맞추는 것

  • 참고 -> 해커랭크 코드에 "한글"있으면 에러떠서 안된다..!

솔루션 코드 - 실행용

  • 간단한 문제였다
from itertools import permutations

def icecreamParlor(m, arr, n):
    answer = []
    visit = [False]*(n)
    
    for i in permutations(arr, 2):
        if sum(i) == m:
            ans = i
            break
            
    for i in range(n):
        if arr[i] == ans[0] and visit[i] == False:
            answer.append(i+1)
            visit[i] = True
        if arr[i] == ans[1] and visit[i] == False:
            answer.append(i+1)
            visit[i] = True
            
    return answer
            
    

print(" ".join(map(str,icecreamParlor(4, [1, 4, 5, 3, 2], 5))))
print(" ".join(map(str,icecreamParlor(4, [2, 2, 4, 3], 4))))

솔루션 코드 - 제출용

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'icecreamParlor' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER m
#  2. INTEGER_ARRAY arr
#

from itertools import permutations

def icecreamParlor(m, arr, n):
    answer = []
    visit = [False]*(n)
    
    for i in permutations(arr, 2):
        if sum(i) == m:
            ans = i
            break
            
    for i in range(n):
        if arr[i] == ans[0] and visit[i] == False:
            answer.append(i+1)
            visit[i] = True
        if arr[i] == ans[1] and visit[i] == False:
            answer.append(i+1)
            visit[i] = True
            
    return answer

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())

    for t_itr in range(t):
        m = int(input().strip())

        n = int(input().strip())

        arr = list(map(int, input().rstrip().split()))

        result = icecreamParlor(m, arr, n)

        fptr.write(' '.join(map(str, result)))
        fptr.write('\n')

    fptr.close()

profile
Do My Best

0개의 댓글