forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhiteHyun.swift
More file actions
38 lines (31 loc) ยท 852 Bytes
/
WhiteHyun.swift
File metadata and controls
38 lines (31 loc) ยท 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// 152. Maximum Product Subarray
// https://leetcode.com/problems/maximum-product-subarray/description/
// Dale-Study
//
// Created by WhiteHyun on 2024/07/14.
//
class Solution {
func maxProduct(_ nums: [Int]) -> Int {
var maxNumber = nums[0]
var minNumber = nums[0]
var answer = nums[0]
// Problem 190 / 191 ๋ฐฉ์ด ์ฝ๋
let negativeCount = nums.filter { $0 < 0 }.count
for index in 1 ..< nums.count {
let current = nums[index]
let tempMin = minNumber
let tempMax = maxNumber
maxNumber = max(current, tempMin * current, tempMax * current)
if negativeCount > 1 {
minNumber = min(current, tempMin * current, tempMax * current)
} else {
minNumber = 0
}
if answer < maxNumber {
answer = maxNumber
}
}
return answer
}
}