Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DP Changes #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions challenges/largest-non-adjacent-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
#
# This problem was asked by Airbnb.
#
# Given a list of integers,
# write a function that
# returns the largest sum of non-adjacent numbers.
# Numbers can be 0 or negative.

# For example,
# [2, 4, 6, 2, 5] should return 13,
# since we pick 2, 6, and 5.
# [5, 1, 1, 5] should return 10,
# since we pick 5 and 5.
#
#

import math

def maximumNonAdjacentSum(array):

if (len(array) == 1):
return array[0]

if (len(array) == 2):
return max(array[0] , array[1])

DP = [0]*len(array)
DP[0] = array[0]
DP[1] = max(array[0] , array[1])

for i in range(2,len(array)):

DP[i] = max( array[i] , array[i] + DP[i-2] , DP[i-1])
print(DP)

return DP[len(array) - 1]

# Outputs
maximumNonAdjacentSum([2])
# 2

maximumNonAdjacentSum([2,3])
# 3

print(maximumNonAdjacentSum([2,4,6,2,5]))
# [2, 4, 8, 0, 0]
# [2, 4, 8, 8, 0]
# [2, 4, 8, 8, 13]
# 13

print(maximumNonAdjacentSum([5,1,1,5]))
# [5, 5, 6, 0]
# [5, 5, 6, 10]
# 10
131 changes: 64 additions & 67 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,70 @@

## Challenges

