leetcode -- 1103、61

1103. Distribute Candies to People

Problem Description

We distribute some number of candies, to a row of n = num_people people in the following way:

We then give 1 candy to the first person, 2 candies to the second person, and so on until we give n candies to the last person.

Then, we go back to the start of the row, giving n + 1 candies to the first person, n + 2 candies to the second person, and so on until we give 2 * n candies to the last person.

This process repeats (with us giving one more candy each time, and moving to the start of the row after we reach the end) until we run out of candies. The last person will receive all of our remaining candies (not necessarily one more than the previous gift).

Return an array (of length num_people and sum candies) that represents the final distribution of candies.

Example 1:

Input: candies = 7, num_people = 4
Output: [1,2,3,1]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0,0].
On the third turn, ans[2] += 3, and the array is [1,2,3,0].
On the fourth turn, ans[3] += 1 (because there is only one candy left), and the final array is [1,2,3,1].

Example 2:

Input: candies = 10, num_people = 3
Output: [5,2,3]
Explanation:
On the first turn, ans[0] += 1, and the array is [1,0,0].
On the second turn, ans[1] += 2, and the array is [1,2,0].
On the third turn, ans[2] += 3, and the array is [1,2,3].
On the fourth turn, ans[0] += 4, and the final array is [5,2,3].

Constraints:

  • 1 <= candies <= 10^9
  • 1 <= num_people <= 1000

Solution Method

int* distributeCandies(int candies, int num_people, int* returnSize)
{
    int turns = 0;
    int * resArr = (int *) malloc (sizeof(int) * num_people);
    memset(resArr, 0, sizeof(int) * num_people);
    *returnSize = num_people;
    if (num_people == 0)
        return NULL;
    for (int i = 0; candies > 0; i ++)
    {
        if (i == num_people)    
        {
            i = 0;
            turns ++;
        }
        // 当candies不够时,将candies发完
        resArr[i] += candies >(turns * num_people + i+1)? (turns * num_people + i+1): candies;
        candies -= turns * num_people + i+1;
    }
    return resArr;
}    

在这里插入图片描述

61. Rotate List

Problem Description

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

Solution Method

基本思路:将链表变成一个循环链表,在合适的位置断开就好了。

struct ListNode* rotateRight(struct ListNode* head, int k)
{
    struct ListNode * p = head, * q = head, *t = NULL;
    int count = 0, sum = 0;
    if (p == NULL)
        return NULL;
    // 找到倒数第k个元素
    while (p != NULL)
    {
        sum ++;
        p=p->next;
    }
    p = head;
    k = k >= sum ? k % sum : k;	// 确保k < sum
    if (k == 0)
        return head;
    while (p->next != NULL)
    {
        if (count < k)
            p=p->next;
        else
        {
            p = p->next;
            q = q->next;
        }
        count ++;
    }
    // 断开
    t = q->next;
    q->next = NULL;
    p->next = head;
    return t;
}

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章