题解 | #将真分数分解为埃及分数#

Redraiment的走法

http://www.nowcoder.com/practice/24e6243b9f0446b081b1d6d32f2aa3aa

代码清晰,好理解:

import java.util.*;

public class Main {
    // 其实就是最长上升子序列
    // 利用二分来查找要插入的位置
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int numCount = in.nextInt();

            int[] nums = new int[numCount];
            for (int i = 0; i < numCount; i++) {
                nums[i] = in.nextInt();
            }

            List<Integer> ascList = new ArrayList<>();
            for (int i = 0; i < numCount; i++) {
                int target = nums[i];
                if (ascList.size() == 0 || target > ascList.get(ascList.size() - 1)) {
                    ascList.add(target);
                } else {
                    int left = 0, right = ascList.size() - 1;
                    int pos = -1;
                    while (left <= right) {
                        int mid = (left + right) / 2;
                        int searchVal = ascList.get(mid);
                        if (target == searchVal) {
                            pos = mid;
                            break;
                        } else if (target > searchVal) {
                            left = mid + 1;
                        } else {
                            // 保存右边界,让右侧更紧密,左侧更稀疏,进而左侧有更多机会寻找更小的数字
                            pos = mid;
                            right = mid - 1;
                        }
                    }
                    ascList.set(pos, target);
                }
            }

            System.out.println(ascList.size());
        }
    }
}
全部评论

相关推荐

在投简历的小学生很爱...:感觉你这实习好水,自己在优化一下实习部分吧
点赞 评论 收藏
分享
05-13 00:41
已编辑
北京邮电大学 Java
理性的杰克刷牛客:ai肯定要有的,最好学一下agent方向加一个智能客服什么的进去,并且多加点什么skill,mcp啥的,另外你现在的项目深度有些浅,这些功能都太简单了,而且也不是真正能扛高并发的实现,没有什么太大的亮点,可以去网上找点更有深度的项目。可以先投一些中小厂,有实习经历以后再去大厂,你现在这个大厂可能机会不大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务