diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 1e277c1..3d46f90 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,7 @@ package main.kotlin +import main.kotlin.dsalgoleetcode.BuyAndSellStock +import main.kotlin.dsalgoleetcode.ContainsDuplicate import main.kotlin.dsalgoleetcode.LinkedListCycleDetection import main.kotlin.dsalgoleetcode.ListNode import main.kotlin.dsalgoleetcode.LongestSubstring @@ -30,4 +32,16 @@ fun main() { node3.next = node1 // cycle back to x val llcdResult = linkedListCycleDetection.hasCycle(node1) println("Has cycle $llcdResult") + + // Calling Contains Duplicate + val containsDuplicate = ContainsDuplicate() + val containsDuplicateResult = containsDuplicate + .containsDuplicate(intArrayOf(1,2,3,1,0,1)) + println("Contains Duplicate Integers $containsDuplicateResult") + + // Calling Buy and Sell Stock + val buyAndSellStock = BuyAndSellStock() + val bnsResult = buyAndSellStock.maxProfit(intArrayOf(1,4,8,0,2)) + println("Max profit : $bnsResult") + } \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt b/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt new file mode 100644 index 0000000..385576b --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt @@ -0,0 +1,33 @@ +package main.kotlin.dsalgoleetcode + +/** + * You are given an array prices where prices[i] is the price of a given stock on the ith day. + * You want to maximize your profit by choosing a single day to buy one stock and choosing a + * different day in the future to sell that stock. + * Return the maximum profit you can achieve from this transaction. + * If you cannot achieve any profit, return 0. +Example 1: + +Input: prices = [7,1,5,3,6,4] +Output: 5 +Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. +Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. + * */ + +class BuyAndSellStock { + + fun maxProfit(prices: IntArray): Int { + + var minPrice = Int.MAX_VALUE + var maxProfit = 0 + + for (price in prices){ + if( price < minPrice){ + minPrice = price + } else{ + maxProfit = maxOf(maxProfit, price - minPrice) + } + } + return maxProfit + } +} \ No newline at end of file diff --git a/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt new file mode 100644 index 0000000..4e35c07 --- /dev/null +++ b/src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt @@ -0,0 +1,27 @@ +package main.kotlin.dsalgoleetcode + +/** + * Contains Duplicate + * Given an integer array nums, return true if any value appears at least twice in the + * array, and return false if every element is distinct. + * + * Example 1: + * Input: nums = [1,2,3,1] + * Output: true + * Explanation: + * The element 1 occurs at the indices 0 and 3. + * */ + +class ContainsDuplicate { + + fun containsDuplicate(nums: IntArray): Boolean { + + val seen = mutableSetOf() + for(i in nums){ + if( i in seen) + return true + seen.add(i) + } + return false + } +} \ No newline at end of file