diff --git a/c/152-Maximum-Product-Subarray.c b/c/152-Maximum-Product-Subarray.c new file mode 100644 index 000000000..d294228ba --- /dev/null +++ b/c/152-Maximum-Product-Subarray.c @@ -0,0 +1,21 @@ +int maxProduct(int* nums, int numsSize){ + int res = nums[0], curMin = 1, curMax = 1; + + for (int i = 0; i < numsSize; i++) { + int temp = curMax * nums[i]; + curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i]); + curMin = min(min(temp, nums[i] * curMin), nums[i]); + res = max(res, curMax); + } + return res; +} + +// C doesn't have a built-in max function +int max(int a, int b) { + return (a > b) ? a : b; +} + +// C doesn't have a built-in min function +int min(int a, int b) { + return (a < b) ? a : b; +} \ No newline at end of file diff --git a/csharp/153-Find-Minimum-in-Rotated-Sorted-Array.cs b/csharp/153-Find-Minimum-in-Rotated-Sorted-Array.cs new file mode 100644 index 000000000..3afc55e86 --- /dev/null +++ b/csharp/153-Find-Minimum-in-Rotated-Sorted-Array.cs @@ -0,0 +1,18 @@ +public class Solution { + public int FindMin(int[] nums) { + int left = 0, right = nums.Length - 1; + while (left <= right) { + if (nums[left] <= nums[right]) { + return nums[left]; + } + int mid = (left + right) / 2; + if (nums[mid] >= nums[left]) { + left = mid + 1; + } + else { + right = mid; + } + } + return 0; + } +} \ No newline at end of file diff --git a/csharp/309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs b/csharp/309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs new file mode 100644 index 000000000..02973c1f8 --- /dev/null +++ b/csharp/309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs @@ -0,0 +1,13 @@ +public class Solution { + public int MaxProfit(int[] prices) { + int sold = 0, rest = 0, hold = Int32.MinValue; + + for (int i = 0; i < prices.Length; i++) { + int prevSold = sold; + sold = hold + prices[i]; + hold = Math.Max(hold, rest - prices[i]); + rest = Math.Max(rest, prevSold); + } + return Math.Max(sold, rest); + } +} \ No newline at end of file diff --git a/go/152-Maximum-Product-Subarray.go b/go/152-Maximum-Product-Subarray.go new file mode 100644 index 000000000..b8a6a77fa --- /dev/null +++ b/go/152-Maximum-Product-Subarray.go @@ -0,0 +1,27 @@ +func maxProduct(nums []int) int { + res, curMin, curMax := nums[0], 1, 1 + + for i := 0; i < len(nums); i++ { + temp := curMax * nums[i] + curMax = max(max(nums[i] * curMax, nums[i] * curMin), nums[i]) + curMin = min(min(temp, nums[i] * curMin), nums[i]) + res = max(res, curMax) + } + return res +} + +// Golang does not have a built-in max for integers +func max(a int, b int) int { + if (a > b) { + return a + } + return b +} + +// Golang does not have a built-in min for integers +func min(a int, b int) int { + if (a < b) { + return a + } + return b +} \ No newline at end of file diff --git a/go/66-Plus-One.go b/go/66-Plus-One.go new file mode 100644 index 000000000..4e2849149 --- /dev/null +++ b/go/66-Plus-One.go @@ -0,0 +1,12 @@ +func plusOne(digits []int) []int { + for i := len(digits) - 1; i >= 0; i-- { + if digits[i] < 9 { + digits[i] += 1 + return digits + } + digits[i] = 0 + } + digits[0] = 1 + digits = append(digits, 0) + return digits +} \ No newline at end of file diff --git a/kotlin/152-Maximum-Product-Subarray.kt b/kotlin/152-Maximum-Product-Subarray.kt new file mode 100644 index 000000000..db1ecf005 --- /dev/null +++ b/kotlin/152-Maximum-Product-Subarray.kt @@ -0,0 +1,15 @@ +class Solution { + fun maxProduct(nums: IntArray): Int { + var res = nums[0] + var curMin = 1 + var curMax = 1 + + for (n in nums) { + val temp = curMax * n + curMax = maxOf(n * curMax, n * curMin, n) + curMin = minOf(temp, n * curMin, n) + res = maxOf(res, curMax) + } + return res + } +} \ No newline at end of file diff --git a/swift/141-Linked-List-Cycle.swift b/swift/141-Linked-List-Cycle.swift new file mode 100644 index 000000000..4a407eea6 --- /dev/null +++ b/swift/141-Linked-List-Cycle.swift @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init(_ val: Int) { + * self.val = val + * self.next = nil + * } + * } + */ + +class Solution { + func hasCycle(_ head: ListNode?) -> Bool { + if head === nil { + return false + } + + var slow = head, fast = head + + while fast != nil && fast?.next != nil { + slow = slow?.next + fast = fast?.next?.next + + if fast === slow { + return true + } + } + return false + } +} \ No newline at end of file diff --git a/swift/152-Maximum-Product-Subarray.swift b/swift/152-Maximum-Product-Subarray.swift new file mode 100644 index 000000000..7ef3132d5 --- /dev/null +++ b/swift/152-Maximum-Product-Subarray.swift @@ -0,0 +1,15 @@ +class Solution { + func maxProduct(_ nums: [Int]) -> Int { + var res = nums[0] + var curMin = 1 + var curMax = 1 + + for n in nums { + let temp = curMax * n + curMax = max(n * curMax, n * curMin, n) + curMin = min(temp, n * curMin, n) + res = max(res, curMax) + } + return res + } +} \ No newline at end of file diff --git a/swift/19-Remove-Nth-Node-From-End-of-List.swift b/swift/19-Remove-Nth-Node-From-End-of-List.swift new file mode 100644 index 000000000..e065cbb99 --- /dev/null +++ b/swift/19-Remove-Nth-Node-From-End-of-List.swift @@ -0,0 +1,31 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func removeNthFromEnd(_ head: ListNode?, _ n: Int) -> ListNode? { + if head === nil { + return head + } + var slow = head, fast = head + for i in 1...n + 1 { + if (slow === nil) { + return head?.next + } + slow = slow?.next + } + + while slow != nil { + slow = slow?.next + fast = fast?.next + } + fast?.next = fast?.next?.next + return head + } +} \ No newline at end of file diff --git a/swift/206-Reverse-Linked-List.swift b/swift/206-Reverse-Linked-List.swift new file mode 100644 index 000000000..cfbb6850c --- /dev/null +++ b/swift/206-Reverse-Linked-List.swift @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init() { self.val = 0; self.next = nil; } + * public init(_ val: Int) { self.val = val; self.next = nil; } + * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } + * } + */ +class Solution { + func reverseList(_ head: ListNode?) -> ListNode? { + if (head === nil || head?.next === nil) { + return head + } + + var prev: ListNode? = nil + var curr = head, next = curr?.next + + while curr != nil { + next = curr?.next + curr?.next = prev + prev = curr + curr = next + } + return prev + } +} \ No newline at end of file diff --git a/swift/213-House-Robber-II.swift b/swift/213-House-Robber-II.swift new file mode 100644 index 000000000..629389d0f --- /dev/null +++ b/swift/213-House-Robber-II.swift @@ -0,0 +1,27 @@ +class Solution { + func rob(_ nums: [Int]) -> Int { + let size = nums.count + + if size == 1 { + return nums[0] + } + + let range1 = robber(nums, 0, size - 2) + let range2 = robber(nums, 1, size - 1) + + return max(range1, range2) + } + + func robber(_ nums: [Int], _ start: Int, _ end: Int) -> Int { + var prev = 0 + var curr = 0 + var next = 0 + + for i in start...end { + next = max(prev + nums[i], curr) + prev = curr + curr = next + } + return curr + } +} \ No newline at end of file diff --git a/swift/57-Insert-Interval.swift b/swift/57-Insert-Interval.swift new file mode 100644 index 000000000..2e6351298 --- /dev/null +++ b/swift/57-Insert-Interval.swift @@ -0,0 +1,28 @@ +class Solution { + func insert(_ intervals: [[Int]], _ newInterval: [Int]) -> [[Int]] { + var res : [[Int]] = [] + var i = 0 + let n = intervals.count + + // parameters are immutable in Swift + var currInterval = newInterval + + while i < n && intervals[i][1] < newInterval[0] { + res.append(intervals[i]) + i += 1 + } + + while i < n && intervals[i][0] <= currInterval[1] { + currInterval[0] = min(currInterval[0], intervals[i][0]) + currInterval[1] = max(currInterval[1], intervals[i][1]) + i += 1 + } + res.append(currInterval) + + while i < n { + res.append(intervals[i]) + i += 1 + } + return res + } +} \ No newline at end of file diff --git a/swift/72-Edit-Distance.swift b/swift/72-Edit-Distance.swift new file mode 100644 index 000000000..5dd2b87f9 --- /dev/null +++ b/swift/72-Edit-Distance.swift @@ -0,0 +1,45 @@ +class Solution { + func minDistance(_ word1: String, _ word2: String) -> Int { + // No operations needed if strings match + if word1 == word2 { + return 0 + } + + let m = word1.count, n = word2.count + + // if one word has no characters, min operations is length of other word + if m == 0 { + return n + } + if n == 0 { + return m + } + + var prev = 0 + var curr = Array(repeating: 0, count: n + 1) + + // convert the strings into an array + let newWord1 = Array(word1) + let newWord2 = Array(word2) + + for j in 1...n { + curr[j] = j + } + + for i in 1...m { + var prev = curr[0] + curr[0] = i + for j in 1...n { + let temp = curr[j] + if newWord1[i - 1] == newWord2[j - 1] { + curr[j] = prev + } + else { + curr[j] = min(prev, min(curr[j - 1], curr[j])) + 1 + } + prev = temp + } + } + return curr[n] + } +} \ No newline at end of file