当前位置: 面试刷题>> 友谊服务 (经典算法题500道)


题目描述补充

题目:友谊服务 - 朋友圈动态排序

在码小课的社交平台上,用户们可以发布自己的动态到朋友圈,其他用户可以点赞、评论和分享这些动态。为了提升用户体验,平台需要实现一个功能,即根据一定的规则对朋友圈的动态进行排序显示。排序规则如下:

  1. 时间因素:最新的动态应该排在最前面。
  2. 互动因素:如果两个动态发布时间相近,那么点赞数、评论数更多的动态应该排在前面。
  3. 用户关系:如果用户与发布动态的人有好友关系,那么这条动态应该有一定的优先级提升(假设好友关系权重为固定值,如增加10%的排序权重)。

示例数据结构

假设我们有一个动态的数据结构如下(以Python为例):

class Post:
    def __init__(self, id, user_id, timestamp, likes, comments, is_friend=False):
        self.id = id
        self.user_id = user_id
        self.timestamp = timestamp  # 发布时间,假设为UNIX时间戳
        self.likes = likes
        self.comments = comments
        self.is_friend = is_friend  # 是否为当前用户的好友发布

    def score(self, friend_bonus=1.1):
        # 计算排序分数,考虑时间、互动和用户关系
        # 假设时间越近分数越高(这里简单用时间戳的负值表示)
        # 互动越多分数越高
        # 好友发布的动态分数有额外加成
        return -(self.timestamp + self.likes * 10 + self.comments * 5) * (friend_bonus if self.is_friend else 1)

示例代码

以下是使用Python、PHP和JavaScript分别实现的示例代码,用于对一组动态进行排序。

Python 示例

posts = [
    Post(1, 101, 1633036800, 5, 2, True),
    Post(2, 102, 1633040400, 10, 1, False),
    Post(3, 101, 1633033200, 8, 3, True),
]

# 按score排序
posts.sort(key=lambda post: post.score(), reverse=True)

for post in posts:
    print(f"Post ID: {post.id}, Score: {post.score()}")

PHP 示例

class Post {
    public $id;
    public $user_id;
    public $timestamp;
    public $likes;
    public $comments;
    public $is_friend;

    function __construct($id, $user_id, $timestamp, $likes, $comments, $is_friend) {
        $this->id = $id;
        $this->user_id = $user_id;
        $this->timestamp = $timestamp;
        $this->likes = $likes;
        $this->comments = $comments;
        $this->is_friend = $is_friend;
    }

    function score($friend_bonus = 1.1) {
        return -($this->timestamp + $this->likes * 10 + $this->comments * 5) * ($this->is_friend ? $friend_bonus : 1);
    }
}

$posts = [
    new Post(1, 101, 1633036800, 5, 2, true),
    new Post(2, 102, 1633040400, 10, 1, false),
    new Post(3, 101, 1633033200, 8, 3, true),
];

usort($posts, function($a, $b) {
    return $b->score() <=> $a->score();
});

foreach ($posts as $post) {
    echo "Post ID: {$post->id}, Score: " . $post->score() . "\n";
}

JavaScript 示例

class Post {
    constructor(id, userId, timestamp, likes, comments, isFriend) {
        this.id = id;
        this.userId = userId;
        this.timestamp = timestamp;
        this.likes = likes;
        this.comments = comments;
        this.isFriend = isFriend;
    }

    score(friendBonus = 1.1) {
        return -(this.timestamp + this.likes * 10 + this.comments * 5) * (this.isFriend ? friendBonus : 1);
    }
}

const posts = [
    new Post(1, 101, 1633036800, 5, 2, true),
    new Post(2, 102, 1633040400, 10, 1, false),
    new Post(3, 101, 1633033200, 8, 3, true),
];

posts.sort((a, b) => b.score() - a.score());

posts.forEach(post => {
    console.log(`Post ID: ${post.id}, Score: ${post.score()}`);
});

以上代码展示了如何在不同编程语言中实现根据特定规则对朋友圈动态进行排序的功能。在码小课网站上,你可以找到更多关于算法和数据结构的相关内容,帮助你深入学习并提升编程技能。

推荐面试题