Leetcode with dani
1.29K subscribers
202 photos
14 videos
56 files
243 links
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
Download Telegram
Constraints:

2 <= letters.length <= 104
letters[i] is a lowercase English letter.
letters is sorted in non-decreasing order.
letters contains at least two different characters.
target is a lowercase English letter.
๐Ÿ‘4
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
let's submit our answer:
it's 91% faster.๐Ÿ˜Ž
๐Ÿ‘5๐Ÿ‘1
แ‹จแˆ›แ‰ฅแ‹ต แ‰ขแˆ˜แˆตแˆˆแŠ แฃ แ‹ตแŠ•แŒˆแ‰ต แŠ แŠ•แ‰บแŠ• แˆณแŒฃ
แŠ แˆจแ‰„ แ‰คแ‰ต แˆ”แŒ„ แฃ แŠ แˆจแ‰„ แˆตแŒ แŒฃ
แŠ แŠ•แ‹ต แŒแŒฅแˆ แˆฐแˆ›แˆ
แแ‰…แˆญแˆฝแŠ• แŠจแˆแ‰ค แฃ แАแ‰…แˆŽ แ‹จแˆšแ‹ซแ‹ˆแŒฃแกแก
แ‹ซแ‹แˆ แˆ˜แŠ•แŒˆแ‹ต แ‰ แˆšแˆ...
แˆˆแˆ˜แŠ•แŒˆแ‹ฐแŠ› แˆฐแ‹ แฃ แˆฐแŠซแˆซแˆ แ‹จแƒแˆแ‹
แŠฅแ‹› แŒ‹ แ‰แŒญ แ‰ฅแˆŽ...
"แŠจแ‹ณแ‰ฝแŠ" แŠฅแ‹ซแˆˆ แฃ แ‹จแˆšแˆˆแˆแˆแˆแ‹
แˆ˜แŠ•แŒˆแ‹ต แ‹จแˆšแˆ แŒแŒฅแˆ™ แฃแŒ†แˆฎแ‹ฌแŠ• แŒˆแˆจแˆแ‹แกแก
แŒ†แˆฎแ‹ฌ แˆฒแŒˆแˆจแ แฃ
แŒ แ‰ฃแˆณ แŒฃแˆˆแ‰ฅแŠ แฃ แŒแŒฅแˆ™แŠ• แŠฅแŠ•แ‹ณแˆแˆจแˆณ
"แˆ˜แŠ•แŒˆแ‹ต แŠ แ‹ซแ‹ฐแˆญแˆตแˆ
แˆ˜แŠ•แŒˆแ‹ต แŠ แ‹ญแˆ˜แˆแˆตแˆ แฃ แŠฅแŒแˆญ แŠซแˆแ‰ฐแАแˆณ!!!"
แŠฅแ‹ซแˆˆ แ‹ญแŒˆแŒฅแˆ›แˆ...
แ‹ฐแŒ‹แŒแˆž แ‹ฐแŒ‹แŒแˆž แฃ แˆŒแˆ‹ แŠ แ‹ญแŠ“แŒˆแˆญแˆ
แŠฅแˆฑแˆ แˆแŠญ แŠฅแŠ•แ‹ฐแŠ” ...
แŠ แŠ•แ‹ทแŠ• แ‰ แˆ˜แˆ”แ‹ท แฃ แˆณแ‹ซแŒฃแ‰ต แŠ แ‹ญแ‰€แˆญแˆแกแก
แ‰ฅแ‰ป แˆฐแŠญแˆชแ‹ซแˆˆแˆ
แˆˆแˆ˜แŠ•แŒˆแ‹ฐแŠ› แˆดแ‰ต แฃ
แ‹จแ‰ฐแƒแˆ แŒแŒฅแˆแŠ• แฃ แŒ†แˆฎแ‹ฌ แ‹ซแ‹ฐแˆžแŒฃแˆ
แˆ˜แˆ”แ‹ตแˆฝแŠ• แ‹ซแ‹จ
แŠฅแŠ•แ‹ฐแˆŒแˆŒแˆฝแŠ แˆฒแ‹ซแ‹แ‰… แฃ แˆŠแŠ–แˆจแŠ แ‹ญแˆ˜แŒฃแˆ
แ‹ญแˆ… แАแ‹ แˆ˜แŠ•แŒˆแ‹ต แˆ›แˆˆแ‰ต!
แ‹จแˆฐแŠซแˆซแˆ แŒแŒฅแˆแŠ• แˆฐแŠญแˆฌ แˆตแˆจแ‹ณแ‹
แˆ›แแ‰€แˆจ แˆณแ‹ญแˆ†แŠ• แˆˆแŠ” แฃ แˆ˜แˆญแˆณแ‰ต แАแ‹ แˆšแŒŽแ‹ณแ‹!!!

แˆฐแˆˆแ‹ˆแ‹ฐแ‹ตแŠฉแ‰ต แАแ‹
๐Ÿ‘7โคโ€๐Ÿ”ฅ3โค1
let's takcle leet code medium question for the first time
๐Ÿ‘2
#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.
๐Ÿ‘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
๐Ÿ‘2
Constraints:

3 <= arr.length <= 105
0 <= arr[i] <= 106
arr is guaranteed to be a mountain array.
๐Ÿ‘2
How do we solve this? Any ideas?
Did you solve the peak mountain array problem?
Anonymous Poll
48%
yes ๐Ÿ’ช
52%
noo ๐Ÿ˜…
๐Ÿ‘2
any one who wants to share ur answer .
๐Ÿ‘1
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.
๐Ÿ‘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!
๐Ÿ‘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
let's submit our answer:
๐ŸŽ‰3
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.
๐Ÿ‘3
Input: n = 5
Output: 2
Explanation: Because the 3rd row is incomplete, we return 2.
๐Ÿ‘2
Input: n = 8
Output: 3
Explanation: Because the 4th row is incomplete, we return 3
๐Ÿ‘5๐Ÿ”ฅ1