Skip to content

Commit

Permalink
solve/#168: 프로그래머스 연속 펄스 부분 수열의 합
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-muuu committed Oct 16, 2023
1 parent 6af2fa3 commit 269598b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions AlgorithmReview.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
EEBC48342AD9343C000BBAF1 /* 셔틀버스.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEBC48332AD9343C000BBAF1 /* 셔틀버스.swift */; };
EEC10A8A2A9EE99900882987 /* 입국 심사.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEC10A892A9EE99900882987 /* 입국 심사.swift */; };
EEC5CAF129B0677900FF4C26 /* 로또의 최고 순위와 최저 순위.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEC5CAF029B0677900FF4C26 /* 로또의 최고 순위와 최저 순위.swift */; };
EECBCFEE2ADCFEB90056BC96 /* 연속 펄스 부분 수열의 합.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECBCFED2ADCFEB90056BC96 /* 연속 펄스 부분 수열의 합.swift */; };
EED6FC712A020BE5006F6D72 /* 2493.swift in Sources */ = {isa = PBXBuildFile; fileRef = EED6FC702A020BE5006F6D72 /* 2493.swift */; };
EED6FC772A036698006F6D72 /* 2800.swift in Sources */ = {isa = PBXBuildFile; fileRef = EED6FC762A036698006F6D72 /* 2800.swift */; };
EEEA901A2A89DC6F00EBE008 /* 수식 최대화.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEA90192A89DC6F00EBE008 /* 수식 최대화.swift */; };
Expand Down Expand Up @@ -167,6 +168,7 @@
EEC10A892A9EE99900882987 /* 입국 심사.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "입국 심사.swift"; sourceTree = "<group>"; };
EEC5CAF029B0677900FF4C26 /* 로또의 최고 순위와 최저 순위.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "로또의 최고 순위와 최저 순위.swift"; sourceTree = "<group>"; };
EEC5E3632A6A2CB8007929D6 /* 숫자 카드 나누기.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "숫자 카드 나누기.swift"; sourceTree = "<group>"; };
EECBCFED2ADCFEB90056BC96 /* 연속 펄스 부분 수열의 합.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "연속 펄스 부분 수열의 합.swift"; sourceTree = "<group>"; };
EECCE33829B9A12E00838047 /* 키패드 누르기.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "키패드 누르기.swift"; sourceTree = "<group>"; };
EECCE33F29BADE8500838047 /* 크레인 인형뽑기 게임.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "크레인 인형뽑기 게임.swift"; sourceTree = "<group>"; };
EECEB2632A6F8BC300FBD5D8 /* 테이블 해시 함수.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "테이블 해시 함수.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -449,6 +451,7 @@
EE31346F2AD51D670092C902 /* 징검다리 건너기.swift */,
EE8F05042AD6695900B70351 /* 경주로 건설.swift */,
EEBC48332AD9343C000BBAF1 /* 셔틀버스.swift */,
EECBCFED2ADCFEB90056BC96 /* 연속 펄스 부분 수열의 합.swift */,
);
path = Lv3;
sourceTree = "<group>";
Expand Down Expand Up @@ -548,6 +551,7 @@
EEAEC89F298CACDD001B1D62 /* main.swift in Sources */,
EE5CEEC62A39A6BB00E1907E /* 가장 큰 정사각형 찾기.swift in Sources */,
EE2738C429C193A3006077A3 /* 대충 만든 자판.swift in Sources */,
EECBCFEE2ADCFEB90056BC96 /* 연속 펄스 부분 수열의 합.swift in Sources */,
EE2E5E6C29B5884D00545253 /* 체육복.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// 연속 펄스 부분 수열의 합.swift
// AlgorithmReview
//
// Created by 박소윤 on 2023/10/16.
//

import Foundation

func solution(_ sequence:[Int]) -> Int {

let first = sequence.first!
var ans = max(first, -first)

func check(_ remain: Int) {

var dp = [Int](repeating: 0, count: sequence.count)
dp[0] = 0 == remain ? -sequence[0] : sequence[0]

for idx in (1..<sequence.count) {
let value = idx % 2 == remain ? -sequence[idx] : sequence[idx]
dp[idx] = max(dp[idx-1], 0) + value
ans = max(ans, dp[idx])
}
}

check(0)
check(1)

return ans
}

0 comments on commit 269598b

Please sign in to comment.