Skip to content

Commit 50f95be

Browse files
author
EternalChildren
committed
increase four solvtions
1 parent 752bcb3 commit 50f95be

File tree

11 files changed

+146
-0
lines changed

11 files changed

+146
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
2+
3+
let num = 4
4+
5+
// 264ms
6+
func judgeSquareSum(_ c: Int) -> Bool {
7+
var hash = [Int: Int]()
8+
var i = 0
9+
var s = 0
10+
while s <= c {
11+
if s*2 == c || hash[c - s] != nil {
12+
return true
13+
} else {
14+
hash[s] = 1
15+
i += 1
16+
s = i*i
17+
}
18+
}
19+
return false
20+
}
21+
22+
judgeSquareSum(num)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amounts can be different for different buildings). Height 0 is considered to be a building as well.
2+
3+
// At the end, the "skyline" when viewed from all four directions of the grid, i.e. top, bottom, left, and right, must be the same as the skyline of the original grid. A city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. See the following example.
4+
5+
// What is the maximum total sum that the height of the buildings can be increased?
6+
7+
let nums = [[3,0,8,4],[2,4,5,7],[9,2,6,3],[0,3,1,0]]
8+
9+
// 56ms
10+
func maxIncreaseKeepingSkyline(_ grid: [[Int]]) -> Int {
11+
var tb = [Int]()
12+
var lr = [Int]()
13+
for i in grid {
14+
lr.append(i.max()!)
15+
if tb.count > 0 {
16+
for index in 0..<i.count {
17+
if i[index] > tb[index] {
18+
tb[index] = i[index]
19+
}
20+
}
21+
}else{
22+
tb = i
23+
}
24+
}
25+
var res = 0
26+
for i in 0..<grid.count {
27+
for j in 0..<grid[i].count {
28+
let max = lr[i] <= tb[j] ? lr[i] : tb[j]
29+
res += max - grid[i][j]
30+
}
31+
}
32+
return res
33+
}
34+
35+
36+
maxIncreaseKeepingSkyline(nums)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Let's call an array A a mountain if the following properties hold:
2+
3+
// A.length >= 3
4+
// There exists some 0 < i < A.length - 1 such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]
5+
// Given an array that is definitely a mountain, return any i such that A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1].
6+
7+
8+
let nums = [0,2,1,0]
9+
10+
11+
// 96ms find the index for max
12+
func peakIndexInMountainArray(_ A: [Int]) -> Int {
13+
return A.firstIndex(of: A.max()!)!
14+
}
15+
16+
peakIndexInMountainArray(nums)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Given two integers A and B, return any string S such that:
2+
3+
// S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
4+
// The substring 'aaa' does not occur in S;
5+
// The substring 'bbb' does not occur in S.
6+
7+
8+
let A = 4
9+
let B = 8
10+
// 20ms
11+
func strWithout3a3b(_ A: Int, _ B: Int) -> String {
12+
var str = ""
13+
var A = A
14+
var B = B
15+
16+
while A > 0 || B > 0 {
17+
var writeA = false
18+
let count = str.count
19+
if str.count >= 2 && str[str.index(before: str.endIndex)] == str[str.index(str.endIndex, offsetBy: -2)] {
20+
if str[str.index(before: str.endIndex)] == "b" {
21+
writeA = true
22+
}
23+
}else{
24+
if A > B {
25+
writeA = true
26+
}
27+
}
28+
29+
if writeA {
30+
A -= 1
31+
str.append("a")
32+
} else {
33+
B -= 1
34+
str.append("b")
35+
}
36+
}
37+
return str
38+
}
39+
40+
strWithout3a3b(A, B)
41+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

strWithout3a3b/strWithout3a3b.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

0 commit comments

Comments
 (0)