Copy Books

Given n books and the i-th book has pages[i] pages. There are k persons to copy these books.

These books list in a row and each person can claim a continous range of books. For example, one copier can copy the books from i-th to j-th continously, but he can not copy the 1st book, 2nd book and 4th book (without 3rd book).

They start copying books at the same time and they all cost 1 minute to copy 1 page of a book. What's the best strategy to assign books so that the slowest copier can finish at earliest time?

Return the shortest time that the slowest copier spends.

Example

Example 1:

Input: pages = [3, 2, 4], k = 2
Output: 5
Explanation: 
    First person spends 5 minutes to copy book 1 and book 2.
    Second person spends 4 minutes to copy book 3.

Example 2:

Input: pages = [3, 2, 4], k = 3
Output: 4
Explanation: Each person copies one of the books.

Challenge

O(nk) time

Notice

The sum of book pages is less than or equal to 2147483647

思路:二分答案,直接求不好求,那么我们就问,如果给你无穷多的人,你需要多久? Max(Length of page[i]), 如果给你1个人,你需要多久,sum (Pages[i]) ,那么问题来了,你需要求给你k个人情况下的,你需要多久。很显然,这又是个递增递减的关系;

人越多,需要的时间越少,人越少需要的时间越多;

create一个canCopy函数,看在K个人的情况下,limit time能否copy完,那么搜索能够用k个人copy完的最小的时间。

public class Solution {
    /**
     * @param pages: an array of integers
     * @param k: An integer
     * @return: an integer
     */
    public int copyBooks(int[] pages, int k) {
        if(pages == null || pages.length == 0) {
            return 0;
        }
        int start = 0; // max(p)
        int end = 0; // sum(p)
        for(int p: pages) {
            start = Math.max(start, p);
            end += p;
        }

        while(start + 1 < end) {
            int mid = start + (end - start) / 2;
            if(needPerson(mid, pages) > k) {
                start = mid;
            } else {
                // needPerson(mid, pages) <= k
                end = mid;
            }
        }
        if(needPerson(start, pages) > k) { // 这个判断条件,跟上面的判断start, end一模一样;
            return end;
        }
        return start;
    }

    private int needPerson(int timelimit, int[] pages) {
        int count = 0;
        int sum = 0;
        int i = 0;
        while(i < pages.length) {
            // 为了计算在时间limit下,一个人能够抄多少,最后计算出需要多少人在限定时间下能够抄完;
            while(i < pages.length && sum + pages[i] <= timelimit) {
                sum += pages[i];
                i++;
            }
            count++;
            sum = 0;
        }
        return count;
    }
}

