Subscribed unsubscribe Subscribe Subscribe

Takefumi Yamamura's blog

This blog is for my memorandum.

Takefumi Yamamura's b!og

This blog is for my memorandum

Group Anagrams

problem

Write a method to sort an array of strings so that all the anagrams are next to each other.

how to solve

I use hash table. once we have grouped all the words into these lists by anagram. we can then put them back into the array.

code

Remove all ads

Successor

prblme

Write an algorithm to find the “next” node(i.e., in-order successor) of a given node in a binary search tree. (You may assume that each node has a link to its parent or you can get the root node)

how to solve

“Next” node means that the most nearest parent node which is right side of a given node. So we should find it.

code 1

When I can get the root node

code 2

When I get the parent node of each node

Remove all ads

Contains Duplicate III

problem

Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.

how to solve

To solve this problem, I use set(binary search tree). In set, the value is sorted automatically and I can insert and delete in O(logn). We can check the most near higher element and the most near lower element in O(logn) using lower_bound. So we can get answer in O(nlogn).

code

Validate BST

problem

Implement a function to check if a binary tree is a binary search tree.

how to solve

Binary search tree means that all left nodes must be less than or equal to the current node and all right nodes must be more than or equal to the current node. So we have to check this condition to preserve the min and max value. When we branch left , the max gets updated. When we branch right, the min gets updated. If anything fails these checks, we stop and return false.

code

Remove all ads

Check Balanced

problem

Implement a function to check if a binary tree is balanced.

how to solve

A balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one. So I implement a function fetchHeight to get the heights of subtrees. To use a past result of fetchHeight function, I used hash table. If I calculate the height once before. I can get this value in O(1). To calculate the all of the value of fetch Height function, I firstly traverse all nodes in O(n). Next, I can check the all subtree to meet the definition of a balanced tree to answer this question.

code

Remove all ads

Intersection

problem

Write a program to find the node at which the intersection of two singly linked lists begins. - If the two linked lists have no intersection at all, return null.

  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

how to solve

Firstly, I calculate the length of two linked list and advance the pointer by the difference in lengths. Secondly, traverse on each linked list until the pointers are the same.

code

Remove all ads

Loop Detection

problem

Given a circular linked list, implement an algorithm that returns the node at the beginning of the loop.

how to solve

If I can use extra space, I can solve this very easily. I can justly use a hash table to detect if the element is appeared or not.

Next, I want to consider this problem without using that data structure. I took the following steps.

  • Create two pointers, FastPointer and SlowPointer.
  • Move fastPointer at a rate of 2 steps and SlowPointer at a rate of 1 step.
  • When they collide, move SlowPointer to LinkedListHead. Keep FastPointer where it is.
  • Move Slow Pointer and FastPointer at a rate of one step. Return the new collision point

code1

without using extra space.

code2(with using map)

with using hash map.

Remove all ads