Last active
June 27, 2016 18:23
-
-
Save kyunghoj/4207e561a9efb7cdcc2c3fc729caefeb to your computer and use it in GitHub Desktop.
Coding challenge #10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class StockN | |
{ | |
// Input format: | |
// n | |
// numbers (daily prices) | |
private static void stockBuySell(int price[], int n) | |
{ | |
int count = 0; | |
int buy[] = new int[(n/2) + 1]; | |
int sell[] = new int[(n/2) + 1]; | |
int i = 0; | |
while (i < n-1) | |
{ | |
// Find local Minima. Note that the limit is (n-2) as we are | |
// comparing present element to the next element. | |
while ((i < n - 1) && (price[i+1] <= price[i])) | |
i++; | |
// If we reached the end, break as no further solution possible | |
if (i == n-1) | |
break; | |
// Store the index of minima | |
buy[count] = i++; | |
// Now, find Local Maxima. Note the the limit is (n-1) as we are | |
// comparing to previous element | |
while ((i < n) && (price[i] >= price[i-1])) | |
{ | |
i++; | |
} | |
// Store the index of maxima | |
sell[count] = i-1; | |
// Increment count of buy/sell pairs | |
count++; | |
} | |
if (count == 0) | |
{ | |
System.out.println("There is no day when buying the stock will make profit\n"); | |
} | |
else | |
{ | |
for (int j = 0; j < count; j++) | |
System.out.printf("Buy on day: %d\t Sell on day: %d\n", buy[j], sell[j]); | |
} | |
} | |
public static void main (String [] args) | |
{ | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
int price[] = new int[n]; | |
int idx = 0; | |
while ( in.hasNextInt() ) | |
{ | |
price[idx++] = in.nextInt(); | |
} | |
if (n <= 1) | |
return; | |
stockBuySell(price, n); | |
return; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
// http://www.geeksforgeeks.org/stock-buy-sell/ | |
public class Stock | |
{ | |
// Input format: | |
// n | |
// numbers (daily prices) | |
public static void main (String [] args) | |
{ | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
int price[] = new int[n]; | |
int idx = 0; | |
while ( in.hasNextInt() ) | |
{ | |
price[idx++] = in.nextInt(); | |
} | |
int A[][] = new int[n][n]; | |
for (int i = 0; i < n - 1; i++) | |
{ | |
int j = i + 1; | |
if (price[j] > price[i]) | |
{ | |
A[i][j] = price[j] - price[i]; | |
} | |
} | |
for (int d = 0; d < n; d++) | |
{ | |
for (int j = 0; j < n - d; j++) | |
{ | |
int max = 0; | |
for (int k = 0; k < d; k++) | |
{ | |
max = Math.max(max, A[j][j+k] + A[j+k][j+d]); | |
} | |
A[j][j+d] = max; | |
System.out.println("A[" + j + "][" + (j+d) + "] = " + A[j][j+d]); | |
} | |
} | |
// This should be the max profit | |
System.out.println(A[0][n-1]); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment