153. Find Minimum in Rotated Sorted Array
#leet_code_Q10 #Medium #binary_search #Q_153
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.
Example 1:
Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Example 2:
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Example 3:
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
#leet_code_Q10 #Medium #binary_search #Q_153
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.
Example 1:
Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
Example 2:
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
Example 3:
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
Leetcode with dani
153. Find Minimum in Rotated Sorted Array #leet_code_Q10 #Medium #binary_search #Q_153 Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if…
class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums)-1
while(left<=right):
mid = (left+right)//2
if nums[mid] <= nums[mid-1]:
return nums[mid]
elif nums[mid] >= nums[left] and nums[left] >nums[left-1]:
left = mid + 1
else:
right = mid -1
👍3
If a coin rolls around a circular path whose circumference is twice the circumference of the coin, how many full rotations does the coin complete by the time it returns to its starting point?
Anonymous Quiz
20%
1
37%
2
20%
3
22%
4
👍4⚡2🤯2
Leetcode with dani
If a coin rolls around a circular path whose circumference is twice the circumference of the coin, how many full rotations does the coin complete by the time it returns to its starting point?
Don't try to answer at first glance. it's more challenging than it seems
Leetcode with dani
Which topic should we cover next?
based on this poll we will start doing two pointer and sliding window respectively
344. Reverse String
#Topics #twopointers #strings
Write a function that reverses a string. The input string is given as an array of characters s
You must do this by modifying the input array in-place with O(1) extra memory
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
link
#Topics #twopointers #strings
Write a function that reverses a string. The input string is given as an array of characters s
You must do this by modifying the input array in-place with O(1) extra memory
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
link
LeetCode
Reverse String - LeetCode
Can you solve this real interview question? Reverse String - Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm]…
You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm]…
👍3
#leet_code_Q_12 #easy #two_pointer #Q_202 202.Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 231 - 1
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 231 - 1
Leetcode with dani
344. Reverse String #Topics #twopointers #strings Write a function that reverses a string. The input string is given as an array of characters s You must do this by modifying the input array in-place with O(1) extra memory Example 1: Input:…
class Solution:
def reverseString(self, s: List[str]) -> None:
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1
How do you convert a tuple into a list in Python?
a) tuple.toList()
b) list(tuple)
c) tuple.list()
a) tuple.toList()
b) list(tuple)
c) tuple.list()
What will be the output of the following code?
print('Hello' * 3)
print('Hello' * 3)
Anonymous Quiz
69%
a) HelloHelloHello
8%
b) HelloHelloHelloHello
9%
c) Hello, Hello, Hello
14%
D) Error
Leetcode with dani
https://youtu.be/jM2dhDPYMQM?si=WG3yrnoQvRyEvnkm
i recommend this video for those who want to learn sliding window algorithm , we will be working on this algorithm in the coming days
#leet_code_Q_13 #easy #Sliding_window #Q_219 219. Contains Duplicate II
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
👍2❤🔥1
out of topic I just watched an amazing video that dives into the theory of relativity and how time works. ⏳
It’s fascinating how scientists always talk about light, and I used to think they had a beef with speed of light 😂 But now I understand why speed of light is so special and its profound connection to time. https://youtu.be/5BBHEZFearI?si=dGWI303LG9XXgQYh
It’s fascinating how scientists always talk about light, and I used to think they had a beef with speed of light 😂 But now I understand why speed of light is so special and its profound connection to time. https://youtu.be/5BBHEZFearI?si=dGWI303LG9XXgQYh
YouTube
Einstein’s Special Relativity Theory | Does Time really Slow down
What is Time dilation? How speed of light affects space time? Let’s understand Time dilation with Einstein’s Special relativity theory.
@Klonusk
To support on patreon (video noscript with high-res explainer art works) : https://patreon.com/Klonusk975…
@Klonusk
To support on patreon (video noscript with high-res explainer art works) : https://patreon.com/Klonusk975…
👍5👀1
Websites To Increase Your Problem-Solving Skills
LeetCode: LeetCode is widely used for practicing coding problems, especially for preparing for technical interviews. It has a vast collection of problems categorized by difficulty and topic.
HackerRank: HackerRank offers challenges in various domains like algorithms, data structures, artificial intelligence, and more. It is also a good platform for preparing for coding interviews.
CodeSignal: CodeSignal provides a variety of coding challenges and also has a feature to simulate technical interviews.
Codeforces: Codeforces is a competitive programming platform that hosts regular contests. It’s a great place to practice algorithmic problems and improve your speed and accuracy.
AtCoder: AtCoder is a Japanese competitive programming site that offers regular contests and a rich set of problems to solve.
TopCoder: TopCoder is one of the original competitive programming platforms. It offers Single Round Matches (SRMs) and has a large problem archive.
Exercism: Exercism offers exercises in various programming languages with mentor feedback.
Codewars: Codewars provides a community-driven platform where you can solve coding challenges and compare solutions with others.
LeetCode: LeetCode is widely used for practicing coding problems, especially for preparing for technical interviews. It has a vast collection of problems categorized by difficulty and topic.
HackerRank: HackerRank offers challenges in various domains like algorithms, data structures, artificial intelligence, and more. It is also a good platform for preparing for coding interviews.
CodeSignal: CodeSignal provides a variety of coding challenges and also has a feature to simulate technical interviews.
Codeforces: Codeforces is a competitive programming platform that hosts regular contests. It’s a great place to practice algorithmic problems and improve your speed and accuracy.
AtCoder: AtCoder is a Japanese competitive programming site that offers regular contests and a rich set of problems to solve.
TopCoder: TopCoder is one of the original competitive programming platforms. It offers Single Round Matches (SRMs) and has a large problem archive.
Exercism: Exercism offers exercises in various programming languages with mentor feedback.
Codewars: Codewars provides a community-driven platform where you can solve coding challenges and compare solutions with others.
Leetcode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Leetcode with dani
#leet_code_Q_13 #easy #Sliding_window #Q_219 219. Contains Duplicate II Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. …
## Intuition
To solve this problem, we can use a sliding window approach along with a hash map (dictionary) to efficiently check for duplicate elements within a given range. The idea is to maintain a window of size
## Algorithm
1. Initialize an empty hash map (dictionary) to store the most recent index of each element.
2. Iterate over the array
- For each element
- If
- Update the hash map to store the current index
3. If no duplicates within the range
## Code
## Complexity Analysis
- Time Complexity: O(n), where n is the length of the array
- Space Complexity: O(min(n, k)), where n is the length of the array. The space used by the hash map is proportional to the number of unique elements within any window of size
This algorithm ensures that we efficiently find any duplicates within a specified range, making use of the hash map for quick lookups and updates.
To solve this problem, we can use a sliding window approach along with a hash map (dictionary) to efficiently check for duplicate elements within a given range. The idea is to maintain a window of size
k and check if any element within this window is duplicated.## Algorithm
1. Initialize an empty hash map (dictionary) to store the most recent index of each element.
2. Iterate over the array
nums:- For each element
nums[i]:- If
nums[i] is already in the hash map and the difference between the current index i and the stored index j is less than or equal to k, return true.- Update the hash map to store the current index
i of nums[i].3. If no duplicates within the range
k are found, return false.## Code
def containsNearbyDuplicate(nums, k):
num_indices = {}
for i, num in enumerate(nums):
if num in num_indices and i - num_indices[num] <= k:
return True
num_indices[num] = i
return False
# Example 1
print(containsNearbyDuplicate([1, 2, 3, 1], 3)) # Output: True
# Example 2
print(containsNearbyDuplicate([1, 0, 1, 1], 1)) # Output: True
# Example 3
print(containsNearbyDuplicate([1, 2, 3, 1, 2, 3], 2)) # Output: False
## Complexity Analysis
- Time Complexity: O(n), where n is the length of the array
nums. We only need to iterate through the array once.- Space Complexity: O(min(n, k)), where n is the length of the array. The space used by the hash map is proportional to the number of unique elements within any window of size
k. In the worst case, this is O(n), but usually, it's limited by k.This algorithm ensures that we efficiently find any duplicates within a specified range, making use of the hash map for quick lookups and updates.
👌4👍2
#leet_code_Q_14 #Q_2269 #easy #Sliding_window
2269. Find the K-Beauty of a Number
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions:
It has a length of k.
It is a divisor of num.
Given integers num and k, return the k-beauty of num.
Note:
Leading zeros are allowed.
0 is not a divisor of any value.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: num = 240, k = 2
Output: 2
Explanation: The following are the substrings of num of length k:
- "24" from "240": 24 is a divisor of 240.
- "40" from "240": 40 is a divisor of 240.
Therefore, the k-beauty is 2.
Constraints:
1 <= num <= 109
1 <= k <= num.length (taking num as a string)
2269. Find the K-Beauty of a Number
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions:
It has a length of k.
It is a divisor of num.
Given integers num and k, return the k-beauty of num.
Note:
Leading zeros are allowed.
0 is not a divisor of any value.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: num = 240, k = 2
Output: 2
Explanation: The following are the substrings of num of length k:
- "24" from "240": 24 is a divisor of 240.
- "40" from "240": 40 is a divisor of 240.
Therefore, the k-beauty is 2.
Constraints:
1 <= num <= 109
1 <= k <= num.length (taking num as a string)
👍3⚡2