Skip to content

Instantly share code, notes, and snippets.

View kuntalchandra's full-sized avatar
🎯
Focusing

Kuntal Chandra kuntalchandra

🎯
Focusing
View GitHub Profile
@kuntalchandra
kuntalchandra / largest_integer.py
Created April 1, 2021 10:03
Given an array of integers A, return the largest integer that only occurs once.
"""
Given an array of integers A, return the largest integer that only occurs once.
If no integer occurs once, return -1.
Example 1:
Input: [5,7,3,9,4,9,8,3,1]
Output: 8
Explanation:
@kuntalchandra
kuntalchandra / palinedrome_linked-list.py
Created April 1, 2021 10:02
Given the head of a singly linked list, return true if it is a palindrome
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
numbers = []
sentinel = head
while sentinel:
@kuntalchandra
kuntalchandra / valid_mountain_array.py
Created December 10, 2020 13:25
Valid Mountain Array
"""
Given an array of integers arr, return true if and only if it is a valid mountain array.
Recall that arr is a mountain array if and only if:
arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < A[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
@kuntalchandra
kuntalchandra / bst_iterator.py
Created December 10, 2020 05:07
Binary Search Tree Iterator
"""
Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):
BSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of
the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
boolean hasNext() Returns true if there exists a number in the traversal to the right of the pointer, otherwise
returns false.
int next() Moves the pointer to the right, then returns the number at the pointer.
Notice that by initializing the pointer to a non-existent smallest number, the first call to next() will return the
smallest element in the BST.
@kuntalchandra
kuntalchandra / place_flowers.py
Created December 5, 2020 10:15
Can Place Flowers
"""
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted
in adjacent plots.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n,
return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
Example 1:
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
@kuntalchandra
kuntalchandra / increasing_order_search_tree.py
Created December 3, 2020 12:28
Increasing Order Search Tree
"""
Given the root of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now
the root of the tree, and every node has no left child and only one right child.
Example 1:
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
Example 2:
Input: root = [5,1,7]
@kuntalchandra
kuntalchandra / linked_list_random_node.py
Created December 2, 2020 09:57
Linked List Random Node
"""
Linked List Random Node
Given a singly linked list, return a random node's value from the linked list.
Each node must have the same probability of being chosen.
Follow up:
What if the linked list is extremely large and its length is unknown to you?
Could you solve this efficiently without using extra space?
Example:
@kuntalchandra
kuntalchandra / shortest_word_distance.py
Last active December 1, 2020 14:56
Shortest Word Distance
"""
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
Example:
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1
@kuntalchandra
kuntalchandra / linked_list_cycle_ii.py
Last active October 27, 2020 08:01
Linked List Cycle II
"""
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Notice that you should not modify the linked list.
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
@kuntalchandra
kuntalchandra / min_depth_binary_tree.py
Created October 23, 2020 04:55
Minimum Depth of Binary Tree
"""
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
Input: root = [3,9,20,null,null,15,7]
Output: 2
Example 2: