Skip to content

Greedy-02 Solutions Posted#566

Open
angad-singh97 wants to merge 3 commits into
super30admin:masterfrom
angad-singh97:master
Open

Greedy-02 Solutions Posted#566
angad-singh97 wants to merge 3 commits into
super30admin:masterfrom
angad-singh97:master

Conversation

@angad-singh97

Copy link
Copy Markdown

No description provided.

@super30admin

Copy link
Copy Markdown
Owner

Task Scheduler (01-task-scheduler.cpp)

Strengths:

  1. Excellent time complexity of O(N) - significantly better than the reference solution
  2. Optimal space complexity of O(1) using a fixed-size array
  3. Correctly handles the edge case where multiple tasks share the maximum frequency
  4. Clean, readable code with good variable naming
  5. Well-commented solution showing understanding of the problem

Areas for Improvement:

  1. The formula approach, while efficient, requires careful derivation. Consider also understanding the simulation approach as a backup verification method.
  2. Variable name maxFreqCount could be more descriptive like maxFreqTaskCount to clarify it represents the number of tasks with max frequency.
  3. The comment about "lowercase alphabet" should say "uppercase" since tasks are uppercase letters A-Z.

Minor Note: The solution header mentions cpp but the code is in C++ (which is correct for the class structure used).

VERDICT: PASS


Queue Reconstruction by Height (02-queue-reconstruction-by-height.cpp)

Strengths:

  • Excellent understanding of the greedy algorithm and why it works
  • Good use of reserve() for memory optimization
  • Clear comments explaining the approach and time/space complexity
  • Successfully solved on LeetCode

Areas for Improvement:

  • The insertion line has a subtle bug: result.insert(result.begin() + count, {height, count}) uses count as the second element. While this works because count == k, it's confusing and could be a source of bugs if the code is modified. Should use {height, person.second} or {height, count} with a renamed variable for clarity.
  • The intermediate conversion to vector<pair<int, int>> is unnecessary — you could sort the original vector<vector<int>> directly with a custom comparator.
  • Consider using LinkedList instead of vector for O(1) insertions, though this would complicate iteration for the final result.

VERDICT: PASS


Partition Labels (03-partition-labels.cpp)

Strengths:

  • Correct implementation of the greedy algorithm with clear logic
  • Good variable naming that makes the code self-documenting
  • Well-commented code explaining the approach and complexity
  • Efficient two-pass solution that avoids unnecessary computations
  • Proper handling of edge cases (initializing array with -1)

Areas for Improvement:

  • The raw array posFromRight[26] could be replaced with std::array<int, 26> for better C++ idioms and bounds checking
  • The commented-out debug cout statements should be removed before submission
  • Consider using size_t instead of int for loop variables when working with string indices

The solution demonstrates a solid understanding of the problem and achieves optimal time complexity. The approach is intuitive and well-implemented.

VERDICT: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants