kkanyo / coding_test Goto Github PK
View Code? Open in Web Editor NEWStudy for coding-test
Study for coding-test
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).
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();
}
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.