Skip to content

Commit 754d71e

Browse files
committed
Update Greedy solutions as per HSC
1 parent 40d10a9 commit 754d71e

21 files changed

Lines changed: 344 additions & 175 deletions

‎Algorithms/Greedy/BeautifulPairs.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
package Greedy;
2-
3-
import java.util.*;
41
import java.io.*;
2+
import java.util.*;
53

6-
/*
7-
* @author -- rajatgoyal715
8-
*/
4+
public class Solution {
95

10-
public class BeautifulPairs {
116
public static void main(String[] args) throws IOException{
127
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
138
int n=Integer.parseInt(br.readLine());

‎Algorithms/Greedy/Candies.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
static int n;
10+
11+
public static void main(String[] args) {
12+
int i,arr[],temp=1,prev=0,count[];
13+
long candies=0;
14+
Scanner scan = new Scanner(System.in);
15+
n = scan.nextInt();
16+
arr = new int[n];
17+
count = new int[n];
18+
for(i=0;i<n;i++){
19+
scan.nextLine();
20+
arr[i] = scan.nextInt();
21+
count[i] = 1;
22+
}
23+
for(i=1;i<n;i++){
24+
if(arr[i]>arr[i-1]){
25+
count[i] = count[i-1]+1;
26+
}
27+
/*
28+
else if(i!=0 && arr[i]<arr[i-1]){
29+
int j = i;
30+
while(j>0 && arr[j] < arr[j-1]){
31+
count[j-1] = Math.max(count[j-1],count[j] + 1);
32+
j--;
33+
}
34+
}*/
35+
}
36+
for(i=n-2;i>=0;i--){
37+
if(arr[i]>arr[i+1]){
38+
count[i]=Math.max(count[i],count[i+1]+1);
39+
}
40+
}
41+
for(i=0;i<n;i++)
42+
candies += count[i];
43+
System.out.print(candies);
44+
}
45+
}

‎Algorithms/Greedy/ChiefHopper.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Solution {
5+
6+
public static void main(String[] args) {
7+
Scanner sc=new Scanner(System.in);
8+
int n=sc.nextInt();
9+
int a[] = new int[n+1];
10+
for(int i=1;i<=n;i++){
11+
a[i]=sc.nextInt();
12+
}
13+
a[0]=0;
14+
int curr=n;
15+
int prev=n-1;
16+
int energy=0;
17+
int diff;
18+
int add=0;
19+
while(curr!=0){
20+
diff=a[curr]-a[prev];
21+
energy+=diff;
22+
if(energy<0){
23+
add=0-energy;
24+
}
25+
curr--;
26+
prev--;
27+
}
28+
System.out.println(energy+add);
29+
}
30+
}
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
import java.util.*;
21
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
36

4-
/*
5-
* @author -- rajatgoyal715
6-
*/
7+
public class Solution {
78

8-
public class CuttingBoards {
99
public static void main(String[] args) throws IOException {
1010
Scanner sc = new Scanner(System.in);
1111
int t = sc.nextInt();
12+
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
// int t = Integer.parseInt(br.readLine());
1214
StringBuilder sb = new StringBuilder("");
1315
while(t--!=0){
1416
int m = sc.nextInt();
1517
int n = sc.nextInt();
18+
// String s1[] = br.readLine().split(" ");
19+
// int m = Integer.parseInt(s1[0]);
20+
// int n = Integer.parseInt(s1[1]);
1621
long y[] = new long[m-1];
22+
// String s2[] = br.readLine().split(" ");
1723
for(int i=0;i<m-1;i++){
1824
y[i] = sc.nextLong();
25+
// y[i] = Integer.parseInt(s2[i]);
1926
}
2027
long x[] = new long[n-1];
28+
// String s3[] = br.readLine().split(" ");
2129
for(int i=0;i<n-1;i++){
2230
x[i] = sc.nextLong();
31+
// x[i] = Integer.parseInt(s3[i]);
2332
}
24-
sb.append(cutBoard(m, n, y, x) + "\n");
33+
sb.append(cost(m, n, y, x) + "\n");
2534
}
2635
System.out.println(sb.toString());
2736
}
2837

29-
public static long cutBoard(int m, int n, long[] y, long[] x){
38+
public static long cost(int m, int n, long[] y, long[] x){
3039
Arrays.sort(y);
3140
Arrays.sort(x);
3241
int horizontal = 1;
@@ -38,25 +47,29 @@ public static long cutBoard(int m, int n, long[] y, long[] x){
3847
while(i>=0&&j>=0){
3948
if(y[i]>=x[j]){
4049
cost += vertical*y[i];
50+
// if(cost>=mod) cost%=mod;
4151
i--;
4252
horizontal++;
4353
}
4454
else{
4555
cost += horizontal*x[j];
56+
// if(cost>=mod) cost%=mod;
4657
j--;
4758
vertical++;
4859
}
4960
}
5061
while(j>=0){
5162
cost += horizontal*x[j];
63+
// if(cost>=mod) cost%=mod;
5264
j--;
5365
vertical++;
5466
}
5567
while(i>=0){
5668
cost += vertical*y[i];
69+
// if(cost>=mod) cost%=mod;
5770
i--;
5871
horizontal++;
5972
}
6073
return cost%mod;
6174
}
62-
}
75+
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
package Greedy;
2-
1+
/* Sample program illustrating input/output methods */
32
import java.util.*;
4-
import java.io.*;
53

6-
/*
7-
* @author -- rajatgoyal715
8-
*/
4+
class Solution{
5+
public static void main( String args[] ){
6+
7+
// helpers for input/output
98

10-
public class GreedyFlorist {
11-
public static void main( String args[] ){
12-
Scanner in = new Scanner(System.in);
13-
int N, K;
14-
N = in.nextInt();
15-
K = in.nextInt();
16-
int C[] = new int[N];
17-
for(int i=0; i<N; i++){
18-
C[i] = in.nextInt();
19-
}
20-
Arrays.sort(C);
21-
int result = 0;
22-
int x =1;
23-
int j=0;
24-
for(int i=N-1;i>=0;i--){
25-
result = result + x*C[i];
26-
j++;
27-
if(j==K){
28-
x++;
29-
j=0;
30-
}
31-
}
32-
System.out.println( result );
33-
}
9+
Scanner in = new Scanner(System.in);
10+
11+
int N, K;
12+
N = in.nextInt();
13+
K = in.nextInt();
14+
15+
int C[] = new int[N];
16+
for(int i=0; i<N; i++){
17+
C[i] = in.nextInt();
18+
}
19+
Arrays.sort(C);
20+
int result = 0;
21+
int x =1;
22+
int j=0;
23+
for(int i=N-1;i>=0;i--){
24+
result = result + x*C[i];
25+
j++;
26+
if(j==K){
27+
x++;
28+
j=0;
29+
}
30+
}
31+
System.out.println( result );
32+
33+
}
3434
}

‎Algorithms/Greedy/GridChallenge.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package Greedy;
2-
3-
import java.util.*;
41
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
56

6-
/*
7-
* @author -- rajatgoyal715
8-
*/
7+
public class Solution {
98

10-
public class GridChallenge {
119
public static void main(String[] args) {
1210
Scanner sc = new Scanner(System.in);
1311
int t = sc.nextInt();
@@ -37,4 +35,4 @@ public static void main(String[] args) {
3735
System.out.println(output);
3836
}
3937
}
40-
}
38+
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
package Greedy;
2-
3-
import java.util.*;
41
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
56

6-
/*
7-
* @author -- rajatgoyal715
8-
*/
7+
public class Solution {
98

10-
public class JimAndOrders {
119
public static void main(String[] args) {
1210
Scanner sc=new Scanner(System.in);
1311
int n=sc.nextInt();
1412
int a[] = new int[n];
1513
String s[]=new String[n+1];
14+
//Map<Key, Value>
15+
//key is a[i]
16+
//value is index
1617
Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
1718
for(int i=0;i<n;i++){
1819
a[i]=sc.nextInt()+sc.nextInt();
@@ -30,4 +31,4 @@ public static void main(String[] args) {
3031
System.out.print(s[t2]);
3132
}
3233
}
33-
}
34+
}

‎Algorithms/Greedy/LargestPermutation.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import java.util.*;
21
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
36

4-
class LargestPermutation{
7+
public class Solution {
58

6-
public static void main(String[] args) {
9+
public static void main(String[] args) {
710
Scanner input = new Scanner(System.in);
811
int n = input.nextInt();
912
int k = input.nextInt();

‎Algorithms/Greedy/LuckBalance.java‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package Greedy;
2-
3-
import java.util.*;
41
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
56

6-
/*
7-
* @author -- rajatgoyal715
8-
*/
7+
public class Solution {
98

10-
public class LuckBalance {
119
public static void main(String[] args) {
1210
Scanner sc=new Scanner(System.in);
1311
int n=sc.nextInt();
@@ -38,11 +36,18 @@ public static void main(String[] args) {
3836
for(int i=n-k;i<n;i++){
3937
sum+=b[i];
4038
}
39+
//System.out.println("greater than");
4140
}
4241
else{
4342
for(int i=total;i<n;i++)
4443
sum+=b[i];
44+
//System.out.println("less than");
4545
}
46+
/*System.out.println("Printing B");
47+
for(int i=0;i<n;i++)
48+
System.out.print(b[i]+" ");
49+
System.out.println(total);
50+
System.out.println(n2);*/
4651
System.out.println(sum);
4752
}
48-
}
53+
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package Greedy;
2-
3-
import java.util.*;
41
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
56

6-
/**
7-
* Created by rajat goyal on 3/19/2017.
8-
*/
7+
public class Solution {
98

10-
public class MarcsCakewalk {
119
public static void main(String[] args) {
1210
Scanner in = new Scanner(System.in);
1311
int n = in.nextInt();
@@ -17,7 +15,7 @@ public static void main(String[] args) {
1715
}
1816
method1(calories, n);
1917
}
20-
18+
2119
public static void method1(int[] c,int n) {
2220
Arrays.sort(c);
2321
long sum = 0;

0 commit comments

Comments
 (0)