728x90
자료구조 3주차 숙제
쓱 최대로 할인 적용하기
📄 나의 코드
def get_max_discounted_price(prices, coupons):
prices.sort(reverse=-1)
coupons.sort(reverse=-1)
result = 0
if len(coupons) < len(prices):
for i in range(min(len(coupons), len(prices))):
result += prices[i] * (1 - (coupons[i] / 100))
for j in range(len(coupons), len(prices)):
result += prices[j]
elif len(coupons) >= len(prices):
for i in range(min(len(coupons), len(prices))):
result += prices[i] * (1 - (coupons[i] / 100))
elif len(coupons) == 0:
for i in range(len(prices)):
result += prices[i]
result = int(result)
return result
print("정답 = 926000 / 현재 풀이 값 = ", get_max_discounted_price([30000, 2000, 1500000], [20, 40]))
print("정답 = 485000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], [10, 70, 30, 20]))
print("정답 = 1550000 / 현재 풀이 값 = ", get_max_discounted_price([50000, 1500000], []))
print("정답 = 1458000 / 현재 풀이 값 = ", get_max_discounted_price([20000, 100000, 1500000], [10, 10, 10]))
역순 정렬하기 위해서는list.sort(reverse=-1)
올바른 괄호
📄 나의 코드
def is_correct_parenthesis(string):
stack = []
for c in string:
if c == '(':
stack.append(c)
elif c == ')' and len(stack) == 0:
stack.append(c)
elif c == ')' and stack[-1] == '(':
stack.pop()
if len(stack) == 0:
result = True
else:
result = False
return result
print("정답 = True / 현재 풀이 값 = ", is_correct_parenthesis("(())"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis(")"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())))"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("())()"))
print("정답 = False / 현재 풀이 값 = ", is_correct_parenthesis("((())"))
stack에 character 하나씩 꺼내서 '('이면 스택에 넣어주고(append),
stack에 character가 ')' 이고 스택에 비어있지 않으면서 stack의 peek값(stack[-1])이 '('이면 빼주고
아니면, ')'도 넣어준다.
'알고리즘' 카테고리의 다른 글
자료구조 3주차 숙제_2 (1) | 2022.12.28 |
---|---|
코딩 테스트 연습 28일 (0) | 2022.12.28 |
코딩 테스트 연습 27일 (0) | 2022.12.27 |
코딩 테스트 연습 22일,23일,26일 (0) | 2022.12.26 |
코딩 테스트 연습 21일 (0) | 2022.12.21 |