Giter Site home page Giter Site logo

coding_test's Introduction

Practice for coding-test

I commit here solved problem at coding-test site.

These codes may not be the perfect answer!

If you have better answer and share that, I appreciated for.

coding_test's People

Contributors

kkanyo avatar

Watchers

 avatar

coding_test's Issues

Attending_Workshops: Time limit exceeded

When the number of test case is too many, time limit exceed.
So, need to optimize this code.

// need to optimize
int CalculateMaxWorkshops(Available_Workshops* ptr) {
    int currentTime = 0;
    int result = 0;
    
    for (int i=0; i < (ptr->n)-1; i++) {    // sort by endTime
        for (int j=i+1; j < ptr->n; j++) {  // Earliest end time first
            struct Workshop tmp;
            if (ptr->arrWs[i].endTime > ptr->arrWs[j].endTime) {
                tmp = ptr->arrWs[i];
                ptr->arrWs[i] = ptr->arrWs[j];
                ptr->arrWs[j] = tmp;
            }
        }
    }
    
    for (int i=0; i < ptr->n; i++) {    
        if (currentTime <= ptr->arrWs[i].startTime) {   //pass the workshop already start
            currentTime = ptr->arrWs[i].endTime;
            result++;
        }
    }
    
    return result;
}

Now, time complexity of 'CalculateMaxWorkshops' function is O(n^2).

Time limit exceeded

This code did not execute within the time limits. Please optimize your code.
It works only when the number of samples is small.

Because, This code's time complexity is 'O(n^2)'.

    while (i < n) {
        subArr.push_back(arr[i++]);

        for (auto j : subArr) {         //O(n^2), Not effective
            if (j > max) { max = j; }
        }
        cout << max << " ";
        
        max = 0;
        subArr.pop_front();
    }

Magic Spells: Do not work correctly about generic spell of some test case

When the generic spells input consecutively, somtimes, result about one of the generic spell is incorrect.
However, If only input the it with problem, it works correctly.

else {  // For generic spells
        // find the longest common subsequence using dynamic programming
        string scrollName_ = spell->revealScrollName();
        string journal_ = SpellJournal::read();
        long long scrollNameSize = scrollName_.size();
        long long journalSize = journal_.size();
        int table[journalSize+1][scrollNameSize+1];
        
        for (int i = 0; i < scrollNameSize; i++) { table[0][i] = 0; }
        for (int i = 0; i < journalSize; i++) { table[i][0] = 0; }
        
        for (int i = 1; i <= journalSize; i++) {
            for (int j = 1; j <= scrollNameSize; j++) {
                if (journal_[i-1] == scrollName_[j-1]) {
                    table[i][j] = table[i-1][j-1] + 1;
                }
                else { 
                    table[i][j] = max(table[i-1][j], table[i][j-1]);
                }
            }
        }
        cout << table[journalSize][scrollNameSize] << endl;
    }

This code related to find a longest subsequence of letters that are contained in both the spell name and spell journal.
I think problem is in this code.
Need to find cause of error and optimize that code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.