2024 February 10 Problems

51 views
Skip to first unread message

daryl...@gmail.com

unread,
Feb 10, 2024, 12:47:56 PMFeb 10
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.

Rolling over the medium problem from last week since it looks like a few people were able to solve it after the meeting.

Flocela Maldonado

unread,
Feb 10, 2024, 1:11:24 PMFeb 10
to leetcode-meetup
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {

unordered_set<char> allowedSet{};
for (const char& ch : allowed)
{
allowedSet.insert(ch);
}

int ans = 0;
for (const string& word : words)
{
int idx = 0;
while (idx < word.size())
{
char ch = word[idx];
if(allowedSet.find(ch) == allowedSet.end())
{
break;
}
++idx;
}
if (idx == word.size())
{
++ans;
}
}
return ans;
}
};

Flocela Maldonado

unread,
Feb 10, 2024, 1:13:21 PMFeb 10
to leetcode-meetup
Don't know if anyone noticed. But the medium problem 621 is the same one from last week. I'm moving on to the hard problem.

Andriy T

unread,
Feb 10, 2024, 1:34:48 PMFeb 10
to leetcode-meetup
class Solution {
     public int countConsistentStrings(String allowed, String[] words) {
          Set<Character> set = new HashSet();
          int res = 0;
          for (int i = 0;i<allowed.length(); i++) {
               set.add(allowed.charAt(i));
          }
          for (String word: words) {
               boolean consistent true;
               for (int j = 0; j < word.length(); j++) {
                    char ch = word.charAt(j);
                    consistent &= set.contains(ch);
               }
               if (consistent) res+=1;
          }
          return res;
     }
}

Flocela Maldonado

unread,
Feb 10, 2024, 2:19:09 PMFeb 10
to leetcode-meetup
621Task Scheduler58.1%Medium
Time O(tlogt + t) if t is number of tasks.
Memory O(t)
class Solution {
public:

// Used for making a max priority queue, based on the second number in the pair.
struct myComp {
constexpr bool operator()(
pair<char, int> const& a,
pair<char, int> const& b)
const noexcept
{
return a.second < b.second;
}
};

int leastInterval(vector<char>& tasks, int n) {
// a map of the count per each task
unordered_map<char, int> countPerTask{};
for (char curTask : tasks)
{
if (countPerTask.find(curTask) == countPerTask.end())
{
countPerTask.insert({curTask, 0});
}
++countPerTask[curTask];
}

// vector of pairs of tasks and their counts. Used to make priority queue.
vector<pair<char, int>> tasksAndTheirCounts{};
for (const auto& taskAndCount : countPerTask)
{
tasksAndTheirCounts.push_back(taskAndCount);
}

// Will be putting tasks in this vector.
vector<char> ans{};

// max heap priority queue. pairs are in decreasing order based on the task's count.
priority_queue<pair<char,int>, vector<pair<char, int>>, myComp> pq(tasksAndTheirCounts.begin(), tasksAndTheirCounts.end());

// for each iteration run through n letters.
while(!pq.empty())
{
// keeps the seen tasks and count
unordered_map<char, int> seen{};

// Run through n letters. If number of letters runs out, then add 'i' to indicate idle.
// Or if number of letters runs out, it may be that we've run out of letters, then break!
for (int ii=0; ii<=n; ++ii)
{
if (pq.empty())
{
if (seen.size() == 0)
{
break;
}
else
{
ans.push_back('i');
}
}
else
{
// always use the letter that has the largest count first.
pair<char, int> topPair = pq.top();
pq.pop();
char topChar = topPair.first;
int topCount = topPair.second;
if (topCount > 1)
{
seen.insert({topChar, topCount-1});
}
ans.push_back(topChar);
}
}

// for next iteration add back the used letters in seen. Realize, they're being added back in with
// one lest count.
for (const auto& seenWithCount : seen)
{
pq.push(seenWithCount);
}
}
return ans.size();

}
};

Anil Kumar Marthi

unread,
Feb 10, 2024, 2:30:53 PMFeb 10
to leetcod...@googlegroups.com
621Task Scheduler58.1%Medium

Math.max((count-1)*(n+1)+k, tasks.length)
where count is number times of most frequent repeating task
k is number of most frequent repeating tasks
n is gap

--
whatspp group: http://whatsapp.techbayarea.us/
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/52cf89fb-a845-4ae8-8cc3-a280632a3128n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages