Copy Books

Given n books and the ith book has A[i] pages. You are given k people to copy the n books.
n books list in a row and each person can claim a continous range of the n books. 
For example one copier can copy the books from ith to jth 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?


java

public class Solution {
    /**
     * @param pages: an array of integers
     * @param k: An integer
     * @return: an integer
     */
    public int copyBooks(int[] pages, int k) {
        // write your code here
        if (pages == null || pages.length == 0) {
            return 0;
        }
        int max = Integer.MIN_VALUE, sum = 0;
        for (int i = 0; i < pages.length; i++) {
            sum += pages[i];
            if (max < pages[i]) {
                max = pages[i];
            }
        }
        int start = max, end = sum, mid = 0;
        while (start + 1 < end) {
            mid = (end + start) / 2;
            if (getCount(pages, mid) > k) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if (getCount(pages, start) <= k) {
            return start;
        } else {
            return end;
        } 
    }
    
    private int getCount(int[] pages, int limit) {
        int copyer = 1, sum = 0;
        for (int i = 0; i < pages.length; i++) {
            if (sum + pages[i] > limit) {
                copyer++;
                sum = 0;
            }
            sum += pages[i];
        }
        return copyer;
    }
}

python

class Solution:
    """
    @param pages: an array of integers
    @param k: An integer
    @return: an integer
    """
    def copyBooks(self, pages, k):
        # write your code here
        if pages == None or len(pages) == 0:
            return 0
        start, end = max(pages), sum(pages)
        while start + 1 < end:
            mid = (start + end) // 2
            if self.getCopyer(pages, mid) > k:
                start = mid
            else:
                end = mid
        if self.getCopyer(pages, start) <= k:
            return start
        else:
            return end
            
    def getCopyer(self, pages, limit):
        copyer, sumVal = 1, 0
        for i in pages:
            if sumVal + i > limit:
                copyer += 1
                sumVal = 0
            sumVal += i
        return copyer

以下是基于需求分析的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
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值