Leetcode with dani
Открыть в Telegram
Join us and let's tackle leet code questions together: improve your problem-solving skills Preparing for coding interviews learning new algorithms and data structures connect with other coding enthusiasts
Больше1 257
Подписчики
Нет данных24 часа
Нет данных7 дней
-930 день
Архив постов
1 257
But since it has O(N) time complexity, we need to find a more efficient approach. This is where binary search comes in handy. By using binary search with left = 1 and right = the number of coins, we can cut the array size in half each time and find the answer with O(logN) time complexity.
1 257
Let's say n is the number of stairs and c is the number of coins. Based on this, we can iterate from 1 up to n, where (n+1 * n) / 2 > c. When we find the first n that fulfills this condition, we subtract 1 so that n is the maximum number of stairs that we can build with the given number of coins.
1 257
First, how can we determine the number of coins that are enough for building an n-staircase? Let's look at an example: to build 1 stair, we need 1 coin; for 2 stairs, we need 3 coins; for 3 stairs, we need 5 coins; and for 4 stairs, we need 10 coins. What do you notice from this pattern? The number of stairs and the number of coins have a relationship, so we can use the formula (n+1 * n) / 2 to calculate the number of coins needed based on the number of stairs.
1 257
You can contact me if you have any comments, ideas, questions, or anything to talk about https://t.me/zprogramming_bot
1 257
Input: n = 8
Output: 3
Explanation: Because the 4th row is incomplete, we return 3
1 257
Input: n = 5
Output: 2
Explanation: Because the 3rd row is incomplete, we return 2.
1 257
441. Arranging Coins #binary_search #Easy #leet_code_Q8 #441
You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
Given the integer n, return the number of complete rows of the staircase you will build.
1 257
Answer:
class Solution: def peakIndexInMountainArray(self, arr: List[int]) -> int: left = 0 right = len(arr)-1 while(left<=right): mid = (left+right)//2 if arr[mid] > arr[mid-1] and arr[mid] > arr[mid+1]: return mid elif arr[mid] > arr[mid+1]: right = mid -1 else: left = mid +1
1 257
Here's a common approach to find the peak index in a mountain array:
1 We can use a binary search algorithm.
2 Start with the middle element of the array.
3 If the middle element is greater than its right neighbor, then the peak must be in the left half of the array. Move to the left half and go to step 2 .
4 If the middle element is less than its left neighbor, then the peak must be in the right half of the array. Move to the right half and go to step 2.
5 If the middle element is greater than both its neighbors, then it's the peak!
1 257
Let's solve the peak index problem in mountain arrays. A peak index in a mountain array refers to the index i where:
Elements to the left of index i are strictly increasing: arr[0] < arr[1] < ... < arr[i-1].
Elements to the right of index i are strictly decreasing: arr[i] > arr[i+1] > ... > arr[arr.length - 1].
In simpler terms, the element at the peak index is the largest element in the array, with elements increasing to its left and decreasing to its right.
Уже доступно! Исследование Telegram 2025 — ключевые инсайты года 
