일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 개발자
- 잡생각
- 괜찮음
- 화상영어
- 3줄정리
- 30분
- 읽기
- 영어공부
- 링피트
- 프로젝트
- Problem Solving
- Daily Challenge
- 뭐든
- 만화도
- realclass
- English
- Writing
- FIT XR
- 10분
- 스탭퍼
- 영어원서읽기
- 쓰릴오브파이트
- 리얼 클래스
- 파비최
- 사이드
- 월간
- 운동
- 매일
- 미드시청
- leetcode
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.07.08 Today's Challenge 본문
https://leetcode.com/problems/put-marbles-in-bags/
Put Marbles in Bags - LeetCode
Can you solve this real interview question? Put Marbles in Bags - You have k bags. You are given a 0-indexed integer array weights where weights[i] is the weight of the ith marble. You are also given the integer k. Divide the marbles into the k bags accord
leetcode.com
오랫만에 풀어봄
class Solution:
def putMarbles(self, weights: List[int], k: int) -> int:
n = len(weights)
pw = [0] * (n - 1)
for i in range(n-1):
pw[i] = weights[i] + weights[i+1]
pw.sort()
answer = 0
for i in range(k-1):
answer += pw[n-2-i] - pw[i]
return answer
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.07.10 Today's Challenge (0) | 2023.07.10 |
---|---|
2023.07.09 Today's Challenge (0) | 2023.07.09 |
2023.07.07 Today's Challenge (0) | 2023.07.07 |
2023.07.06 Today's Challenge (0) | 2023.07.06 |
2023.07.05 Today's Challenge (0) | 2023.07.05 |
Comments