码迷,mamicode.com
首页 > 其他好文 > 详细

Weekly Contest 112

时间:2018-11-25 14:29:51      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:empty   moved   discuss   find   core   oid   win   weekly   only   

945. Minimum Increment to Make Array Unique

Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.

Return the least number of moves to make every value in A unique.

 

Example 1:

Input: [1,2,2]
Output: 1
Explanation:  After 1 move, the array could be [1, 2, 3].

Example 2:

Input: [3,2,1,2,1,7]
Output: 6
Explanation:  After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.

 

Note:

  1. 0 <= A.length <= 40000
  2. 0 <= A[i] < 40000
 

Approach #1: 

class Solution {
public:
    int minIncrementForUnique(vector<int>& A) {
        sort(A.begin(), A.end());
        int move = 0;
        for (int i = 1; i < A.size(); ++i) {
            if (A[i] <= A[i-1]) {
                int step= A[i] == A[i-1] ? 1 : A[i-1]+1-A[i];
                A[i] += step;
                move += step;
            }
        }
        return move;
    }
};

  

946. Validate Stack Sequences

Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.

 

Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.

 

Note:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed is a permutation of popped.
  4. pushed and popped have distinct values.

 

Approach #1:

class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> ipush;
        queue<int> ipop;
        for (int i = 0; i < popped.size(); ++i)
            ipop.push(popped[i]);
        for (int i = 0; i < pushed.size(); ++i) {
            ipush.push(pushed[i]);
            while (!ipush.empty() && ipush.top() == ipop.front()) {
                ipush.pop();
                ipop.pop();
            }
        }
        return ipush.empty();
    }
};

  

948. Bag of Tokens

You have an initial power P, an initial score of 0 points, and a bag of tokens.

Each token can be used at most once, has a value token[i], and has potentially two ways to use it.

  • If we have at least token[i] power, we may play the token face up, losing token[i] power, and gaining 1 point.
  • If we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.

Return the largest number of points we can have after playing any number of tokens.

 

Example 1:

Input: tokens = [100], P = 50
Output: 0

Example 2:

Input: tokens = [100,200], P = 150
Output: 1

Example 3:

Input: tokens = [100,200,300,400], P = 200
Output: 2

 

Note:

  1. tokens.length <= 1000
  2. 0 <= tokens[i] < 10000
  3. 0 <= P < 10000

 

Approach #1:

class Solution {
public:
    int bagOfTokensScore(vector<int>& tokens, int P) {
        if (tokens.size() == 0) return 0;
        sort(tokens.begin(), tokens.end());
        if (P < tokens[0]) return 0;
        int temp = 0, ans = 0;
        int start = 0, end = tokens.size()-1;
        while (start <= end && (temp > 0 || P >= tokens[ans])) {
            if (P >= tokens[start]) {
                P -= tokens[start];
                temp++;
                start++;
                ans = max(ans, temp);          
            } else {
                temp--;
                P += tokens[end];
                end--;
            }
        }
        return ans;
    }
};

  

947. Most Stones Removed with Same Row or Column

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone.

Now, a move consists of removing a stone that shares a column or row with another stone on the grid.

What is the largest possible number of moves we can make?

 

Example 1:

Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
Output: 5

Example 2:

Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
Output: 3

Example 3:

Input: stones = [[0,0]]
Output: 0

 

Note:

  1. 1 <= stones.length <= 1000
  2. 0 <= stones[i][j] < 10000

 

 Appraoch #1: C++ [grap coloring]
class Solution {
	void color(vector<vector<int>> &G, vector<int> &C, int i, int c) {
		C[i] = c;
		for (int j : G[i]) {
			if (C[j] == -1) color(G, C, j, c);
		}
	}
public:
	int removeStones(vector<vector<int>> & stones) {
		int N = stones.size();
		vector<vector<int>> G(N);
		for (int i = 0; i < N-1; i++) {
			int x = stones[i][0];
			int y = stones[i][1];
			for (int j = i + 1; j < N; j++) {
				if ((stones[j][0] == stones[i][0])||(stones[j][1] == stones[i][1])) {
					G[i].push_back(j);
					G[j].push_back(i);
				}
			}
		}
		vector<int> C(N, -1);
		int c = 0;
		for (int i = 0; i < N; i++) {
			if (C[i] == -1) color(G, C, i, c++);
		}
		return N - c;
	}
};

  

Approach #2: C++ [UnionFind]

class Solution {
public:
    int removeStones(vector<vector<int>>& stones) {
        for (int i = 0; i < stones.size(); ++i)
            uni(stones[i][0], ~stones[i][1]);
        return stones.size() - islands;
    }
    
    unordered_map<int, int> f;
    int islands = 0;
    
    int find(int x) {
        if (!f.count(x)) f[x] = x, islands++;
        if (x != f[x]) f[x] = find(f[x]);
        return f[x];
    }
    
    void uni(int x, int y) {
        x = find(x), y = find(y);
        if (x != y) f[x] = y, islands--;
    }

};

  

Approach #3: Python [DFS]

class Solution(object):
    def removeStones(self, stones):
        """
        :type stones: List[List[int]]
        :rtype: int
        """
        index = collections.defaultdict(set)
        for i, j in stones:
            index[i].add(j + 10000)
            index[j+10000].add(i)
            
        def dfs(i):
            seen.add(i)
            for j in index[i]:
                if j not in seen:
                    dfs(j)
                    
        seen = set()
        islands = 0
        
        for i, j in stones:
            if i not in seen:
                islands += 1
                dfs(i)
                dfs(j + 10000)
                
        return len(stones) - islands

  

come from: 

https://www.jianshu.com/p/30d2058db7f7

https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/discuss/197659/C%2B%2B-solution-using-graph-coloring

 

Weekly Contest 112

标签:empty   moved   discuss   find   core   oid   win   weekly   only   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/10015272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!