Problem#9 : Non-Adjacent Sum

2020. 8. 16. 00:53PS/도전

All problems are from https://www.dailycodingproblem.com/

 

Solution Codes : https://github.com/Kusin/DailyCoding

 

Kusin/DailyCoding

DailyCodingProblem. Contribute to Kusin/DailyCoding development by creating an account on GitHub.

github.com

Daily Coding Problem #9: Non-Adjacent Sum

Problem Description

Idea

Simple DP Problem.

 

We will set two different DP Table.

DP1 (i) : we seek 1st ~ ith elements. if we include ith element, what would be the max non-adjacent sum?

DP2 (i) : we seek 1st ~ ith elements. if we don't include ith element, what would be the max non-adjacent sum?

 

DP1(i) = DP2(i-1)+arr[i]

DP2(i) = max(DP1(i-1),DP2(i-1))

 

DP1[0] = arr[0]

DP2[0] = - INF

Implementation

Very Simple.

 

 

728x90

'PS > 도전' 카테고리의 다른 글

P#11 String Auto Complete System (문자열 자동완성기능 구현)  (0) 2020.08.16
Problem#10 : Time Lapse  (0) 2020.08.16
Problem#8 :Unival Subtrees  (0) 2020.08.16
Problem#7 : Decoding Possibility  (0) 2020.08.15
Problem #6 XOR Linked List  (0) 2020.08.15