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

c/152-Maximum-Product-Subarray.c

Lines changed: 21 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 13 additions & 0 deletions
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+
}

go/152-Maximum-Product-Subarray.go

Lines changed: 27 additions & 0 deletions
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+
}

go/66-Plus-One.go

Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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+
}

swift/141-Linked-List-Cycle.swift

Lines changed: 31 additions & 0 deletions
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+
}
Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 31 additions & 0 deletions
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+
}

swift/206-Reverse-Linked-List.swift

Lines changed: 28 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)