leetcode Day29----array.easy

博客介绍了三道算法题,包括“Maximize Distance to Closest Person”“Transpose Matrix”“Fair Candy Swap”,分别给出了题目描述、示例及注意事项,并展示了使用C语言、Python3、JAVA实现这些算法题的运行时间和内存使用情况,还提及了Python中两种除法运算符的区别。

Maximize Distance to Closest Person

In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty.

There is at least one empty seat, and at least one person sitting.

Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized.

Return that maximum distance to closest person.

Example 1:

Input: [1,0,0,0,1,0,1]
Output: 2
Explanation:
If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.
If Alex sits in any other open seat, the closest person has distance 1.
Thus, the maximum distance to the closest person is 2.
Example 2:

Input: [1,0,0,0]
Output: 3
Explanation:
If Alex sits in the last seat, the closest person is 3 seats away.
This is the maximum distance possible, so the answer is 3.
Note:

1 <= seats.length <= 20000
seats contains only 0s or 1s, at least one 0, and at least one 1.

C语言

int maxDistToClosest(int* seats, int seatsSize){
    int left=-1,count=0,max=1,i=0,distance;
    while(i<seatsSize){
        if(seats[i] == 0){count++;}
        else{
            if(left<0)
                distance=count;
            else{
                distance=count/2+count%2;}
            left=i;count=0;
            if(distance>max){max=distance;}
        }
    ++i;
    }
        return (max>count)?max:count;
}

Success
Details
Runtime: 12 ms, faster than 45.21% of C online submissions for Maximize Distance to Closest Person.
Memory Usage: 8 MB, less than 100.00% of C online submissions for Maximize Distance to Closest Person.

python3

class Solution:
    def maxDistToClosest(self, seats: List[int]) -> int:
        left, count, _max_distance = -1, 0, 1
        for i, x in enumerate(seats):
            if x == 0:
                count += 1
            else:
                if left < 0:
                    distance = count
                else:
                    distance = count // 2 + count % 2
                left, count = i, 0
                _max_distance = max(_max_distance, distance)
        return max(_max_distance, count)
        

Success
Details
Runtime: 56 ms, faster than 69.46% of Python3 online submissions for Maximize Distance to Closest Person.
Memory Usage: 13.5 MB, less than 5.55% of Python3 online submissions for Maximize Distance to Closest Person.

两种除法运算符:"/"、"//"。两者最大区别在:

  1. python2.2前的版本和python2.2以后3.0以前的版本的默认情况下,"/"所做的除法是以一种两个数或者多个数出现一个浮点数结果就以浮点数的形式表示,即float除法
  2. "//“所做的除法则不相同,”//"不管两者出现任何数,都以整除结果为准,不对小数部分进行处理,直接抛弃,也就是整除法

Transpose Matrix

Given a matrix A, return the transpose of A.

The transpose of a matrix is the matrix flipped over it’s main diagonal, switching the row and column indices of the matrix.

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:

Input: [[1,2,3],[4,5,6]]
Output: [[1,4],[2,5],[3,6]]

Note:

1 <= A.length <= 1000
1 <= A[0].length <= 1000

C语言

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** transpose(int** A, int ASize, int* AColSize, int* returnSize, int** returnColumnSizes){
    int **B = (int **)malloc(sizeof(int *) * (*AColSize));
    for(int i = 0 ; i < *AColSize; i++) *(B + i) = (int *)malloc(sizeof(int) * ASize);
    *returnColumnSizes = (int *)malloc(sizeof(int) * (*AColSize));
    for(int i = 0 ; i < *AColSize; i++) *(*returnColumnSizes + i) = ASize;
    for(int ColIdx = 0; ColIdx < *AColSize; ColIdx++){
        for(int RowIdx = 0; RowIdx < ASize; RowIdx++){
            *(*(B + ColIdx) + RowIdx) = *(*(A + RowIdx) + ColIdx);
        }
    }
    *returnSize = *AColSize;
    return B;
}


Success
Details
Runtime: 20 ms, faster than 95.87% of C online submissions for Transpose Matrix.
Memory Usage: 11.2 MB, less than 11.11% of C online submissions for Transpose Matrix.

python3

class Solution:
    def transpose(self, A: List[List[int]]) -> List[List[int]]:
        return list(map(list,zip(*A)))

Success
Details
Runtime: 60 ms, faster than 81.38% of Python3 online submissions for Transpose Matrix.
Memory Usage: 13.7 MB, less than 5.45% of Python3 online submissions for Transpose Matrix.

Fair Candy Swap

Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.

Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.

Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]
Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]
Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]
Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]

Note:

1 <= A.length <= 10000
1 <= B.length <= 10000
1 <= A[i] <= 100000
1 <= B[i] <= 100000
It is guaranteed that Alice and Bob have different total amounts of candy.
It is guaranteed there exists an answer.

JAVA

class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        int dif = (IntStream.of(A).sum() - IntStream.of(B).sum()) / 2;
        HashSet<Integer> S = new HashSet<>();
        for (int a : A) S.add(a);
        for (int b : B) if (S.contains(b + dif)) return new int[] {b + dif, b};
        return new int[0];
    }
}

Success
Details
Runtime: 54 ms, faster than 34.43% of Java online submissions for Fair Candy Swap.
Memory Usage: 39.7 MB, less than 94.07% of Java online submissions for Fair Candy Swap.

python3

class Solution:
    def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
        s = (sum(A) - sum(B)) / 2
        A = set(A)
        for b in set(B):
            if s + b in A:
                return [int(s+b), b]

Success
Details
Runtime: 56 ms, faster than 100.00% of Python3 online submissions for Fair Candy Swap.
Memory Usage: 15.6 MB, less than 5.05% of Python3 online submissions for Fair Candy Swap.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值