Skip to content

Commit 7f95db3

Browse files
authored
Merge pull request #1142 from Ykhan799/main
Create: 57-Insert-Interval.swift, 141-Linked-List-Cycle.swift, 206-Reverse-Linked-List.swift, 213-House-Robber-II.swift, 19-Remove-Nth-Node-From-End-of-List.swift, 72-Edit-Distance.swift, 152-Maximum-Product-Subarray.swift, 152-Maximum-Product-Subarray.c, 152-Maximum-Product-Subarray.go, 66-Plus-One.go, 152-Maximum-Product-Subarray.kt, 153-Find-Minimum-in-Rotated-Sorted-Array.cs, 309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs
2 parents 16c3057 + eb434ab commit 7f95db3

13 files changed

+311
-0
lines changed

Diff for: c/152-Maximum-Product-Subarray.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int maxProduct(int* nums, int numsSize){
2+
int res = nums[0], curMin = 1, curMax = 1;
3+
4+
for (int i = 0; i < numsSize; i++) {
5+
int temp = curMax * nums[i];
6+
curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i]);
7+
curMin = min(min(temp, nums[i] * curMin), nums[i]);
8+
res = max(res, curMax);
9+
}
10+
return res;
11+
}
12+
13+
// C doesn't have a built-in max function
14+
int max(int a, int b) {
15+
return (a > b) ? a : b;
16+
}
17+
18+
// C doesn't have a built-in min function
19+
int min(int a, int b) {
20+
return (a < b) ? a : b;
21+
}

Diff for: csharp/153-Find-Minimum-in-Rotated-Sorted-Array.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int FindMin(int[] nums) {
3+
int left = 0, right = nums.Length - 1;
4+
while (left <= right) {
5+
if (nums[left] <= nums[right]) {
6+
return nums[left];
7+
}
8+
int mid = (left + right) / 2;
9+
if (nums[mid] >= nums[left]) {
10+
left = mid + 1;
11+
}
12+
else {
13+
right = mid;
14+
}
15+
}
16+
return 0;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int MaxProfit(int[] prices) {
3+
int sold = 0, rest = 0, hold = Int32.MinValue;
4+
5+
for (int i = 0; i < prices.Length; i++) {
6+
int prevSold = sold;
7+
sold = hold + prices[i];
8+
hold = Math.Max(hold, rest - prices[i]);
9+
rest = Math.Max(rest, prevSold);
10+
}
11+
return Math.Max(sold, rest);
12+
}
13+
}

Diff for: go/152-Maximum-Product-Subarray.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func maxProduct(nums []int) int {
2+
res, curMin, curMax := nums[0], 1, 1
3+
4+
for i := 0; i < len(nums); i++ {
5+
temp := curMax * nums[i]
6+
curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i])
7+
curMin = min(min(temp, nums[i] * curMin), nums[i])
8+
res = max(res, curMax)
9+
}
10+
return res
11+
}
12+
13+
// Golang does not have a built-in max for integers
14+
func max(a int, b int) int {
15+
if (a > b) {
16+
return a
17+
}
18+
return b
19+
}
20+
21+
// Golang does not have a built-in min for integers
22+
func min(a int, b int) int {
23+
if (a < b) {
24+
return a
25+
}
26+
return b
27+
}

Diff for: go/66-Plus-One.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func plusOne(digits []int) []int {
2+
for i := len(digits) - 1; i >= 0; i-- {
3+
if digits[i] < 9 {
4+
digits[i] += 1
5+
return digits
6+
}
7+
digits[i] = 0
8+
}
9+
digits[0] = 1
10+
digits = append(digits, 0)
11+
return digits
12+
}

Diff for: kotlin/152-Maximum-Product-Subarray.kt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
fun maxProduct(nums: IntArray): Int {
3+
var res = nums[0]
4+
var curMin = 1
5+
var curMax = 1
6+
7+
for (n in nums) {
8+
val temp = curMax * n
9+
curMax = maxOf(n * curMax, n * curMin, n)
10+
curMin = minOf(temp, n * curMin, n)
11+
res = maxOf(res, curMax)
12+
}
13+
return res
14+
}
15+
}

Diff for: swift/141-Linked-List-Cycle.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init(_ val: Int) {
7+
* self.val = val
8+
* self.next = nil
9+
* }
10+
* }
11+
*/
12+
13+
class Solution {
14+
func hasCycle(_ head: ListNode?) -> Bool {
15+
if head === nil {
16+
return false
17+
}
18+
19+
var slow = head, fast = head
20+
21+
while fast != nil && fast?.next != nil {
22+
slow = slow?.next
23+
fast = fast?.next?.next
24+
25+
if fast === slow {
26+
return true
27+
}
28+
}
29+
return false
30+
}
31+
}