* [#1 Sum Pair In Array](./challenges/sum-pair-in-array.py)
* [#2 Product Array Puzzle](./challenges/product-array-puzzle.py)
* [#3 Serialize And Deserialize Binary Tree](./challenges/serialize-and-deserialize-binary-tree.py)
* [#4 First Missing Positive](./challenges/first-missing-positive.py)
* [#5 Function Implementation](./challenges/function-implementation.py)
* [#6 XOR Linked List](./challenges/xor-linked-list.py)
* [#7 Decode Ways](./challenges/decode-ways.py)
* [#8 Count Unival Subtrees](./challenges/count-unival-subtrees.py)
* #10 Job Scheduler
* [job-scheduler.go](./challenges/job-scheduler.go)
* [job-scheduler.py](./challenges/job-scheduler.py)
* [#11 Autocomplete System](./challenges/autocomplete-system.py)
* [#12 Climb The Staircase](./challenges/climb-the-staircase.py)
* [#14 Estimate Pi - Monte Carlo](./challenges/estimate-pi-monte-carlo.py)
* [#15 Reservoir Sampling](./challenges/reservoir-sampling.py)
* [#16 Order Log](./challenges/order-log.py)
* [#17 Longest Absolute Path](./challenges/longest-absolute-path.py)
* [#20 Intersection In Linked Lists](./challenges/intersection-in-linked-lists.py)
* [#21 Minimum Classrooms Required](./challenges/minimum-classrooms-required.py)
* [#22 Form Sentence From String](./challenges/form-sentence-from-string.py)
* [#23 Shortest Path In A Maze](./challenges/shortest-path-in-a-maze.py)
* [#24 Locking In Binary Tree](./challenges/locking-in-binary-tree.py)
* [#25 Regex Matching](./challenges/regex-matching.py)
* [#26 Kth Last Element In Singly Linked List](./challenges/kth-last-element-in-singly-linked-list.py)
* [#27 Well Formedness Of Brackets](./challenges/well-formedness-of-brackets.py)
* [#28 Justify Text](./challenges/justify-text.py)
* [#29 Run Length Encoding](./challenges/run-length-encoding.py)
* [#36 Second Largest Node In BST](./challenges/second-largest-node-in-bst.py)
* [#42 SubArray Sum](./challenges/subarray-sum.py)
* [#43 Max Stack Implementation](./challenges/max-stack-implementation.py)
* [#52 LRU Cache](./challenges/lru-cache.py)
* [#55 URL Shortener](./challenges/url-shortener.py)
* [#67 LFU Cache](./challenges/lfu-cache.py)
* [#89 Valid Binary Search Tree](./challenges/valid-binary-search-tree.py)
* [#92 Course Scheduler](./challenges/course-scheduler.py)
* [#105 Debounce Function](./challenges/debounce-function.go)
* [#116 Arbitrarily Large Binary Tree](./challenges/arbitrarily-large-binary-tree.py)
* [#121 Make Palidrome](./challenges/make-palindrome.py)
* [#123 Valid Number](./challenges/valid-number.py)
* [#137 Bit Array](./challenges/bit-array.go)
* [#150 K Nearest Points](./challenges/k-nearest-points.py)
* [#157 Is Permutation Palindrome](./challenges/is-permutation-palindrome.py)
* [#163 Reverse Polish Notation](./challenges/reverse-polish-notation.py)
* [#164 Find Duplicate Element](./challenges/find-duplicate-element.py)
* [#165 Smaller Elements On The Right](./challenges/smaller-elements-on-right.py)
* [#166 2D Iterator](./challenges/two-d-iterator.py)
* [#171 Busiest Time In Building](./challenges/busiest-time-in-building.py)
* [#173 Flatten Nested Dictionary](./challenges/flatten-nested-dictionary.py)
* [#176 One To One Character Mapping](./challenges/one-to-one-character-mapping.py)
* [#182 Minimally Connected Graph](./challenges/minimally-connected-graph.py)
* [#183 What Happens When You Type An URL In A Browser](./challenges/what-happens-when-you-type-an-url-in-a-browser.md)
* [#188 Make Functions](./challenges/make-functions.py)
* [#195 Count Elements Given Upper And Lower Bounds](./challenges/count-elements-given-upper-and-lower-bounds.py)
* [#202 Is Number Palindrome](./challenges/is-number-palindrome.py)
* [#215 Bottom View Of Binary Tree](./challenges/bottom-view-of-binary-tree.py)
* [#222 Standardize Absolute Path](./challenges/standardize-absolute-path.py)
* [#233 Fibonacci Using Constant Space](./challenges/fibonacci-using-constant-space.py)
* [#244 Sieve Of Eratosthenes - Prime Generator](./challenges/sieve-of-eratosthenes-prime-generator.py)
* [#246 Chain Words](./challenges/chain-words.py)
* [#273 Fixed Point In Array](./challenges/fixed-point-in-array.py)
* [#282 Pythagorean Triplet In Array](./challenges/pythagorean-triplet-in-array.py)
* [#307 BST Floor And Ceil](./challenges/bst-floor-and-ceil.py)
1. [Sum Pair In Array](./challenges/sum-pair-in-array.py)
2. [Product Array Puzzle](./challenges/product-array-puzzle.py)
3. [Serialize And Deserialize Binary Tree](./challenges/serialize-and-deserialize-binary-tree.py)
4. [First Missing Positive](./challenges/first-missing-positive.py)
5. [Function Implementation](./challenges/function-implementation.py)
6. [XOR Linked List](./challenges/xor-linked-list.py)
7. [Decode Ways](./challenges/decode-ways.py)
8. [Count Unival Subtrees](./challenges/count-unival-subtrees.py)
9. [Largest Non-adjacent Sum](./challenges/largest-non-adjacent-sum.py)
10. Job Scheduler
* [Golang](./challenges/job-scheduler.go)
* [Python](./challenges/job-scheduler.py)
11. [Autocomplete System](./challenges/autocomplete-system.py)
12. [Climb The Staircase](./challenges/climb-the-staircase.py)
13. Longest Subsstring With At Most K Characters
14. [Estimate Pi - Monte Carlo](./challenges/estimate-pi-monte-carlo.py)
15. [Reservoir Sampling](./challenges/reservoir-sampling.py)
16. [Order Log](./challenges/order-log.py)
17. Longest Absolute Path
18. Max In Each Subarray
19. Paint Houses
20. [Intersection In Linked Lists](./challenges/intersection-in-linked-lists.py)
21. [Minimum Classrooms Required](./challenges/minimum-classrooms-required.py)
22. [Form Sentence From String](./challenges/form-sentence-from-string.py)
23. [Shortest Path In A Maze](./challenges/shortest-path-in-a-maze.py)
24. [Locking In Binary Tree](./challenges/locking-in-binary-tree.py)
25. [Regex Matching](./challenges/regex-matching.py)
26. [Kth Last Element In Singly Linked List](./challenges/kth-last-element-in-singly-linked-list.py)
27. [Well Formedness Of Brackets](./challenges/well-formedness-of-brackets.py)
28. [Justify Text](./challenges/justify-text.py)
29. [Run Length Encoding](./challenges/run-length-encoding.py)
30. Trapped Rain Water
31. Edit Distance
32. Currency Exchange Arbitrage
33. Running Median
34. Form A Palindrome
35. RGB Array Segregation
36. [Second Largest Node In BST](./challenges/second-largest-node-in-bst.py)
37. Power Set
38. N Queens Puzzle
39. Conway's Game Of Life
40. Non Duplicated Number
41. Flight Itinerary Problem
42. [SubArray Sum](./challenges/subarray-sum.py)
43. Stack Implementation
44. Number Of Inversions
45. rand7
46. Longest Panlindromic Contiguous Substring
47. Maximum Profit In Stock
48. Reconstruct Tree Using Pre-order and In-order Traversals
49. Maximum Sum Of Contiguous Subarray
50. Evaluate Arithmetic Expression Binary Tree
51. Shuffle Cards
52. LRU Cache
53. Queue Using Stacks
54. Sudoku Solver
55. [URL Shortener](./challenges/url-shortener.py)



## Lessons

* [Merge Sort](./lessons/merge-sort.py)
* [Count Nodes in Binary Tree](./lessons/count-nodes-in-binary-tree.py)
* [Deepest Node in Binary Tree](./lessons/deepest-node-in-binary-tree.py)
* [N Queens Puzzle](./lessons/n-queens-puzzle.py)
* [Flight Itinerary Problem](./lessons/flight-itinerary-problem.py)
1. [Merge Sort](./lessons/merge-sort.py)
2. [Count Nodes in Binary Tree](./lessons/count-nodes-in-binary-tree.py)
3. [Deepest Node in Binary Tree](./lessons/deepest-node-in-binary-tree.py)
4. [N Queens Puzzle](./lessons/n-queens-puzzle.py)
5. [Flight Itinerary Problem](./lessons/flight-itinerary-problem.py)