Google Kickstart Round D 2k20 | Record Breaker problem (4,8)

KICK-START ROUND D SOLUTION

RECORD BREAKER PROBLEM (4pts,8pts)

Problem Explanation :




Code in Python :

def sol():
    n = int(input())
    v = list(map(intinput().split()))
    m = -1
    ans = 0
    for i in range(n):
        if v[i] > m:
            m = v[i]
            if i == n - 1 or v[i] > v[i + 1]:
                ans += 1
    return ans


T = int(input())
try:
    for i in range(T):
        print("Case #{}{}".format(i + 1, sol()))

except:
    pass


Code in CPP/C++ :

#include <bits/stdc++.h>
using namespace std;

int countRecordBreakingDays(vector<int> visitors) {
  int recordBreaksCount = 0;
  int previousRecord = 0;
  for(int i = 0; i < checkpoints.size(); i++) {
     bool greaterThanPreviousDays = i == 0 || visitors[i] > previousRecord;
     bool greaterThanFollowingDay = i == checkpoints.size()-1 || visitors[i] > visitors[i+1];
     if(greaterThanPreviousDays && greaterThanFollowingDay) {
        recordBreaksCount++;
     }
     previousRecord = max(previousRecord, visitors[i]);
  }
  return recordBreaksCount;
}

Code in Java :

# THE CODE LOOKS BIG AS IT WAS USED AS A TEMPLATE FOR CP

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

public class Solution {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        RecordBreaker solver = new RecordBreaker();
        int testCount = Integer.parseInt(in.next());
        for (int i = 1; i <= testCount; i++)
            solver.solve(i, in, out);
        out.close();
    }

    static class RecordBreaker {
        public void solve(int testNumber, InputReader sc, PrintWriter out) {
            int n = sc.nextInt();

            long a[] = new long[n];
            for (int i = 0; i < n; ++i)
                a[i] = sc.nextLong();

            long maxV = -1;
            int ans = 0;
            for (int i = 0; i < n; ++i) {
                int flag = 0;
                if (a[i] > maxV)
                    flag++;
                if (i == n - 1 || a[i] > a[i + 1])
                    flag++;
                if (flag == 2)
                    ans++;
                maxV = Math.max(maxV, a[i]);
            }
            out.println("Case #" + testNumber + ": " + ans);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();

            while (isSpaceChar(c))
                c = read();

            int sgn = 1;

            if (c == '-') {
                sgn = -1;
                c = read();
            }

            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));

            return res * sgn;
        }

        public long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;

            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            }
            while (!isSpaceChar(c));

            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public String next() {
            return readString();
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

Comments

Popular posts from this blog

How to NOT write UGLY CODE ? | PEP-8 | Python Enhancement Proposals | VS-Code Plugins | autopep8 | Prettier | Preffered Settings

DC's DSA-CRACKER #3 | Array | Kth Maxima and Minima in an Array

DC's DSA-CRACKER #1 | Array | REVERSE THE ARRAY