answer:
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
left = 0
right = len(letters)-1
while(left<=right):
mid = (left+right)//2
if(letters[mid] > target )and (letters[mid-1] <= target or mid==left):
return letters[mid]
elif letters[mid] > target:
right = mid -1
else:
left = mid +1
return letters[0]
👍5☃2
የማብድ ቢመስለኝ ፣ ድንገት አንቺን ሳጣ
አረቄ ቤት ሔጄ ፣ አረቄ ስጠጣ
አንድ ግጥም ሰማሁ
ፍቅርሽን ከልቤ ፣ ነቅሎ የሚያወጣ፡፡
ያውም መንገድ በሚል...
ለመንገደኛ ሰው ፣ ሰካራም የፃፈው
እዛ ጋ ቁጭ ብሎ...
"ከዳችኝ" እያለ ፣ የሚለፈልፈው
መንገድ የሚል ግጥሙ ፣ጆሮዬን ገረፈው፡፡
ጆሮዬ ሲገረፍ ፣
ጠባሳ ጣለብኝ ፣ ግጥሙን እንዳልረሳ
"መንገድ አያደርስም
መንገድ አይመልስም ፣ እግር ካልተነሳ!!!"
እያለ ይገጥማል...
ደጋግሞ ደጋግሞ ፣ ሌላ አይናገርም
እሱም ልክ እንደኔ ...
አንዷን በመሔዷ ፣ ሳያጣት አይቀርም፡፡
ብቻ ሰክሪያለሁ
ለመንገደኛ ሴት ፣
የተፃፈ ግጥምን ፣ ጆሮዬ ያደሞጣል
መሔድሽን ያየ
እንደሌሌሽኝ ሲያውቅ ፣ ሊኖረኝ ይመጣል
ይህ ነው መንገድ ማለት!
የሰካራም ግጥምን ሰክሬ ስረዳው
ማፍቀረ ሳይሆን ለኔ ፣ መርሳት ነው ሚጎዳው!!!
ሰለወደድኩት ነው
👍7❤🔥3❤1
#binary_search #Medium #leet_code_Q7 852. Peak Index in a Mountain Array
An array arr is a mountain if the following properties hold:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
You must solve it in O(log(arr.length)) time complexity.
An array arr is a mountain if the following properties hold:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given a mountain array arr, return the index i such that arr[0] < arr[1] < ... < arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
You must solve it in O(log(arr.length)) time complexity.
👍5
Example 1:
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Example 3:
Input: arr = [0,10,5,2]
Output: 1
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Example 3:
Input: arr = [0,10,5,2]
Output: 1
👍2
Constraints:
3 <= arr.length <= 105
0 <= arr[i] <= 106
arr is guaranteed to be a mountain array.
3 <= arr.length <= 105
0 <= arr[i] <= 106
arr is guaranteed to be a mountain array.
👍2
👍2
join the discussion group and share ur ideas leetcoders
Telegram
leetcoders
You’ve been invited to join this group on Telegram.
👍2
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.
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.
👍2
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 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!
👍2
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
👍7
Media is too big
VIEW IN TELEGRAM
How to be a creative thinker | Carnegie Mellon University Po-Shen Loh
👍2⚡1🔥1
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.
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.
👍3
You can contact me if you have any comments, ideas, questions, or anything to talk about https://news.1rj.ru/str/zprogramming_bot
👍2