Weekly Contest 197

周赛地址(英):weekly contest 197
周赛地址(中):第 197 场周赛
仓库地址:week-Leetcode

1512. Number of Good Pairs

Given an array of integers nums.

A pair (i,j) is called good if nums[i] == nums[j] and i < j.

Return the number of good pairs.

Example 1:

Input: nums = [1,2,3,1,1,3]
Output: 4
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.

Example 2:

Input: nums = [1,1,1,1]
Output: 6
Explanation: Each pair in the array are good.

Example 3:

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

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100

题解

/**
 * @param {number[]} nums
 * @return {number}
 */
const numIdenticalPairs = function(nums) {
    let ans=0;
    for(let i=0;i<nums.length;i++){
        for(let j=i+1;j<nums.length;j++){
            if(nums[i]===nums[j]) ans++;
        }
    }
    return ans
};

1513. Number of Substrings With Only 1s

Given a binary string s (a string consisting only of '0' and '1's).

Return the number of substrings with all characters 1's.

Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:

Input: s = "0110111"
Output: 9
Explanation: There are 9 substring in total with only 1's characters.
"1" -> 5 times.
"11" -> 3 times.
"111" -> 1 time.

Example 2:

Input: s = "101"
Output: 2
Explanation: Substring "1" is shown 2 times in s.

Example 3:

Input: s = "111111"
Output: 21
Explanation: Each substring contains only 1's characters.

Example 4:

Input: s = "000"
Output: 0

Constraints:

  • s[i] == '0' or s[i] == '1'
  • 1 <= s.length <= 10^5

题解

/**
 * @param {string} s
 * @return {number}
 */
const numSub = function(s) {
    let M=Math.pow(10,9)+7
    let ans=0;
    let curr=0
    for(let i=0;i<s.length;i++){
        if(s[i]=='1') {
            curr++
           ans+=curr 
        } else {
            curr=0
        } 
    }
    return ans%M
};

1514. Path with Maximum Probability

You are given an undirected weighted graph of n nodes (0-indexed), represented by an edge list where edges[i] = [a, b] is an undirected edge connecting the nodes a and b with a probability of success of traversing that edge succProb[i].

Given two nodes start and end, find the path with the maximum probability of success to go from start to end and return its success probability.

If there is no path from start to end, return 0. Your answer will be accepted if it differs from the correct answer by at most 1e-5.

Example 1:

example1

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Output: 0.25000
Explanation: There are two paths from start to end, one having a probability of success = 0.2 and the other has 0.5 * 0.5 = 0.25.

Example 2:
example2

Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.3], start = 0, end = 2
Output: 0.30000

Example 3:
example3

Input: n = 3, edges = [[0,1]], succProb = [0.5], start = 0, end = 2
Output: 0.00000
Explanation: There is no path between 0 and 2.

Constraints:

  • 2 <= n <= 10^4
  • 0 <= start, end < n
  • start != end
  • 0 <= a, b < n
  • a != b
  • 0 <= succProb.length == edges.length <= 2*10^4
  • 0 <= succProb[i] <= 1
  • There is at most one edge between every two nodes.

题解

/**
 * @param {number} n
 * @param {number[][]} edges
 * @param {number[]} succProb
 * @param {number} start
 * @param {number} end
 * @return {number}
 */
const maxProbability = function(n, edges, succProb, start, end) {
        let graph = new Map()
        let probMap = new Map()
        for (let i = 0; i < n; i++) {
            graph.set(i, new Set());
            probMap.set(i, new Map());
        }
        for (let i = 0; i < edges.length; i++) {
            let a = edges[i][0];
            let b = edges[i][1];
            graph.get(a).add(b);
            graph.get(b).add(a);
            probMap.get(a).set(b, succProb[i]);
            probMap.get(b).set(a, succProb[i]);
        }
        
        let pq=[]
        pq.push([start, 1]);
        let set = new Set();
        let res = 0.0;

        while (pq.length>0) {
            let cur=pq.splice(0,1)[0];
            let index = cur[0];
            if (index === end)
                res = Math.max(res, cur[1]);
            if (set.has(index)) {
                continue;
            }else{
              set.add(index)  
            } 

            for (let next of graph.get(index)){
                 pq.push([next, cur[1] * probMap.get(index).get(next)]);   
                 pq.sort((a,b)=>b[1]-a[1])
            }
        }
        return res;
};

1515. Best Position for a Service Centre

A delivery company wants to build a new service centre in a new city. The company knows the positions of all the customers in this city on a 2D-Map and wants to build the new centre in a position such that the sum of the euclidean distances to all customers is minimum.

Given an array positions where positions[i] = [xi, yi] is the position of the ith customer on the map, return the minimum sum of the euclidean distances to all customers.

In other words, you need to choose the position of the service centre [xcentre, ycentre] such that the following formula is minimized:

position

Answers within 10^-5 of the actual value will be accepted.

Example 1:

example1

Input: positions = [[0,1],[1,0],[1,2],[2,1]]
Output: 4.00000
Explanation: As shown, you can see that choosing [xcentre, ycentre] = [1, 1] will make the distance to each customer = 1, the sum of all distances is 4 which is the minimum possible we can achieve.

Example 2:

example2

Input: positions = [[1,1],[3,3]]
Output: 2.82843
Explanation: The minimum possible sum of distances = sqrt(2) + sqrt(2) = 2.82843

Example 3:

Input: positions = [[1,1]]
Output: 0.00000

Example 4:

Input: positions = [[1,1],[0,0],[2,0]]
Output: 2.73205
Explanation: At the first glance, you may think that locating the centre at [1, 0] will achieve the minimum sum, but locating it at [1, 0] will make the sum of distances = 3.
Try to locate the centre at [1.0, 0.5773502711] you will see that the sum of distances is 2.73205.
Be careful with the precision!

Example 5:

Input: positions = [[0,1],[3,2],[4,5],[7,6],[8,9],[11,1],[2,12]]
Output: 32.94036
Explanation: You can use [4.3460852395, 4.9813795505] as the position of the centre.

Constraints:

  • 1 <= positions.length <= 50
  • positions[i].length == 2
  • 0 <= positions[i][0], positions[i][1] <= 100

题解

/**
 * @param {number[][]} positions
 * @return {number}
 */
const getMinDistSum = function(pos) {
        var n = pos.length;
    if(pos.length == 1){
        return 0;
    }
    var dis = 0;
    var x = 0,y =0;
    var dx = [-1, 0, 1, 0];
    var dy = [0, 1, 0, -1];
    for ( var i = 0; i < n; i++){
        x += pos[i][0],
        y += pos[i][1]; 
    }
	//geometric centre
    x /= n; 
    y /= n;
    var edist = function(x,y){
        var ret = 0;
        for ( var i = 0; i < n; i++){
            var dx = pos[i][0] - x;
            var dy = pos[i][1] - y;
            ret += Math.sqrt(dx*dx + dy*dy); 
        }
        return ret;
    }
    var d = edist(x, y);
    var max = 100;
    var flag = 0;
    while (max > 0.000001){
        flag = 0;
        for ( var i = 0; i < 4; i++){
            var nx = x + max*dx[i];
            var ny = y + max*dy[i];
            var t = edist(nx, ny);

            if ( t < d ){
                d = t;
                x = nx;
                y = ny;
                flag = 1;
                break;
            }else{
                dis = t
            }
        }
        if ( !flag )
            max /= 2;
    }
    return dis;
};

参考

posted @ 2020-07-12 21:37  mingL  阅读(155)  评论(0编辑  收藏  举报