Skip to content

Instantly share code, notes, and snippets.

View Rutvik17's full-sized avatar
🎯
Focusing

Rutvik Rutvik17

🎯
Focusing
  • Software Engineer
  • Toronto Canada
View GitHub Profile
@Rutvik17
Rutvik17 / myLinkedList.java
Created August 9, 2021 02:40
Design A Linked List LeetCode
class DoublyListNode {
int val;
DoublyListNode next, prev;
DoublyListNode (int x) {val = x;}
}
/*
1. Initiate a new linked list: represent a linked list using the head node.
*/
public class MyLinkedList {
@Rutvik17
Rutvik17 / findNUniqueIntegersSumUpToZero.js
Created August 9, 2021 02:21
Find N Unique Integers Sum Up To Zero
/*
Given an integer n, return any array containing n unique integers such that they add up to 0.
Example 1:
Input: n = 5
Output: [-7,-1,1,3,4]
Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].
Example 2:
Input: n = 3
Output: [-1,0,1]
@Rutvik17
Rutvik17 / numberOfStudentsUnableToEatLunch.js
Created August 9, 2021 02:19
Number Of Students Unable To Eat Lunch LeetCode
/*
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.
The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:
If the student at the front of the queue prefers the sandwich on the top of the stack, they will take it and leave the queue.
Otherwise, they will leave it and go to the queue's end.
This continues until none of the queue students want to take the top sandwich and are thus unable to eat.
You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.
Example 1:
Input: students = [1,1,0,0], sandwiches = [0,1,0,1
@Rutvik17
Rutvik17 / insertIntoABinarySearchTree.js
Created August 9, 2021 02:18
Insert Into A Binary Search Tree LeetCode
/*
You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
Example 1:
Input: root = [4,2,7,1,3], val = 5
Output: [4,2,7,1,3,5]
Explanation: Another accepted tree is:
Example 2:
@Rutvik17
Rutvik17 / LRUCache.js
Created August 9, 2021 02:16
LRU Cache LeetCode
/*
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
int get(int key) Return the value of the key if the key exists, otherwise return -1.
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
Follow up:
Could you do get and put in O(1) time complexity?
Example 1:
@Rutvik17
Rutvik17 / validateBinarySearchTree.js
Created August 9, 2021 02:15
Validate Binary Search Tree LeetCode
/*
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
Example 1:
Input: root = [2,1,3]
Output: true
@Rutvik17
Rutvik17 / binaryTreeLevelOrderTraversal.js
Created August 9, 2021 02:14
Binary Tree Level Order Traversal LeetCode
/*
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Example 2:
Input: root = [1]
Output: [[1]]
Example 3:
@Rutvik17
Rutvik17 / binaryTreeInorderTraversal.js
Created August 9, 2021 02:13
Binary Tree Inorder Traversal LeetCode
/*
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Example 2:
Input: root = []
Output: []
Example 3:
@Rutvik17
Rutvik17 / sortColors.js
Created August 9, 2021 02:12
Sort Colors LeetCode
/*
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.
Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
Example 2:
Input: nums = [2,0,1]
@Rutvik17
Rutvik17 / searchInRotatedSortedArray.js
Created August 9, 2021 02:12
Search In Rotated Sorted Array LeetCode
/*
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2: