[LeetCode] 2140. Solving Questions With Brainpower

Posted CNoodle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 2140. Solving Questions With Brainpower相关的知识,希望对你有一定的参考价值。

You are given a 0-indexed 2D integer array questions where questions[i] = [pointsi, brainpoweri].

The array describes the questions of an exam, where you have to process the questions in order (i.e., starting from question 0) and make a decision whether to solve or skip each question. Solving question i will earn you pointsi points but you will be unable to solve each of the next brainpoweri questions. If you skip question i, you get to make the decision on the next question.

  • For example, given questions = [[3, 2], [4, 3], [4, 4], [2, 5]]:
    • If question 0 is solved, you will earn 3 points but you will be unable to solve questions 1 and 2.
    • If instead, question 0 is skipped and question 1 is solved, you will earn 4 points but you will be unable to solve questions 2 and 3.

Return the maximum points you can earn for the exam.

Example 1:

Input: questions = [[3,2],[4,3],[4,4],[2,5]]
Output: 5
Explanation: The maximum points can be earned by solving questions 0 and 3.
- Solve question 0: Earn 3 points, will be unable to solve the next 2 questions
- Unable to solve questions 1 and 2
- Solve question 3: Earn 2 points
Total points earned: 3 + 2 = 5. There is no other way to earn 5 or more points.

Example 2:

Input: questions = [[1,1],[2,2],[3,3],[4,4],[5,5]]
Output: 7
Explanation: The maximum points can be earned by solving questions 1 and 4.
- Skip question 0
- Solve question 1: Earn 2 points, will be unable to solve the next 2 questions
- Unable to solve questions 2 and 3
- Solve question 4: Earn 5 points
Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

Constraints:

  • 1 <= questions.length <= 105
  • questions[i].length == 2
  • 1 <= pointsi, brainpoweri <= 105

解决智力问题。

给你一个下标从 0 开始的二维整数数组 questions ,其中 questions[i] = [pointsi, brainpoweri] 。

这个数组表示一场考试里的一系列题目,你需要 按顺序 (也就是从问题 0 开始依次解决),针对每个问题选择 解决 或者 跳过 操作。解决问题 i 将让你 获得  pointsi 的分数,但是你将 无法 解决接下来的 brainpoweri 个问题(即只能跳过接下来的 brainpoweri 个问题)。如果你跳过问题 i ,你可以对下一个问题决定使用哪种操作。

比方说,给你 questions = [[3, 2], [4, 3], [4, 4], [2, 5]] :
如果问题 0 被解决了, 那么你可以获得 3 分,但你不能解决问题 1 和 2 。
如果你跳过问题 0 ,且解决问题 1 ,你将获得 4 分但是不能解决问题 2 和 3 。
请你返回这场考试里你能获得的 最高 分数。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/solving-questions-with-brainpower
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是 DP,我参考了这个帖子。这道题可以从左往右计算,也可以从右往左计算。我这里分享一个从左往右计算的做法,因为比较好思考。这里 dp[i] 的定义是从第 0 个问题到第 I - 1 个问题

对于在 index == i 位置上的问题,questions[i][0] 是得分,questions[i][0] 是需要跳过的问题的个数,所以对于位置 i 来说,

如果不解决这个问题,那么在当前位置的得分 = 上一个位置的得分,即 dp[i] = dp[i - 1]

如果解决这个问题,因为涉及到需要跳过的问题,所以我们直接看他跳到的下一个位置 j 的得分是多少,其中 j = i + 1 + questions[i][0],如果是从位置 i 跳过来的,他的得分 dp[j] = dp[i] + questions[i][0]

时间O(n)

空间O(n)

Java实现

 class Solution 
     public long mostPoints(int[][] questions) 
         var n = questions.length;
         var f = new long[n + 1];
         for (var i = 0; i < n; i++) 
             f[i + 1] = Math.max(f[i + 1], f[i]);
             var q = questions[i];
             var j = Math.min(i + q[1] + 1, n);
             f[j] = Math.max(f[j], f[i] + q[0]);
         
         return f[n];
     
 
 // 正序

 

LeetCode 题目总结

AST11103 Problem Solving


AST11103 Problem Solving with Programming Skills
Additional Individual Assignment: Min-Game Programming (40%)
Learning Outcomes
1. Use common application software and program development tools;
2. Analyze simple problems with basic problem solving skills and techniques;
3. Apply programming skills and styles in constructing simple computer application;
4. Create reliable and error-free computer application with systematic testing technique; and
5. 5. Compose documentation for the computer applications.
Introduction
The aim of this project is to apply and demonstrate all your skills learnt in this course and implement a
mini-game in Java. You are free to design the GUI and functions of your game. You will also produce a user
manual as documentation of your game. In lab 8, we have used Random() to make a simple game, in this
assignment, you are expected to design your own mini-game, here are some ideas of mini game:
- Paper, Scissors and Rock
- random number guessing game
- Blackjack
- And any other game idea
Basic Requirements
You are required to implement a Mini-game Java GUI application. The application should include, but not
limited to, the following features:
A. Basic Features (30%)
? Display user-friendly GUIs that enable player to start and end the game.
? Reasonable game logic and end game conditions (win or loss).
? The game should allow player to play multiple rounds of the game, your game should record the
accumulated marks / scores / number of wins, etc.
? Your game contains a high scores record system using read/write of txt file (Lab 9).
? Any additional and appropriate features to make the game interesting.
B. Documentation (10%)
? Besides adding sufficient comments in your Java code, you are required to write a Player
Manual (at most 5 pages).
? The Player Manual provides information to player on how to start, play and complete your
AST11103课程作业代做、代写Programming Skills作业
mini-game in a step-by-step manner. By following your player manual, players should be able
to run your game without any additional assistance.
AST11103 Problem Solving with Programming Skills
© Copyright 2019, Prepared by Dr. Lau Ho Lam. All Rights Reserved 2
Submission
? Please make sure your project is runnable in NetBeans and the PDF is readable.
? Use Export > To Zip to export your game and rename your zip file as “YourFullName_Game.zip”
(For example, “LauHoLam_Game.zip”).
? You may use any software to produce your player manual, save it as PDF file.
? Put both (1) the Exported ZIP file and (2) PDF player manual into a single ZIP file as
“YourFullName.zip” (For example, “LauHoLam.zip”).
? Upload your zip file to the course website-> Assignments -> Additional Individual Assignment.
? Due Date: 21 December 2019 23:59, late submission will NOT be accepted.
Testing Environment
? When we grade your project, we will use NetBeans Import from ZIP to open and test your project.
? If you have any special requirements, please contact me (holamlau@cityu.edu.hk) by email.

因为专业,所以值得信赖。如有需要,请加QQ99515681  微信:codehelp

以上是关于[LeetCode] 2140. Solving Questions With Brainpower的主要内容,如果未能解决你的问题,请参考以下文章

B - Crossword solving

SOLVING PROBLEMS

AST11103 Problem Solving

ENGG1110 — Problem Solving

COMP 1043 Problem Solving and Programming

Solving Linear Equation