일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- DWG
- attribute
- LeetCode
- Substring with Concatenation of All Words
- Python
- Regular Expression
- Decorator
- 파이썬
- t1
- Convert Sorted List to Binary Search Tree
- 315. Count of Smaller Numbers After Self
- 컴퓨터의 구조
- concurrency
- Class
- 프로그래머스
- 운영체제
- shiba
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- Protocol
- Generator
- 43. Multiply Strings
- iterator
- 715. Range Module
- 시바견
- 30. Substring with Concatenation of All Words
- 밴픽
- kaggle
- Python Code
- data science
Archives
- Today
- Total
Scribbling
[C++] vector sum, vector max 본문
LeetCode: 410. Split Array Largest Sum
https://leetcode.com/problems/split-array-largest-sum/description/
Split Array Largest Sum - LeetCode
Can you solve this real interview question? Split Array Largest Sum - Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized. Return the minimized largest sum of the split.
leetcode.com
class Solution {
public:
int splitArray(vector<int>& nums, int k) {
int ret = accumulate(nums.begin(), nums.end(), 0);
int left = *max_element(nums.begin(), nums.end());
int right(ret);
while (left <= right) {
int mid = (left + right) / 2;
int n = numGroups(nums, mid);
if (n <= k) {
ret = min(ret, mid);
right = mid - 1;
}
else {
left = mid + 1;
}
}
return ret;
}
int numGroups(vector<int>& nums, int limit) const {
int ret = 0;
int total = 0;
for (int num : nums) {
total += num;
if (total > limit) {
total = num;
ret++;
}
}
return ret + 1;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] vector sum, dp table with vector (0) | 2023.09.02 |
---|---|
[C++] unordered_map, unordered_set, substr (0) | 2023.08.31 |
[C++] ostringstream, isdigit (0) | 2023.08.29 |
[C++] to_string, deque, string split (0) | 2023.08.24 |
[C++] istringstream (0) | 2023.08.22 |