Skip to content

Commit

Permalink
Buy and sell stocks 2 done
Browse files Browse the repository at this point in the history
  • Loading branch information
rampatra committed Apr 24, 2019
1 parent fcc1c4a commit 7eca377
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/arrays/BuySellStocks.java
@@ -1,6 +1,7 @@
package com.leetcode.arrays;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
*
* @author rampatra
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/com/leetcode/arrays/BuySellStocksII.java
@@ -0,0 +1,94 @@
package com.leetcode.arrays;

/**
* Level: Easy
* Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
* Problem Description:
* Say you have an array for which the ith element is the price of a given stock on day i.
* <p>
* Design an algorithm to find the maximum profit. You may complete as many transactions as you
* like (i.e., buy one and sell one share of the stock multiple times).
* <p>
* Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock
* before you buy again).
*
* @author rampatra
* @since 2019-04-23
*/
public class BuySellStocksII {

/**
* The key point is we need to consider every peak immediately following a valley to maximize the profit. In case
* we skip one of the peaks (trying to obtain more profit), we will end up losing the profit over one of the
* transactions leading to an overall lesser profit.
* <a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/solution/">Read this to learn more</a>.
* <p>
* Time complexity: O(n)
* where,
* n = no. of stock prices
* <p>
* Runtime: <a href="https://leetcode.com/submissions/detail/224655101/">0 ms</a>.
*
* @param prices
* @return
*/
public static int maxProfit(int[] prices) {
int valley;
int peak;
int maxProfit = 0;

for (int i = 0; i < prices.length; i++) {
while (i < prices.length - 1 && prices[i] > prices[i + 1]) {
i++;
}
valley = i;

while (i < prices.length - 1 && prices[i] < prices[i + 1]) {
i++;
}
peak = i;

maxProfit += prices[peak] - prices[valley];
}

return maxProfit;
}

/**
* This solution follows the logic used in the above approach {@link #maxProfit(int[])}, but with only a slight
* variation. In this case, instead of looking for every peak following a valley, we can simply go on crawling over
* the slope and keep on adding the profit obtained from every consecutive transaction.
* In the end, we will be using the peaks and valleys effectively, but we need not track the costs corresponding to
* the peaks and valleys along with the maximum profit, but we can directly keep on adding the difference between the
* consecutive numbers of the array if the second number is larger than the first one, and at the total sum we obtain
* will be the maximum profit. This approach will simplify the solution.
* <p>
* Time complexity: O(n)
* where,
* n = no. of stock prices
*
* @param prices
* @return
*/
public static int maxProfitSimplified(int[] prices) {
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
maxProfit += prices[i] - prices[i - 1];
}
}
return maxProfit;
}

public static void main(String[] args) {
System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
System.out.println(maxProfit(new int[]{1, 2, 3, 4, 5}));
System.out.println(maxProfit(new int[]{7, 6, 4, 3, 1}));

System.out.println("----");

System.out.println(maxProfitSimplified(new int[]{7, 1, 5, 3, 6, 4}));
System.out.println(maxProfitSimplified(new int[]{1, 2, 3, 4, 5}));
System.out.println(maxProfitSimplified(new int[]{7, 6, 4, 3, 1}));
}
}
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/arrays/PascalsTriangle.java
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/pascals-triangle/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/arrays/RotateArray.java
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/rotate-array/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/AnagramsInString.java
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/find-all-anagrams-in-a-string/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/CountAndSay.java
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/count-and-say/
*
* @author rampatra
Expand Down
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/longest-common-prefix/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/RansomNote.java
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/ransom-note/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/ReverseStringII.java
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/reverse-string-ii/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/ReverseVowels.java
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/reverse-vowels-of-a-string/
*
* @author rampatra
Expand Down
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/first-unique-character-in-a-string/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/strings/ValidPalindrome.java
@@ -1,6 +1,7 @@
package com.leetcode.strings;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/valid-palindrome/
*
* @author rampatra
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/leetcode/trie/LongestWord.java
Expand Up @@ -4,6 +4,7 @@
import java.util.Stack;

/**
* Level: Easy
* Problem: https://leetcode.com/problems/longest-word-in-dictionary/
*
* @author rampatra
Expand Down

0 comments on commit 7eca377

Please sign in to comment.