일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Problem Solving
- 만화도
- 파비최
- 프로젝트
- 스탭퍼
- 운동
- 뭐든
- 괜찮음
- 잡생각
- 미드시청
- 링피트
- 사이드
- 10분
- Writing
- English
- 개발자
- 화상영어
- 3줄정리
- 영어공부
- Daily Challenge
- 쓰릴오브파이트
- realclass
- 30분
- leetcode
- 읽기
- 영어원서읽기
- 매일
- 리얼 클래스
- FIT XR
- 월간
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