Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Carousel
- baekjoon
- swea
- 취준생
- 레지스터
- jsx
- 함수선언식
- HTML
- Navigation Bar
- 18352
- slick
- JS
- 게임 맵 최단거리
- 프로그래머스
- let
- var
- react slick
- navbar
- 코딩테스트
- 함수표현식
- 호이스팅
- Reactjs
- 취뽀기원
- 알고리즘
- JavaScript
- programmers
- 파이썬
- Python
- react-slick
- react
Archives
- Today
- Total
고짬기록
[백준 14929] 귀찮아 (SIB) Python3 본문
문제 주소 및 출처 ❕
https://www.acmicpc.net/problem/14929
14929번: 귀찮아 (SIB)
n과 xi가 주어짇나. n은 10만 이하ㅇ고, xi는 젗ㄹ댓값이 100이하인 정수디이다.
www.acmicpc.net
문제 : 14929 귀찮아 (SIB)
언어 : Python3
itertools.combinations와 2중 for문을 이용한 경우 시간초과 발생😂
결합법칙을 이용하여 통과😍
😥 나의 코드 (시간초과)
❌ [itertools 사용] Python3
import itertools
n = int(input())
arr = list(map(int, input().split()))
ans = 0
a = [x for x in range(n)]
v = itertools.combinations(a, 2)
for w in v:
ans += arr[w[0]] * arr[w[1]]
print(ans)
❌ [2중 for문 사용] Python3
import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
temp = []
for i in range(1, n):
temp.append(sum(arr[i:]))
ans = 0
for i in range(n-1):
ans += arr[i] * temp[i]
print(ans)
💬 나의 코드 (성공❤)
📌 Python3
import sys
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
# 자신까지의 누적합 배열
temp = [arr[0]]
for i in range(1, n):
temp.append(temp[i-1]+arr[i])
# a*b+a*c=a*(b+c)를 이용
# 자신의 값 * 자신 이후의 누적합
ans = 0
for i in range(n-1):
ans += arr[i] * (temp[n-1]-temp[i]) # 전체 누적합 - 자신까지의 누적합
print(ans)
'알고리즘 공부 ⌨ > 백준' 카테고리의 다른 글
[백준 18352] 특정 거리의 도시 찾기 Python3 (0) | 2021.09.26 |
---|---|
[백준 14567] 선수과목 (Prerequisite) Python3 (0) | 2021.09.10 |
[백준 15649] N과 M (1) Python3 (0) | 2021.09.07 |
Comments