Skip to content

Max profit on Buy and Sell stock #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,9 +34,14 @@ fun main() {
println("Has cycle $llcdResult")

// Calling Contains Duplicate
val `containsDuplicate.kt` = `ContainsDuplicate.kt`()
val containsDuplicateResult = `containsDuplicate.kt`
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")

}
33 changes: 33 additions & 0 deletions src/main/kotlin/dsalgoleetcode/BuyAndSellStock.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package main.kotlin.dsalgoleetcode
* The element 1 occurs at the indices 0 and 3.
* */

class `ContainsDuplicate.kt` {
class ContainsDuplicate {

fun containsDuplicate(nums: IntArray): Boolean {

Expand Down