叶予清晨

叶予清晨

hhh

Youth Training Camp Question Practice Check-in

Little U's Digital Insertion Problem#

image

#include <iostream>
using namespace std;

int solution(int a, int b) {
    string s  = to_string(a);  
    string res;
    bool flag=1;
    string temp = to_string(b);
    for(int i=0;i<min(s.length(),temp.length());i++){
        if(temp[i]>s[i]){
            string ss = temp;
            temp = s;
            s = ss;
            break;
        }
    }
    for(int i=0;i<s.length();i++){
        if(flag&&temp[0]>=s[i]&&temp[0]>=s[i+1]){
            flag = 0;
            for(auto q : temp){
                res+=q;
            }
        }
        res+=s[i];
    }
    if(flag == 1)
    {
            for(auto q : temp){
                res+=q;
            }}
    int result = stoi(res);
    return result;
}

int main() {
    cout << (solution(76543, 4) == 765443) << endl;  
    cout << (solution(1, 0) == 10) << endl;  
    cout << (solution(44, 5) == 544) << endl;
    cout << (solution(666, 6) == 6666) << endl;
    return 0;
}

Little M's Cheese Problem#

image

#include <iostream>
#include <string>
using namespace std;
    string solution(int A, int B) {
    // PiLEASE DO NOT MODIFY THE FUNCTION SIGNATURE
    // write code here
    int c = B-A;
    string h = to_string(c);
    string q = to_string(B);
   string s = h+'/'+q;
    return s ;
}

int main() {
    std::cout << (solution(2, 7) == "5/7") << std::endl;
    std::cout << (solution(1, 3) == "2/3") << std::endl;                              
    std::cout << (solution(3, 5) == "2/5") << std::endl;
}

Reverse Selection Questions#

Little T's Password Transformation Rules#

image

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

char charToDigit(char c) {
    if (islower(c)) {
        if (c >= 'a' && c <= 'c') return '2';
        if (c >= 'd' && c <= 'f') return '3';
        if (c >= 'g' && c <= 'i') return '4';
        if (c >= 'j' && c <= 'l') return '5';
        if (c >= 'm' && c <= 'o') return '6';
        if (c >= 'p' && c <= 's') return '7';
        if (c >= 't' && c <= 'v') return '8';
        if (c >= 'w' && c <= 'z') return '9';
    } else if (isupper(c)) {
        c = tolower(c);
        if (c == 'a'){
             return '9';
        } else {
            c--;
           
            if (c >= 'a' && c <= 'c') return '2';
            if (c >= 'd' && c <= 'f') return '3';
            if (c >= 'g' && c <= 'i') return '4';
            if (c >= 'j' && c <= 'l') return '5';
            if (c >= 'm' && c <= 'o') return '6';
            if (c >= 'p' && c <= 's') return '7';
            if (c >= 't' && c <= 'v') return '8';
            if (c >= 'w' && c <= 'z') return '9';
        }

    }
    return c;
}

string solution(const string& s) {
    string result = "";
    for (char c : s) {
        result += charToDigit(c);
    }
    return result;
}

int main() {
    cout << (solution("LIming0701") == "5464640701") << endl;
    cout << (solution("PassW0rd") == "62778073") << endl;
    cout << (solution("helloWORLD123") == "4355686752123") << endl;
    return 0;
}

Numerical Operations Digital Expectation Problem#

image

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

string solution(int a, int b) {
    double c = 9.0/4*(a+b);
   stringstream ss;
    ss << fixed << setprecision(2) << c;
    string s = ss.str();
    return s;
}

int main() {
    cout << (solution(3, 3) == "13.50") << endl;
    cout << (solution(5, 7) == "27.00") << endl;
    cout << (solution(1, 1) == "4.50") << endl;
    return 0;
}

Determine the Existence of a Boomerang#

image

#include <iostream>
#include <vector>
using namespace std;

bool solution(vector<vector<int>>& points) {
    // Check for duplicate points
    if ((points[0] == points[1]) || (points[0] == points[2]) || (points[1] == points[2])) {
        return false;
    }

    // Calculate vectors
    int x1 = points[1][0] - points[0][0];
    int y1 = points[1][1] - points[0][1];
    int x2 = points[2][0] - points[0][0];
    int y2 = points[2][1] - points[0][1];

    // Check for collinearity
    return (x1 * y2 - x2 * y1) != 0;
}

int main() {
    vector<vector<int>> v1 = {{1, 1}, {2, 3}, {3, 2}};
    vector<vector<int>> v2 = {{1, 1}, {2, 2}, {3, 3}};
    vector<vector<int>> v3 = {{0, 0}, {1, 1}, {1, 0}};

    cout << (solution(v1) == true) << endl;
    cout << (solution(v2) == false) << endl;
    cout << (solution(v3) == true) << endl;
    return 0;
}

Little C's Takeout Timeout Judgment#

image

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

// Convert "HH:MM" format time string to minutes
int timeToMinutes(const string& time) {
    stringstream ss(time);
    int hour, minute;
    char colon;
    ss >> hour >> colon >> minute;
    return hour * 60 + minute;
}

