Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1142 from Ykhan799/main
Browse files Browse the repository at this point in the history
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
Ahmad-A0 authored Sep 24, 2022
2 parents 16c3057 + eb434ab commit 7f95db3
Show file tree
Hide file tree
Showing 13 changed files with 311 additions and 0 deletions.
21 changes: 21 additions & 0 deletions c/152-Maximum-Product-Subarray.c
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;
}
18 changes: 18 additions & 0 deletions csharp/153-Find-Minimum-in-Rotated-Sorted-Array.cs
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 csharp/309-Best-Time-to-Buy-and-Sell-Stock-With-Cooldown.cs
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);
}
}
27 changes: 27 additions & 0 deletions go/152-Maximum-Product-Subarray.go
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
}
12 changes: 12 additions & 0 deletions go/66-Plus-One.go
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
}
15 changes: 15 additions & 0 deletions kotlin/152-Maximum-Product-Subarray.kt
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
}
}
31 changes: 31 additions & 0 deletions swift/141-Linked-List-Cycle.swift
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
}
}
15 changes: 15 additions & 0 deletions swift/152-Maximum-Product-Subarray.swift
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
}
}
31 changes: 31 additions & 0 deletions swift/19-Remove-Nth-Node-From-End-of-List.swift
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
}
}
28 changes: 28 additions & 0 deletions swift/206-Reverse-Linked-List.swift
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
}
}
27 changes: 27 additions & 0 deletions swift/213-House-Robber-II.swift
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
}
}
28 changes: 28 additions & 0 deletions swift/57-Insert-Interval.swift
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
}
}
45 changes: 45 additions & 0 deletions swift/72-Edit-Distance.swift
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]
}
}

0 comments on commit 7f95db3

Please sign in to comment.