Diff for: swift/152-Maximum-Product-Subarray.swift

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func maxProduct(_ nums: [Int]) -> Int {
3+
var res = nums[0]
4+
var curMin = 1
5+
var curMax = 1
6+
7+
for n in nums {
8+
let temp = curMax * n
9+
curMax = max(n * curMax, n * curMin, n)
10+
curMin = min(temp, n * curMin, n)
11+
res = max(res, curMax)
12+
}
13+
return res
14+
}
15+
}

Diff for: swift/19-Remove-Nth-Node-From-End-of-List.swift

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? {
13+
if head === nil {
14+
return head
15+
}
16+
var slow = head, fast = head
17+
for i in 1...n + 1 {
18+
if (slow === nil) {
19+
return head?.next
20+
}
21+
slow = slow?.next
22+
}
23+
24+
while slow != nil {
25+
slow = slow?.next
26+
fast = fast?.next
27+
}
28+
fast?.next = fast?.next?.next
29+
return head
30+
}
31+
}

Diff for: swift/206-Reverse-Linked-List.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public var val: Int
5+
* public var next: ListNode?
6+
* public init() { self.val = 0; self.next = nil; }
7+
* public init(_ val: Int) { self.val = val; self.next = nil; }
8+
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
func reverseList(_ head: ListNode?) -> ListNode? {
13+
if (head === nil || head?.next === nil) {
14+
return head
15+
}
16+
17+
var prev: ListNode? = nil
18+
var curr = head, next = curr?.next
19+
20+
while curr != nil {
21+
next = curr?.next
22+
curr?.next = prev
23+
prev = curr
24+
curr = next
25+
}
26+
return prev
27+
}
28+
}

Diff for: swift/213-House-Robber-II.swift

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func rob(_ nums: [Int]) -> Int {
3+
let size = nums.count
4+
5+
if size == 1 {
6+
return nums[0]
7+
}
8+
9+
let range1 = robber(nums, 0, size - 2)
10+
let range2 = robber(nums, 1, size - 1)
11+
12+
return max(range1, range2)
13+
}
14+
15+
func robber(_ nums: [Int], _ start: Int, _ end: Int) -> Int {
16+
var prev = 0
17+
var curr = 0
18+
var next = 0
19+
20+
for i in start...end {
21+
next = max(prev + nums[i], curr)
22+
prev = curr
23+
curr = next
24+
}
25+
return curr
26+
}
27+
}

Diff for: swift/57-Insert-Interval.swift

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
func insert(_ intervals: [[Int]], _ newInterval: [Int]) -> [[Int]] {
3+
var res : [[Int]] = []
4+
var i = 0
5+
let n = intervals.count
6+
7+
// parameters are immutable in Swift
8+
var currInterval = newInterval
9+
10+
while i < n && intervals[i][1] < newInterval[0] {
11+
res.append(intervals[i])
12+
i += 1
13+
}
14+
15+
while i < n && intervals[i][0] <= currInterval[1] {
16+
currInterval[0] = min(currInterval[0], intervals[i][0])
17+
currInterval[1] = max(currInterval[1], intervals[i][1])
18+
i += 1
19+
}
20+
res.append(currInterval)
21+
22+
while i < n {
23+
res.append(intervals[i])
24+
i += 1
25+
}
26+
return res
27+
}
28+
}

Diff for: swift/72-Edit-Distance.swift

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
func minDistance(_ word1: String, _ word2: String) -> Int {
3+
// No operations needed if strings match
4+
if word1 == word2 {
5+
return 0
6+
}
7+
8+
let m = word1.count, n = word2.count
9+
10+
// if one word has no characters, min operations is length of other word
11+
if m == 0 {
12+
return n
13+
}
14+
if n == 0 {
15+
return m
16+
}
17+
18+
var prev = 0
19+
var curr = Array(repeating: 0, count: n + 1)
20+
21+
// convert the strings into an array
22+
let newWord1 = Array(word1)
23+
let newWord2 = Array(word2)
24+
25+
for j in 1...n {
26+
curr[j] = j
27+
}
28+
29+
for i in 1...m {
30+
var prev = curr[0]
31+
curr[0] = i
32+
for j in 1...n {
33+
let temp = curr[j]
34+
if newWord1[i - 1] == newWord2[j - 1] {
35+
curr[j] = prev
36+
}
37+
else {
38+
curr[j] = min(prev, min(curr[j - 1], curr[j])) + 1
39+
}
40+
prev = temp
41+
}
42+
}
43+
return curr[n]
44+
}
45+
}

0 commit comments

Comments
 (0)