以下是基于需求分析的12个核心实体及关系设计: 1. Users (用户表) - 主键: user_id - 字段: username, password_hash, email, phone, register_time, last_login 2. Roles (角色表) - 主键: role_id - 字段: role_name, role_desc 3. User_Roles (用户角色关联表) - 复合主键: user_id, role_id - 外键: user_id -> Users, role_id -> Roles 4. Books (图书信息表) - 主键: book_id - 字段: isbn, title, abstract, publish_year, word_count 5. Authors (作者表) - 主键: author_id - 字段: author_name, nationality, biography 6. Book_Authors (图书作者关联表) - 复合主键: book_id, author_id - 外键: book_id -> Books, author_id -> Authors 7. Publishers (出版社表) - 主键: publisher_id - 字段: publisher_name, address, phone, website 8. Categories (图书分类表) - 主键: category_id - 字段: category_name, parent_id, description 9. Book_Copies (馆藏副本表) - 主键: copy_id - 字段: book_id, copy_status, purchase_date, location - 外键: book_id -> Books 10. Loans (借阅记录表) - 主键: loan_id - 字段: user_id, copy_id, loan_date, due_date, return_date, status - 外键: user_id -> Users, copy_id -> Book_Copies 11. Reservations (预约表) - 主键: reservation_id - 字段: user_id, book_id, reservation_date, status, notify_date - 外键: user_id -> Users, book_id -> Books 12. Fines (罚款记录表) - 主键: fine_id - 字段: loan_id, fine_amount, fine_reason, paid_status, create_time - 外键: loan_id -> Loans (一对一关联) 关系说明 一对一关系: Loans↔ Fines(一条逾期借阅记录对应一条罚款记录) 一对多关系: Publishers → Books(一个出版社出版多本书) Categories → Books(一个分类包含多本书) Books → Book_Copies(一本书有多个物理副本) Users → Loans(一个用户有多条借阅记录) 多对多关系: Users ↔ Roles(通过`User_Roles`中间表) Books ↔ Authors(通过`Book_Authors`中间表) Users↔ Books(通过`Reservations`中间表实现预约) 生成ldm pdm 图
最新发布
06-17
#include <iostream> #include <cstring> #include <utility> #include <vector> using namespace std; // 书 class Book { public: Book(const char *name, const char *isbn = "", float price=0.0, const char *text=""); // 构造函数 Book(const Book &copy); // 复制构造函数 Book(Book &&copy) noexcept; // 移动构造函数 Book & operator=(const Book &copy); // 重载赋值运算符 Book & operator=(Book && copy) noexcept; // 移动赋值 void setText (const char *text); // 修改书的描述 ~Book() { delete [] strText; } // 析构函数 void Show() const; // 显示书本:名称(isbn, 价格): 书的描述 friend ostream& operator<<(ostream& out,const class Book& bk); private: char name[128]; // 书名 char ISBN[32]; // ISBN号 float price; // 价格 char *strText; // 书的描述 }; // 图书馆 class Library { public: Library(const char *name, int size=100); // 默认构造函数 Library(const Library & copy); // 拷贝构造 Library(Library && copy) noexcept; // 移动构造 Library & operator= (const Library &copy); // 赋值 Library & operator= (Library &&copy) noexcept; // 移动赋值 void Show(); // 显示名称和藏书数量 void addBook(const Book &book); // 增加一本书的收藏 vector<Book> & getBooks(); // 获取图书 friend ostream& operator<<(ostream& out,const class Library& lib); private: char name[128]; // 图书馆的名字 vector<Book> books; // 收藏的书 }; /* 请在这里填写答案 */ // 主函数main() int main() { char txt1[] = "C++ Programming"; Book b1("BK0011", "ISBN-0011", 35.0, txt1); char txt2[] = "Operating System"; Book b2("BK0022", "ISBN-0022", 40.0, txt2); Library lib1("HangZhou Library"); // 创建图书馆并增加2本书 lib1.addBook(b1); lib1.addBook(b2); // lib1.Show(); cout << lib1 << endl; Library lib2(lib1); // 拷贝构造并增加1本书 lib2.addBook(Book("BK1031", "ISBN-1031", 100, "Economy Theory")); // lib2.Show(); cout << lib2 << endl; Library lib3 = std::move(lib1); // 移动构造 // lib3.Show(); // lib1.Show(); cout << lib3 << endl; cout << lib1 << endl; Library lib4("NanJing"); lib4.addBook(Book("BK2109", "ISBN-2109", 200, "The Law Principles")); lib4 = std::move(lib2); // 移动赋值 // lib4.Show(); // lib2.Show(); cout << lib4 << endl; cout << lib2 << endl; }会得到: HangZhou Library:2 BK0011(ISBN-0011,35):C++ Programming BK0022(ISBN-0022,40):Operating System HangZhou Library:3 BK0011(ISBN-0011,35):C++ Programming BK0022(ISBN-0022,40):Operating System BK1031(ISBN-1031,100):Economy Theory HangZhou Library:2 BK0011(ISBN-0011,35):C++ Programming BK0022(ISBN-0022,40):Operating System :0 HangZhou Library:3 BK0011(ISBN-0011,35):C++ Programming BK0022(ISBN-0022,40):Operating System BK1031(ISBN-1031,100):Economy Theory NanJing:1 BK2109(ISBN-2109,200):The Law Principles
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值