forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#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
- Loading branch information
Showing
13 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
csharp/309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} | ||
} |