forked from gitgik/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
``` |