Share leetcode 875 c++,

duonglucille

New member
#LeetCode, #c ++, #Coding, #AlGorithM, #Giải quyết vấn đề ## leetcode 875 C ++:

Cho một mảng số nguyên `cọc` có độ dài` n`, trong đó `cọc ` đại diện cho số lượng chuối trong đống `i`th.Có những con khỉ K` sẽ ăn chuối.Mỗi con khỉ sẽ ăn tất cả chuối trong một đống, sau đó chuyển sang đống tiếp theo.Những con khỉ sẽ tiếp tục ăn cho đến khi không còn chuối nữa.

Trả lại số lượng cọc tối thiểu sẽ bị bỏ trống sau khi tất cả những con khỉ đã ăn.

**Ví dụ 1:**

`` `
Đầu vào: Cọc = [3,6,7,11], k = 2
Đầu ra: 1
Giải trình:
Những con khỉ sẽ ăn chuối theo thứ tự sau:
- Cọc 1: 3 chuối ăn bởi khỉ 1.
- Cọc 2: 6 chuối ăn bởi khỉ 1.
- Cọc 3: 7 chuối ăn bởi khỉ 2.
- Pile 4: 11 chuối ăn bởi khỉ 2.
Sau khi ăn tất cả chuối, đống 4 sẽ bị bỏ trống.
`` `

** Ví dụ 2: **

`` `
Đầu vào: Cọc = [1000000000,1000000000], K = 1
Đầu ra: 0
Giải trình:
Tất cả các chuối sẽ được ăn bởi một con khỉ.
`` `

**Hạn chế:**

* `n == PILES.LEPTI
* `1 <= n <= 10^5`
* `1 <= cọc <= 10^9`
* `1 <= k <= n`

**Giải pháp:**

`` `C ++
int mineatingSpeed (vector <tint> & piles, int k) {
// Tìm số lượng chuối tối đa trong một đống.
int maxbananas = *max_element (piles.begin (), piles.end ());

// Tìm kiếm nhị phân cho tốc độ ăn tối thiểu sẽ cho phép tất cả các chuối được ăn.
int thấp = 1;
int cao = maxbananas;
while (thấp <cao) {
int mid = (thấp + cao) / 2;
int chuối = 0;
for (int pile: cọc) {
chuối + = (cọc + mid - 1) / mid;
}

if (BananaSeaten <= k) {
cao = giữa;
} khác {
Thấp = Mid + 1;
}
}

trả lại thấp;
}
`` `

** Hashtags: **

* LeetCode
* C ++
* Mã hóa
* Thuật toán
* giải quyết vấn đề
=======================================
#LeetCode, #C++, #Coding, #AlGorithM, #Problem-solving ## Leetcode 875 C++: Koko Eating Bananas

Given an integer array `piles` of length `n`, where `piles` represents the number of bananas in the `i`th pile. There are `k` monkeys that will eat the bananas. Each monkey will eat all the bananas in a single pile, then move on to the next pile. The monkeys will continue eating until there are no more bananas left.

Return the minimum number of piles that will be left empty after all the monkeys have eaten.

**Example 1:**

```
Input: piles = [3,6,7,11], k = 2
Output: 1
Explanation:
The monkeys will eat the bananas in the following order:
- Pile 1: 3 bananas eaten by monkey 1.
- Pile 2: 6 bananas eaten by monkey 1.
- Pile 3: 7 bananas eaten by monkey 2.
- Pile 4: 11 bananas eaten by monkey 2.
After eating all the bananas, pile 4 will be left empty.
```

**Example 2:**

```
Input: piles = [1000000000,1000000000], k = 1
Output: 0
Explanation:
All the bananas will be eaten by a single monkey.
```

**Constraints:**

* `n == piles.length`
* `1 <= n <= 10^5`
* `1 <= piles <= 10^9`
* `1 <= k <= n`

**Solution:**

```c++
int minEatingSpeed(vector<int>& piles, int k) {
// Find the maximum number of bananas in a pile.
int maxBananas = *max_element(piles.begin(), piles.end());

// Binary search for the minimum eating speed that will allow all the bananas to be eaten.
int low = 1;
int high = maxBananas;
while (low < high) {
int mid = (low + high) / 2;
int bananasEaten = 0;
for (int pile : piles) {
bananasEaten += (pile + mid - 1) / mid;
}

if (bananasEaten <= k) {
high = mid;
} else {
low = mid + 1;
}
}

return low;
}
```

**Hashtags:**

* leetcode
* c++
* coding
* algorithm
* problem-solving
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top