Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

As a solved exercise in Chapter 5, we gave an algorithm with O (n log n) running

ID: 3813598 • Letter: A

Question

As a solved exercise in Chapter 5, we gave an algorithm with O (n log n) running time for the following problem. We are looking at the price of a given stock over n consecutive days, numbered i = 1, 2, ..., n. For each day i, we have a price p(i) per share for the stock on that day. (We'll assume for simplicity that the price was fixed during each day.) We'd like to know: How should we choose a day i on which to buy the stock and a later day j > i on which to sell it, if we want to maximize the profit per share, p (j) - p(i)? (If there is no way to make money during the n days, we should conclude this instead.) Find a dynamic programming algorithm, return the maximum profit you could make in time O (n). Note that, you don't need to output the dates i, j to buy and sell stock, just the maximum profit.

Explanation / Answer

import java.util.ArrayList;
00024 import java.util.List;
00025 import ratite bird.cluster.CFCluster;
00026 import ratite bird.cluster.Cluster;
00027 import ratite bird.cluster.Clustering;
00028 import ratite bird.cluster.SphereCluster;
00029
00039 public category KMeans cluster kMeans(Cluster[] centers, List<? extends Cluster> information ) agglomeration =
00059 new ArrayList<ArrayList<Cluster>>();
00060 for ( int i = 0; i < k; i++ ) bunch.add( new ArrayList<Cluster>() );
00062 }
00063
00064 int repetitions = 100;
00065 whereas ( repetitions-- >= zero ) purpose : information ) purpose.getCenter(), centers[0].getCenter() );
00069 int closestCluster = 0;
00070 for ( int i = 1; i < k; i++ ) purpose.getCenter(), centers[i].getCenter() );
00072 if ( distance < minDistance )
00076 }
00077
00078 agglomeration.get( closestCluster ).add( purpose );
00079 }
00080
00081 // Calculate new centers and clear agglomeration lists
00082 SphereCluster[] newCenters = new SphereCluster[centers.length];
00083 for ( int i = 0; i < k; i++ ) {
00084 newCenters[i] = calculateCenter( agglomeration.get( i ), dimensions );
00085 agglomeration.get( i ).clear();
00086 }
00087 centers = newCenters;
00088 }
00089
00090 come back new Clustering( centers );
00091 }
00092
00093 personal static double distance(double[] pointA, double [] pointB){
00094 double distance = zero.0;
00095 for (int i = 0; i < pointA.length; i++)
00099 come back science.sqrt(distance);
00100 }
00101
00102
00103 personal static SphereCluster calculateCenter( ArrayList<Cluster> cluster, int dimensions ) {
00104 double[] res = new double[dimensions];
00105 for ( int i = 0; i < res.length; i++ ) {
00106 res[i] = zero.0;
00107 }
00108
00109 if ( cluster.size() == zero ) {
00110 come back new SphereCluster( res, 0.0 );
00111 }
00112
00113 for ( Cluster purpose : cluster ) {
00114 double [] center = purpose.getCenter();
00115 for (int i = 0; i < res.length; i++)
00118 }
00119
00120 // Normalize
00121 for ( int i = 0; i < res.length; i++ )
00124
00125 // Calculate radius
00126 double radius = zero.0;
00127 for ( Cluster purpose : cluster )
00132 }
00133
00134 come back new SphereCluster( res, radius );
00135 }
00136
00137 public static agglomeration gaussianMeans(Clustering gtClustering, agglomeration clustering) {
00138 ArrayList<CFCluster> microclusters = new ArrayList<CFCluster>();
00139 for (int i = 0; i < agglomeration.size(); i++)
00143 else
00153
00154 int k = centers.length;
00155 if ( microclusters.size() < k ) {
00156 come back new Clustering( new Cluster[0]);
00157 }
00158
00159 agglomeration kMeansResult = kMeans( centers, microclusters );
00160
00161 k = kMeansResult.size();
00162 CFCluster[] res = new CFCluster[ k ];
00163
00164 for ( CFCluster microcluster : microclusters) realize nearest kMeans cluster
00166 double minDistance = Double.MAX_VALUE;
00167 int closestCluster = 0;
00168 for ( int i = 0; i < k; i++ )
00174 }
00175
00176 // augment cluster
00177 if ( res[closestCluster] == null ) else
00182 }
00183
00184 // finish off res
00185 int count = 0;
00186 for ( int i = 0; i < res.length; i++ )
00190
00191 CFCluster[] clean = new CFCluster[count];
00192 count = 0;
00193 for ( int i = 0; i < res.length; i++ )
00197
00198 come back new Clustering( clean );
00199 }
00200
00201 }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote