Skip to content

Commit

Permalink
add md versions of new notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
gitgik committed Feb 4, 2022
1 parent 39850a5 commit 49b0f8c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
73 changes: 73 additions & 0 deletions queues/cyclic_execution_of_kth_prisoner.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
## Problem

There are N prisoners standing at a circle, waiting to be executed. Executions start with the Kth person, and goes on clockwise, removing every successive Kth person until there's no one left.

Given N and K, determine where the prisoner should stand in order to be the last survivor.

For example, if N = 5, and k = 2, order of executions will be [2, 4, 1, 5, 3] so return 3.



```python
def last_executed(n, k):
# create list of size n
prisoners = [i for i in range(1, n + 1)]
# use zero-based index
i = k - 1

# loop through cyclicly over prisoners list, until last visited prisoner
while (n > 1):
prisoners.pop(i)
i = (i + k - 1) % len(prisoners)
n -= 1
return prisoners[0]
```


```python
last_executed(5, 2)
```




3



Since popping from a random index in a list is O(N) and we are doing this N times, the solution runs in O(N^2).

We can improve the efficiency by using a double-ended queue.

We will rotate our list in O(k) time by using a deque, so that we can pop elements at the end.
This allows us to eliminate each prisoner in O(k) time, thereby making the whole solution run in O(k * N)



```python
from collections import deque

def last_one_standing(n, k):
prisoners = deque(range(1, n + 1))

while (n > 1):
prisoners.rotate(-k)
prisoners.pop()

n -= 1

return prisoners[0]

```


```python
last_one_standing(5, 2)
```




3


35 changes: 35 additions & 0 deletions strings/string_is_shifted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## Problem
Given two strings A and B, return whether or not A can be shifted a number of times to get B.

For example, if `A = cdeab` and `B = abcde` return true. If `A = xyz and B = xzy` return false.

### Solution
First, we should return false if the first and second string differ in length.

We can then concatenate one of the strings to itself, like (A + A) and check if B is in the concatenated string.
If the string is shifted, we will find it in the new string.


```python
def is_shifted(a, b):
if len(a) != len(b):
return False
return b in a + a
```


```python
is_shifted("cdeab", "abcde")
```




True




```python

```

0 comments on commit 49b0f8c

Please sign in to comment.