Giter Site home page Giter Site logo

algoexpert_ts's People

Contributors

joshmomel avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

syedanas242

algoexpert_ts's Issues

Thoughts on question `nonConstructibleChange`

Task

As shown below, we need to find out the non-Constructible Change from giving list of coins
Screen Shot 2021-04-18 at 10 04 25 pm

How to approach?

1. 🐨 Sort

It makes sense to sort an array when we deal with array questions.

start from short array

let's consider a single array [number] (number from 1 to infinity)
In this case, it will return 1 or number + 1

let's consider array [1,2]
It can generate:
[1] -> 1
[1, 2] -> 1, 2, 3

2. 🐨 Let's consider array [1,1,2,3,5]

from to range
[1] 1 [1]
[1, 1] 1, 2 [1, 2]
[1, 1, 2] 1, 2, 2+1, 2+2 [1, 4]
[1, 1, 2, 5] 1, 2, 3, 4, ..., 5, 5+1, 5+2, .. , 9(5+4) [1, 9]
[1, 1, 2, 5, 7] 1, 2, 3, 4, ..., 7, 7+1, ..., 16(7+9) [1, 16]
[1, 1, 2, 5, 7, 22] 1, 2, 3, 4, ..., 16, 22, 22+1 πŸ€” Wait! 17 is missing [1, 9]

3. πŸ’°Findings

  1. we sum up list from smallest [1, ..., sum]
  2. the last element in the list shouldn't larger than the sum + 1

Thoughts on question removeDuplicatesFromLinkedList

Task πŸ—’οΈ

This task is to remove the duplicate node in the Linked list
image

How to approach? πŸ”’

πŸ’° In general, most tasks related to Linked list are about altering node.next.

In this task, we also need to come up with an approach to alter node.next, which ensures node.value NOT equal to node.next.vale

Example

1 -> 1 -> 1 -> 2 -> 3 => 1 -> 3

So, it's not hard to think that we need an loop

while(...) {
  if(!valueDuplicated) {
	...
  } else {
	`alter node.next`
  }
}

The trick

πŸ’¬ We can look ahead

But how?

Instead of checking linkedlist is null as the while loop condition like below

  while (currentNode) {
	let nextNode = currentNode.next
    while(currentNode.value === nextNode.value) {
		nextNode = currentNode.next
  	}
	currentNode.next = nextNode
 	currentNode = nextNode
  }

We can check the next node like below

  const list: Array<number> = [linkedList.value];
  let pointer = linkedList

  while (pointer.next) {
    const v = pointer.next.value;
	...
    pointer.next = pointer.next.next

  }

Summary

In summary, by looking ahead, pointer only move to the right place instead moving one by one. However, both approach have same complexity

Thoughts on question Permutations

Task πŸ“–

Get the array of all permutations. i.e.
[1,2,3] => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Screen Shot 2021-04-24 at 3 06 30 pm

How to approach? πŸ”’

**Let's start from some simple cases **
0 numbers [] => [] total = 0
1 numbers [1] => [1] total = 1
2 numbers [1,2] => [1,2] [2,1] total = 2 * 1 = 2
3 numbers [1,2,3] => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] total = 3 * 2
n numbers [1,2,3...n] => [...] total = n * total(n-1)

details
[1,2] can be seen as 2 insert into all positions of [1] (in the front and back) => [1,2] [2,1]
[1,2,3] can be seen as 3 insert into all positions of the Permutations [1,2], which includes
- 3 insert into all positions of [1,2] => [3,1,2],[1,3,2],[1,2,3]
- 3 insert into all positions of [2,1] => [3,2,1],[2,3,1],[2,1,3]

Coding

As discussed above, we can build the permutation from [].

let's assumed that we need to find the permutation of [1,2,3..n]

Sudo code can be

for eachNumber in n:
	insert eachNumber into all positions of Permutation of [1..n-1]	

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.