Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f4aaae

Browse files
Max profit on Buy and Sell stock
1 parent 8930d12 commit 8f4aaae

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

‎src/main/kotlin/Main.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package main.kotlin
22

3+
import main.kotlin.dsalgoleetcode.BuyAndSellStock
4+
import main.kotlin.dsalgoleetcode.ContainsDuplicate
35
import main.kotlin.dsalgoleetcode.LinkedListCycleDetection
46
import main.kotlin.dsalgoleetcode.ListNode
57
import main.kotlin.dsalgoleetcode.LongestSubstring
@@ -32,9 +34,14 @@ fun main() {
3234
println("Has cycle $llcdResult")
3335

3436
// Calling Contains Duplicate
35-
val `containsDuplicate.kt` = `ContainsDuplicate.kt`()
36-
val containsDuplicateResult = `containsDuplicate.kt`
37+
val containsDuplicate = ContainsDuplicate()
38+
val containsDuplicateResult = containsDuplicate
3739
.containsDuplicate(intArrayOf(1,2,3,1,0,1))
3840
println("Contains Duplicate Integers $containsDuplicateResult")
3941

42+
// Calling Buy and Sell Stock
43+
val buyAndSellStock = BuyAndSellStock()
44+
val bnsResult = buyAndSellStock.maxProfit(intArrayOf(1,4,8,0,2))
45+
println("Max profit : $bnsResult")
46+
4047
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main.kotlin.dsalgoleetcode
2+
3+
/**
4+
* You are given an array prices where prices[i] is the price of a given stock on the ith day.
5+
* You want to maximize your profit by choosing a single day to buy one stock and choosing a
6+
* different day in the future to sell that stock.
7+
* Return the maximum profit you can achieve from this transaction.
8+
* If you cannot achieve any profit, return 0.
9+
Example 1:
10+
11+
Input: prices = [7,1,5,3,6,4]
12+
Output: 5
13+
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
14+
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
15+
* */
16+
17+
class BuyAndSellStock {
18+
19+
fun maxProfit(prices: IntArray): Int {
20+
21+
var minPrice = Int.MAX_VALUE
22+
var maxProfit = 0
23+
24+
for (price in prices){
25+
if( price < minPrice){
26+
minPrice = price
27+
} else{
28+
maxProfit = maxOf(maxProfit, price - minPrice)
29+
}
30+
}
31+
return maxProfit
32+
}
33+
}

‎src/main/kotlin/dsalgoleetcode/ContainsDuplicate.kt.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package main.kotlin.dsalgoleetcode
1212
* The element 1 occurs at the indices 0 and 3.
1313
* */
1414

15-
class `ContainsDuplicate.kt` {
15+
class ContainsDuplicate {
1616

1717
fun containsDuplicate(nums: IntArray): Boolean {
1818

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /