Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Linked list cycle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}
21 changes: 21 additions & 0 deletions Longest increasing subsequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public int lengthOfLIS(int[] nums) {
if(nums==null || nums.length==0)
return 0;

int[] max = new int[nums.length];
Arrays.fill(max, 1);

int result = 1;
for(int i=0; i<nums.length; i++){
for(int j=0; j<i; j++){
if(nums[i]>nums[j]){
max[i]= Math.max(max[i], max[j]+1);

}
}
result = Math.max(max[i], result);
}

return result;
}}
18 changes: 18 additions & 0 deletions counting bits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int[] countBits(int n) {
int[] dp = new int[n+1];

dp[0] = 0;
int nearest = 0;
for (int k = 1; k <= n; k++) {
if ((k & (k-1)) == 0) {
dp[k] = 1;
nearest = k;
} else {
dp[k] = dp[k-nearest] + dp[nearest];
}
}

return dp;
}
}
27 changes: 27 additions & 0 deletions invert binary tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
TreeNode right = invertTree(root.right);
TreeNode left = invertTree(root.left);
root.left = right;
root.right = left;
return root;
}
}
66 changes: 66 additions & 0 deletions kth smallest element in a bst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
class Result
{
// Record how many nodes we visited in-order-wise so far;
// if the value is k then we know we've found the kth
// smallest and no need to do further recursion.
public Integer leftCount;
// null if not kth smallest; nonnull if we hit kth smallest
public Integer value;

public Result(Integer leftCount, Integer value)
{
this.leftCount = leftCount;
this.value = value;
}
}
public int kthSmallest(TreeNode root, int k) {
// inorder DFS
return helper(root,k).value;
}

private Result helper(TreeNode n, int k)
{
if (n==null)
{
return new Result(0,null);
}

Result left = helper(n.left, k);
Result right = null;
if (left.leftCount==k) // kth smallest node is in left tree
{
return left;
}
else if (left.leftCount+1==k) // current root is our answer
{
return new Result(k,n.val);
}
else if (left.leftCount+1<k)
{
right = helper(n.right,k-(left.leftCount+1));
if (right.value != null) // kth smallest node is in right tree
{
return new Result(k,right.value);
}
}

// kth smallest node is not in this subtree rooted at n
return new Result(left.leftCount+1+right.leftCount, null);
}
}
28 changes: 28 additions & 0 deletions maximum depth of bst.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {

if (root == null) {
return 0;
}

int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);

return Math.max(leftDepth, rightDepth) + 1;
}
}
16 changes: 16 additions & 0 deletions maximum product subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public static int maxProduct(int[] nums) {
if (nums.length == 0) return 0;

int result = nums[0];

for (int i = 0; i < nums.length; i++) {
int accu = 1;
for (int j = i; j < nums.length; j++) {
accu *= nums[j];
result = Math.max(result, accu);
}
}

return result;
}}
35 changes: 35 additions & 0 deletions maximum subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public int maxSubArray(int[] nums) {

int l=nums.length;
int i=0,s=0,max=nums[0];
int[] b = new int[l];

while(i!=l)
{
b[i]=nums[i];
i++;
}
// if(l==1)
// return nums[0];
Arrays.sort(b);
if(b[l-1]<=0)
return b[l-1];

i=0;
while(i!=l)
{
s+=nums[i];
if(s<0)
{
s=0;
}
else if(s>max)
max=s;
System.out.println(max);
i++;
}
System.out.println(max);
return max;
}
}
65 changes: 65 additions & 0 deletions merge k sorted lists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
// Base condition
if (lists == null || lists.length == 0) {
return null;
}
return mergeKLists(lists, 0, lists.length - 1);
}

private ListNode mergeKLists(ListNode[] lists, int start, int end) {
if (start == end) {
return lists[start];
}
// Mid of list of lists
int mid = start + (end - start) / 2;
// Recursive call for left sublist
ListNode left = mergeKLists(lists, start, mid);
// Recursive call for right sublist
ListNode right = mergeKLists(lists, mid + 1, end);
// Merge the left and right sublist
return merge(left, right);
}

private ListNode merge(ListNode left, ListNode right) {
// Create a dummy node
ListNode head = new ListNode(-1);
// Temp node
ListNode temp = head;
// Loop until any of the list becomes null
while (left != null && right != null) {
// Choose the value from the left and right which is smaller
if (left.val < right.val) {
temp.next = left;
left = left.next;
} else {
temp.next = right;
right = right.next;
}
temp = temp.next;
}
// Take all nodes from left list if remaining
while (left != null) {
temp.next = left;
left = left.next;
temp = temp.next;
}
// Take all nodes from right list if remaining
while (right != null) {
temp.next = right;
right = right.next;
temp = temp.next;
}
return head.next;
}
}
15 changes: 15 additions & 0 deletions number of 1 bits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int count = 0;
for(int i=1; i<33; i++){
if(getBit(n, i) == true){
count++;
}
}
return count;
}

public boolean getBit(int n, int i){
return (n & (1 << i)) != 0;
}}
23 changes: 23 additions & 0 deletions remove nth node from end of list.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode fast = head, slow = head;
for (int i = 0; i < n; i++) fast = fast.next;
if (fast == null) return head.next;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
Loading