Skip to content

Commit

Permalink
added some
Browse files Browse the repository at this point in the history
  • Loading branch information
aelam committed Mar 15, 2021
1 parent 477856a commit 675cc38
Show file tree
Hide file tree
Showing 19 changed files with 827 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//: [Previous](@previous)

/*: #
[1. Two Sum](http://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/)

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

```
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
```
*/

class HashSolution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var cached = Dictionary<Int, Int>()
for i in 0..<nums.count {
let reminding = target - nums[i]
let index = cached[reminding]
if let a = index {
return [a, i]
} else {
cached[nums[i]] = i
}
}

return []

}
}

class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
for i in 0..<nums.count-1 {
let reminding = target - nums[i]
for j in i+1..<nums.count {
if nums[j] == reminding {
return [i, j]
}
}
}

return []
}
}


let nums = [2, 7, 11, 15]
let target = 9
let solution = Solution()
print(solution.twoSum(nums, target))


//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//: [Previous](@previous)

/*: [12. Integer to Roman](https://leetcode-cn.com/problems/integer-to-roman/)

Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.

```
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
```
For example, 2 is written as `II` in Roman numeral, just two one's added together. 12 is written as `XII`, which is simply `X + II`. The number 27 is written as XXVII, which is `XX + V + II`.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before `V` (5) and `X` (10) to make 4 and 9.
X can be placed before `L` (50) and `C` (100) to make 40 and 90.
C can be placed before `D` (500) and `M` (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.


```
Example 1:

Input: num = 3
Output: "III"
```

```
Example 2:

Input: num = 4
Output: "IV"
```

```
Example 3:

Input: num = 9
Output: "IX"
```

```
Example 4:

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
```

```
Example 5:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

```

Constraints:

* 1 <= num <= 3999
*/



class Solution {
func intToRoman(_ num: Int) -> String {
var result = ""
let digits = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
let romas = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]

var temp = num

while (temp > 0) {
for i in 0..<digits.count {
let v = digits[i]
while temp >= v {
result += romas[i]
temp -= v
}
}
}

return result
}
}

let s = Solution()

let num = 1994
//"MCMXCIV"

s.intToRoman(num)


//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//: [Previous](@previous)


/*:
[322. Coin Change](https://leetcode-cn.com/problems/coin-change/)

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return `-1`.

You may assume that you have an infinite number of each kind of coin.


```
Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1
```

```
Example 2:

Input: coins = [2], amount = 3
Output: -1
```

```
Example 3:

Input: coins = [1], amount = 0
Output: 0
```

```
Example 4:

Input: coins = [1], amount = 1
Output: 1
```

```
Example 5:

Input: coins = [1], amount = 2
Output: 2

```

Constraints:

* 1 <= coins.length <= 12
* 1 <= coins[i] <= 231 - 1
* 0 <= amount <= 104



// f(n) = min(f(n - c1), f(n - c2), ... f(n - cn)) + 1
*/

class Solution {
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
let maxValue = Int.max - amount
var dp = [Int].init(repeating: maxValue, count: amount + 1)

dp[0] = 0

for i in 1..<(amount+1) {
var cost = dp[i]
for c in coins {
if i >= c {
cost = min(cost, dp[i - c] + 1)
}
}
dp[i] = cost
print("\(i): \(dp[i])")
}

if dp[amount] == maxValue {
return -1
}

return dp[amount]
}
}

let s = Solution()
//let coins = [1,2,5]
//let amount = 11
//
//s.coinChange(coins, amount)

// 0 1 2 3 4 5 6 7 8 9 10 11
// 0: 0
// 1: dp[0] + 1 = 1
// 2: min(dp[1] + 1) = 2
// 3: min(dp[2] + 1) = 3
// 4: min(dp[3] + 1) = 4
// 5: min(dp[4] + 1, dp[0] + 1) = 1
//

let coins = [2]
let amount = 3

s.coinChange(coins, amount)


//: [Next](@next)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//: [Previous](@previous)

/*:[33. Search in Rotated Sorted Array](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/description/)

There is an integer array `nums` sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is rotated at an unknown pivot index `k` (0 <= k < nums.length) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`.

Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or `-1` if it is not in nums.


```
Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
```
```
Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

```

```
Example 3:

Input: nums = [1], target = 0
Output: -1
 ```

Constraints:

* 1 <= nums.length <= 5000
* -104 <= nums[i] <= 104
* All values of nums are unique.
* nums is guaranteed to be rotated at some pivot.
* -104 <= target <= 104


*/

class Solution {
func search(_ nums: [Int], _ target: Int) -> Int {
return search(nums, 0, nums.count - 1, target)
}

func search(_ nums:[Int], _ start: Int, _ end: Int, _ target: Int) -> Int {
print("start:\(start), end:\(end)")
if start > end {
return -1
}

let mid = ( start + end ) / 2

if nums[mid] == target {
return mid
}

if nums[start] <= target || nums[mid - 1] <= target {
let ss = search(nums, start, mid - 1, target)
if ss != -1 {
return ss
}
} else if nums[mid + 1] <= target || nums[end] <= target {
let ss = search(nums, mid + 1, end, target)
if ss != -1 {
return ss
}
}
return -1
}
}


//Input: nums = [4,5,6,7,0,1,2], target = 0
//Output: 4

//let nums = [4,5,6,7,0,1,2], target = 0
let nums = [1], target = 0

let s = Solution()
let r = s.search(nums, target)
print(r)

//: [Next](@next)
Loading

0 comments on commit 675cc38

Please sign in to comment.