-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
LeetCodePlayground.playground/Pages/343_IntegerBreak.xcplaygroundpage/Contents.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
class Solution { | ||
func integerBreak(_ n: Int) -> Int { | ||
if n <= 3 { | ||
return n - 1 | ||
} | ||
|
||
let numCount = n / 3 | ||
let num = n % 3 | ||
|
||
if num == 0 { | ||
return Int(pow(3.0, Double(numCount))) | ||
} else if num == 1 { | ||
return Int(pow(3.0, Double(numCount - 1))) * 4 | ||
} else { | ||
return Int(pow(3.0, Double(numCount))) * 2 | ||
} | ||
|
||
} | ||
} | ||
|
||
// Tests | ||
let s = Solution() | ||
s.integerBreak(2) == 1 | ||
s.integerBreak(10) == 36 | ||
|
||
//: [Next](@next) |
This file contains 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