This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def search(self, nums: List[int], target: int) -> int: | |
n = len(nums) | |
lo, hi = 0, n - 1 | |
while hi - lo > 1 : | |
mid = (lo + hi)//2 | |
if nums[mid] < target : | |
lo = mid + 1 | |
else: | |
hi = mid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question Link - https://www.hackerearth.com/practice/algorithms/string-algorithm/string-searching/practice-problems/algorithm/palindrome-queries-eefd5c23/ | |
''' | |
Problem | |
You are given a string | |
that contains | |
lowercase alphabets. There are | |
queries of the form | |
. Your task is to determine if every character in the range | |
can be arranged in such a manner that a palindromic substring for that range can be formed. If it is possible to create the palindrome substring in the provided interval, then print Possible. Otherwise, print Impossible. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Space Optimized Iterative | |
class Solution: | |
##Complete this function | |
#Function to find the sum of contiguous subarray with maximum sum. | |
def maxSubArraySum(self,A,N): | |
##Your code here | |
currmax, maxsofar = A[0], A[0] | |
for i in range(1, N): | |
currmax = max(A[i], A[i] + currmax) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: | |
rev = None | |
while head : | |
rev, rev.next, head = head, rev, head.next | |
return rev | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"strconv" | |
"bufio" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
# @param A : tuple of list of integers | |
# @return a list of integers | |
def spiralOrder(self, A): | |
n,m = len(A),len(A[0]) | |
l,r,t,d = 0,m-1,0,n-1 | |
ans = [] | |
direction = 0 | |
while l<= r and t <= d : | |
if direction == 0 : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def merge(self, intervals: List[List[int]]) -> List[List[int]]: | |
if not intervals : | |
return [] | |
ans = [] | |
intervals.sort(key = lambda x :x[0]) | |
for interval in intervals : | |
if not ans or interval[0] > ans[-1][1] : | |
ans.append(interval) | |
else: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(graph , node, parent,visited): | |
visited[node] = True | |
for adj in graph[node] : | |
if visited[adj]: | |
if adj != parent : | |
return True | |
else : | |
if dfs(graph,adj,node,visited) == True : | |
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def kthSmallest(self, root: TreeNode, k: int) -> int: | |
arr = [] | |
def inorder(root,arr): | |
if root.left : | |
inorder(root.left,arr) | |
if root : | |
arr.append(root.val) | |
if root.right : | |
inorder(root.right,arr) |