Leetcode Biweekly Contest 104

Published by

on

Leetcode Biweekly 104 Contest Questions Solved

6366. Number of Senior Citizens

You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:

  • The first ten characters consist of the phone number of passengers.
  • The next character denotes the gender of the person.
  • The following two characters are used to indicate the age of the person.
  • The last two characters determine the seat allotted to that person.

Return the number of passengers who are strictly more than 60 years old.

Example-1:

Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
Output: 2
Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.

Example-2:

Input: details = ["1313579440F2036","2921522980M5644"]
Output: 0
Explanation: None of the passengers are older than 60.

Constraints:

  • 1 <= details.length <= 100
  • details[i].length == 15
  • details[i] consists of digits from '0' to '9'.
  • details[i][10] is either 'M' or 'F' or 'O'.
  • The phone numbers and seat numbers of the passengers are distinct.

CODE:

class Solution {
public:
    int countSeniors(vector<string>& details) {
        int count = 0;
        int j = 11;
        for(int i=0; i<details.size(); i++)
        {
            if(details[i][11] == '7' || details[i][11] == '8' || details[i][11] == '9')
            {
                count++;
            }
            if(details[i][11] == '6')
            {
                if(details[i][12] != '0')
                {
                    count++;
                }
            }
        }
        return count;
    }
};

6367. Sum in a Matrix

You are given a 0-indexed 2D integer array nums. Initially, your score is 0. Perform the following operations until the matrix becomes empty:

  1. From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen.
  2. Identify the highest number amongst all those removed in step 1. Add that number to your score.

Return the final score.

Example 1:

Input: nums = [[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
Output: 15
Explanation: In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.

Example 2:

Input: nums = [[1]]
Output: 1
Explanation: We remove 1 and add it to the answer. We return 1.

Constraints:

  • 1 <= nums.length <= 300
  • 1 <= nums[i].length <= 500
  • 0 <= nums[i][j] <= 103

CODE:

class Solution {
public:
    int matrixSum(vector<vector<int>>& nums) {
        int score = 0;
        for(auto &i:nums)
        {
            sort(i.begin(),i.end());
        }
        for(int i=0; i<nums[0].size(); i++)
        {
            int temp = 0;
            for(int j=0; j<nums.size(); j++)
            {
                if(temp < nums[j][i])
                {
                    temp = nums[j][i];
                }
            }
            score = score + temp;
        }
        return score;
    }
};

Leave a comment