Skip to content

Commit

Permalink
0259-3sum-smaller
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeizhu committed Aug 20, 2018
1 parent 450f165 commit e1d7eb3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 0259-3sum-smaller.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// 259. 3Sum Smaller
/// Given an array of n integers nums and a target, find the number of index triplets i, j, k
/// with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

import XCTest

/// Approach: Two Pointers
func threeSumSmaller(_ nums: [Int], _ target: Int) -> Int {
guard nums.count > 2 else { return 0 }
let nums = nums.sorted()
var count = 0
for i in 0..<nums.count - 2 {
var j = i + 1
var k = nums.count - 1
while j < k {
if nums[i] + nums[j] + nums[k] < target {
count += k - j
j += 1
} else {
k -= 1
}
}
}
return count
}

class Tests: XCTestCase {
func testExample() {
let nums = [-2, 0, 1, 3]
XCTAssertEqual(threeSumSmaller(nums, 2), 2)
}
}

Tests.defaultTestSuite.run()
4 changes: 4 additions & 0 deletions 0259-3sum-smaller.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<timeline fileName='timeline.xctimeline'/>
</playground>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ You can visit the pages below to search problems by company tags. Then come back
84 | [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0084-largest-rectangle-in-histogram.playground/Contents.swift) | Stack
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0163-missing-ranges.playground/Contents.swift) | Iteration
200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0200-number-of-islands.playground/Contents.swift) | BFS
259 | [Three Sum Smaller](https://leetcode.com/problems/3sum-smaller/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0259-3sum-smaller.playground/Contents.swift) | BFS
482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0482-license-key-formatting.playground/Contents.swift) | String
681 | [Next Closest Time](https://leetcode.com/problems/next-closest-time/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0681-next-closest-time.playground/Contents.swift) | Simulation
683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/description/) | [Solution](https://github.com/zhubofei/LeetCode-Swift/blob/master/0683-k-empty-slots.playground/Contents.swift) | Sliding Window
Expand Down

0 comments on commit e1d7eb3

Please sign in to comment.