Smarking Algorithm Contest 3

1. 453. Minimum Moves to Equal Array Elements

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.
Ex:

Input:
[1,2,3]

Output:
3

Explanation:
Only three moves are needed (remember each move increments two elements):

[1,2,3]  =>  [2,3,3]  =>  [3,4,3]  =>  [4,4,4]

给一个长度为n的非空数组,将这个数组中的其中n-1个元素都+1,称为一次操作。求出将这个数组所有元素都变为相同大小的操作的次数。
解法:

func minMoves(nums []int) int {
    if len(nums) <= 1{
        return 0
    }
    sum,min,max := nums[0],nums[0],nums[0]
    for i:=1;i<len(nums);i++{
        sum += nums[i]
        if min > nums[i]{
            min = nums[i]
        }
        if max < nums[i]{
            max = nums[i]
        }
    }
    return sum - len(nums)*min
}

2. 447. Number of Boomerangs

Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Ex:

Input:
[[0,0],[1,0],[2,0]]

Output:
2

Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

给定平面内的坐标。求出这些坐标中,一个点到另外两个点距离相同的组合。
这题只要求出每个坐标到其他坐标的距离。然后再遍历就行了。

func pow(t int) int{
    return t*t
}
func numberOfBoomerangs(points [][]int) int {
    if len(points) <3{
        return 0
    }
    dis := make([][]int,len(points))
    for i:=0;i<len(points);i++{
        dis[i] = make([]int,len(points))
    }
    for i:=0;i<len(points);i++{
        for j:=i+1;j<len(points);j++{
            t := pow(points[i][0] - points[j][0])+pow(points[i][1] - points[j][1])
            dis[i][j],dis[j][i] = t,t
        }
    }
    count := 0
    for i:=0;i<len(points);i++{
        for j:=0;j<len(points);j++{
            if j == i{
                continue
            }
            for k := j+1;k<len(points);k++{
                if dis[i][j] == dis[i][k]{
                    count +=2
                }
            }
        }
    }
    return count
}

3. 452. Minimum Number of Arrows to Burst Balloons

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it’s horizontal, y-coordinates don’t matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.

An arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
Ex:

Input:
[[10,16], [2,8], [1,6], [7,12]]

Output:
2

Explanation:
One way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).

这题把一堆乱七八糟的背景剥开后,题目实际上是这样的:
给定一组区间,然后要你给出长度最小的一组数,这组数中所有的点都在区间内而且没有区间被遗漏。
用贪心算法就可以做。将区间按起点从小到大排列。
然后每一次都选一个点,能够尽量的覆盖到最多的区间。直到不能覆盖为止。依次取点,直到区间都有点落在上面。

type ranges [][]int 

func(r ranges)Len() int{
    return len(r)
}
func(r  ranges)Less(i,j int)bool{
    if r[i][0] != r[j][0]{
        return r[i][0]<r[j][0]
    }
    return r[i][1]<r[j][1]
}

func (r  ranges)Swap(i,j int){
    r[i][0],r[j][0] = r[j][0],r[i][0]
    r[j][1],r[i][1] = r[i][1],r[j][1]
}

func findMinArrowShots(points [][]int) int {
    p := ranges(points)
    sort.Sort(p)
    count := 0
    for i:=0;i<len(p);i++{
        count ++
        j := i+1
        at := p[i][1]
        for j = i+1;j<len(p) ;j++{
            if p[j][0]>at{
                i = j-1
                break
            }
            if p[j][1] < at{
                at = p[j][1]
            }
        }
        if j == len(p){
            break
        }

    }
    return count
}

4. 446. Arithmetic Slices II - Subsequence

A zero-indexed array A consisting of N numbers is given. A subsequence slice of that array is any sequence of integers (P0, P1, …, Pk) such that 0 ≤ P0 < P1 < … < Pk < N.

A subsequence slice (P0, P1, …, Pk) of array A is called arithmetic if the sequence A[P0], A[P1], …, A[Pk-1], A[Pk] is arithmetic. In particular, this means that k ≥ 2.

The function should return the number of arithmetic subsequence slices in the array A.

Ex:

Input: [2, 4, 6, 8, 10]

Output: 7

Explanation:
All arithmetic subsequence slices are:
[2,4,6]
[4,6,8]
[6,8,10]
[2,4,6,8]
[4,6,8,10]
[2,4,6,8,10]
[2,6,10]

这题试了下DFS会超时,用DP做会MLE。最后没做出来。
给个别人的代码:

class Solution(object):
    def numberOfArithmeticSlices(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        ans=0
        dp = []
        for i in range(len(A)):
            a=A[i]
            dp.append({})
            for j in range(i):
                b=A[j]
                d=a-b
                if d in dp[j]:
                    v=1+dp[j][d]
                else:
                    v=1
                if d in dp[i]:
                    dp[i][d]=dp[i][d]+v
                else:
                    dp[i][d]=v
            ans += sum([dp[i][x] for x in dp[i]]) - i
        return ans
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值