← Home

1217. Play with Chips

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
121

Let's make this one less mysterious. For 1217. Play with Chips, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are minCostToMoveChips.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 63.72% of C++ online submissions for Play with Chips.
02//Memory Usage: 8.6 MB, less than 100.00% of C++ online submissions for Play with Chips.
03
04class Solution {
05public:
06    int minCostToMoveChips(vector<int>& chips) {
07        int odd_count = 0, even_count = 0;
08        for(int chip : chips){
09            if(chip % 2 == 1) odd_count++;
10            else even_count++;
11        }
12        //assume we have change all odd chip to 1 and all even chip to 0
13        //now we want to either change all 0 to 1 or all 1 to 1
14        //we choose to change all 0 to 1 if 0's count is less and vice versa
15        return min(even_count, odd_count);
16    }
17};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.