string solution(const string& t1, const string& t2, const string& t3) {
    int time1 = timeToMinutes(t1);
    int time2 = timeToMinutes(t2);
    int time3 = timeToMinutes(t3);
    
    // Handle overnight cases, converting time to minutes starting from 0
    if (time2 < time1) {
        time2 += 24 * 60;
    }
     if (time3 < time1) {
         time3 += 24*60;
     }

    // Check for timeout
    if (time3 > time2) {
        return "Yes";
    } else {
        return "No";
    }
}

int main() {
    cout << (solution("18:00", "19:05", "19:05") == "No") << endl;
    cout << (solution("23:00", "00:21", "00:23") == "Yes") << endl;
    cout << (solution("23:05", "00:05", "23:58") == "No") << endl;
    return 0;
}

Wealth Assessment Project#

image

#include <iostream>
#include <vector>
#include <algorithm> // Include <algorithm> header file to use max function
using namespace std;

int solution(std::vector<std::vector<int>> financials) {
    int max_sum = 0;
    for (const auto& customer : financials) {
        int current_sum = 0;
        for (int amount : customer) {
            current_sum += amount;
        }
        max_sum = max(max_sum, current_sum); // Use max function to update maximum funds
    }
    return max_sum;
}

int main() {
    cout << (solution({{2, 4, 6}, {4, 3, 2}}) == 12) << endl;
    cout << (solution({{3, 9}, {8, 2}, {5, 6}}) == 12) << endl;
    cout << (solution({{5, 5, 9, 3}, {6, 2, 3, 1}, {3, 4, 10, 2}}) == 22) << endl;
    return 0;
}

Interval Merging for Point Timers#

image

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int solution(vector<vector<int>> inputArray) {
    if (inputArray.empty()) return 0;

    // 1. Sort
    sort(inputArray.begin(), inputArray.end());

    // 2. Merge intervals
    vector<vector<int>> mergedIntervals;
    mergedIntervals.push_back(inputArray[0]);
    for (size_t i = 1; i < inputArray.size(); ++i) {
        vector<int>& lastInterval = mergedIntervals.back();
        const vector<int>& currentInterval = inputArray[i];

        if (currentInterval[0] <= lastInterval[1]) {
            lastInterval[1] = max(lastInterval[1], currentInterval[1]);
        } else {
            mergedIntervals.push_back(currentInterval);
        }
    }

    // 3. Calculate total points
    int totalPoints = 0;
    for (const auto& interval : mergedIntervals) {
        totalPoints += (interval[1] - interval[0] + 1);
    }

    return totalPoints;
}

int main() {
    vector<vector<int>> testArray1 = {{1, 4}, {7, 10}, {3, 5}};
    vector<vector<int>> testArray2 = {{1, 2}, {6, 10}, {11, 15}};
    vector<vector<int>> testArray3 = {{1, 5}, {2, 4}};
    vector<vector<int>> testArray4 = {{1, 5}, {6, 10}};
     vector<vector<int>> testArray5 = {{}};

    cout << (solution(testArray1) == 9) << endl;
    cout << (solution(testArray2) == 12) << endl;
    cout << (solution(testArray3) == 5) << endl;
    cout << (solution(testArray4) == 10) << endl;
     cout << (solution(testArray5) == 0) << endl;
    return 0;
}

Count Liars in the Class#

image

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> A) {
    int n = A.size();
    int liarCount = 0;

    for (int i = 0; i < n; ++i) {
        int currentScore = A[i];
        int lowerOrEqualCount = 0;
        int higherCount = 0;

        for (int j = 0; j < n; ++j) {
            if (A[j] <= currentScore) {
                lowerOrEqualCount++;
            } else {
                higherCount++;
            }
        }
        if (lowerOrEqualCount > higherCount) {
            liarCount++;
        }
    }
    return liarCount;
}

int main() {
    cout << (solution({100, 100, 100}) == 3) << endl;
    cout << (solution({2, 1, 3}) == 2) << endl;
    cout << (solution({30, 1, 30, 30}) == 3) << endl;
    cout << (solution({19, 27, 73, 55, 88}) == 3) << endl;
    cout << (solution({19, 27, 73, 55, 88, 88, 2, 17, 22}) == 5) << endl;
    return 0;
}

Little M's Multi-Task Downloader#

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

int solution(int n, vector<vector<int>> array) {
    map<int, int> timeline; // Use map to store time points and task count changes
    for (const auto& task : array) {
        int startTime = task[0];
        int endTime = startTime + task[1];
        timeline[startTime]++;     // Increase task count by 1 when task starts
        timeline[endTime]--;       // Decrease task count by 1 when task ends
    }

    int maxConcurrentTasks = 0;
    int currentConcurrentTasks = 0;
    for (const auto& entry : timeline) {
        currentConcurrentTasks += entry.second;
        maxConcurrentTasks = max(maxConcurrentTasks, currentConcurrentTasks);
    }
    return maxConcurrentTasks;
}

int main() {
    cout << (solution(2, {{1, 2}, {2, 3}}) == 2) << endl;
    cout << (solution(4, {{1, 2}, {2, 3}, {3, 5}, {4, 3}}) == 3) << endl;
     cout << (solution(5 ,{{1, 3}, {3, 4}, {2, 2}, {6, 5}, {5, 3}}) == 3) << endl;
      cout << (solution(1, {{1, 2}}) == 1) << endl;
      cout << (solution(2, {{1, 2}, {3, 4}}) == 1) << endl;
    return 0;
}

Reactant Concentration#

image

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int solution(vector<string> instructions) {
    int reactant = 0;
    for (const string& instruction : instructions) {
        if (instruction == "++") {
            reactant++;
        } else if (instruction == "--") {
            reactant--;
        }
    }
    return reactant;
}

int main() {
    cout << (solution({"++", "--", "++"}) == 1) << endl;
    cout << (solution({"++", "++", "--", "--"}) == 0) << endl;
    cout << (solution({"++", "++", "--"}) == 1) << endl;
    return 0;
}

String Decoding Problem#

image

#include <iostream>
#include <string>

using namespace std;

string solution(int N, const string& S) {
    string decoded_s = "";
    for (char c : S) {
        if (c == 'x') {
            decoded_s += 'y';
        } else if (c == 'y') {
            decoded_s += 'x';
        } else if (c == 'a') {
            decoded_s += 'b';
        } else if (c == 'b') {
            decoded_s += 'a';
        } else {
            decoded_s += c;
        }
    }
    return decoded_s;
}

int main() {
    cout << (solution(5, "xaytq") == "ybxtq") << endl;
    cout << (solution(6, "abcxyz") == "bacyxz") << endl;
    cout << (solution(3, "zzz") == "zzz") << endl;
    return 0;
}

Version Number Comparison#

image

#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

int solution(string version1, string version2) {
    vector<int> v1, v2;
    
    stringstream ss1(version1);
    string token;
    while(getline(ss1, token, '.')){
        v1.push_back(stoi(token));
    }
    
      stringstream ss2(version2);
   
    while(getline(ss2, token, '.')){
        v2.push_back(stoi(token));
    }


    int i = 0;
    while (i < v1.size() || i < v2.size()) {
        int num1 = (i < v1.size()) ? v1[i] : 0;
        int num2 = (i < v2.size()) ? v2[i] : 0;
        
        if (num1 > num2) return 1;
        if (num1 < num2) return -1;
        
        i++;
    }

    return 0;
}

int main() {
    cout << (solution("0.1", "1.1") == -1) << endl;
    cout << (solution("1.0.1", "1") == 1) << endl;
    cout << (solution("7.5.2.4", "7.5.3") == -1) << endl;
    cout << (solution("1.0", "1.0.0") == 0) << endl;
    cout << (solution("1.0.1","1.0.1") == 0) << endl;
     cout << (solution("1.0.1","1.0") == 1) << endl;
      cout << (solution("1","1.0.1") == -1) << endl;
        cout << (solution("1.0","1") == 0) << endl;
          cout << (solution("1","1.0") == 0) << endl;
             cout << (solution("0.0.1","1") == -1) << endl;
                 cout << (solution("1","0.0.1") == 1) << endl;
    return 0;
}

Prefix Sum#

image

#include <iostream>
#include <vector>

using namespace std;

vector<int> solution(vector<int> nums) {
    int n = nums.size();
    vector<int> prefixSums(n);
    
    if (n == 0) return prefixSums;
    
    prefixSums[0] = nums[0];
    for (int i = 1; i < n; ++i) {
        prefixSums[i] = prefixSums[i - 1] + nums[i];
    }
    return prefixSums;
}

int main() {
    cout << (solution({4, 5, 1, 6}) == vector<int>{4, 9, 10, 16}) << endl;
    cout << (solution({2, 2, 2, 2}) == vector<int>{2, 4, 6, 8}) << endl;
    cout << (solution({7, 3, 9, 4}) == vector<int>{7, 10, 19, 23}) << endl;
    cout << (solution({1, 2, 3}) == vector<int>{1, 3, 6}) << endl;
       cout << (solution({}) == vector<int>{}) << endl;
    return 0;
}

Game Ranking Third Largest#

image

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int solution(int n, vector<int> nums) {
    set<int> uniqueNums;
    for(int num : nums) {
        uniqueNums.insert(num);
    }
    
    vector<int> sortedUniqueNums(uniqueNums.begin(), uniqueNums.end());
     sort(sortedUniqueNums.rbegin(), sortedUniqueNums.rend());

    if (sortedUniqueNums.size() >= 3) {
        return sortedUniqueNums[2];
    } else {
        return sortedUniqueNums[0];
    }
}

int main() {
    cout << (solution(3, {3, 2, 1}) == 1) << endl;
    cout << (solution(2, {1, 2}) == 2) << endl;
    cout << (solution(4, {2, 2, 3, 1}) == 1) << endl;
    cout << (solution(5, {1,2,3,4,5}) == 3) << endl;
     cout << (solution(1, {5}) == 5) << endl;
     cout << (solution(5, {1,1,1,1,1}) == 1) << endl;
     cout << (solution(5, {1,1,2,2,3}) == 1) << endl;
    return 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.