Last active
September 19, 2017 19:07
-
-
Save stevieoj/5017e01a18e3816d1ee8e45d29d7a28c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! Python | |
# github.com/stevieoj | |
# 1. Given an array of N integers, find maximum difference, where the index of the largest number is greater than | |
# the index of smallest number. If not find the next smallest number. If condition cannot be met return -1. | |
# Time complexity: O(n) | |
# Space Complexity: O(1) | |
def max_difference(xs): | |
min_elem = xs[0] | |
max_elem = xs[0] | |
max_diff = -1 | |
for elem in xs[1:]: | |
min_elem = min(elem, min_elem) | |
if elem > max_elem: | |
max_diff = max(max_diff, elem - min_elem) | |
max_elem = elem | |
return max_diff | |
# 2. James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to | |
# meddle with the letter. He changes all the words in the letter into palindromes. | |
# To do this, he follows two rules: | |
# He can reduce the value of a letter, e.g. he can change d to c, but he cannot change c to d. | |
# In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes a. | |
# Once a letter has been changed to a, it can no longer be changed. | |
# Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to | |
# convert a given string into a palindrome. | |
# Time complexity: O(n/2) | |
# Space complexity: O(1) | |
def theLoveLetterMystery(s): | |
out = 0 | |
for i in range(len(s) // 2): | |
# ord method -> finds unicode point | |
out += abs(ord(s[i]) - ord(s[len(s) - i - 1])) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment