{ "data": { "question": { "questionId": "3494", "questionFrontendId": "3218", "boundTopicId": null, "title": "Minimum Cost for Cutting Cake I", "titleSlug": "minimum-cost-for-cutting-cake-i", "content": "

There is an m x n cake that needs to be cut into 1 x 1 pieces.

\n\n

You are given integers m, n, and two arrays:

\n\n\n\n

In one operation, you can choose any piece of cake that is not yet a 1 x 1 square and perform one of the following cuts:

\n\n
    \n\t
  1. Cut along a horizontal line i at a cost of horizontalCut[i].
  2. \n\t
  3. Cut along a vertical line j at a cost of verticalCut[j].
  4. \n
\n\n

After the cut, the piece of cake is divided into two distinct pieces.

\n\n

The cost of a cut depends only on the initial cost of the line and does not change.

\n\n

Return the minimum total cost to cut the entire cake into 1 x 1 pieces.

\n\n

 

\n

Example 1:

\n\n
\n

Input: m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]

\n\n

Output: 13

\n\n

Explanation:

\n\n

\"\"

\n\n\n\n

The total cost is 5 + 1 + 1 + 3 + 3 = 13.

\n
\n\n

Example 2:

\n\n
\n

Input: m = 2, n = 2, horizontalCut = [7], verticalCut = [4]

\n\n

Output: 15

\n\n

Explanation:

\n\n\n\n

The total cost is 7 + 4 + 4 = 15.

\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 102, "dislikes": 5, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "3\n2\n[1,3]\n[5]\n2\n2\n[7]\n[4]", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Array", "slug": "array", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Dynamic Programming", "slug": "dynamic-programming", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Greedy", "slug": "greedy", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Sorting", "slug": "sorting", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class Solution {\npublic:\n int minimumCost(int m, int n, vector& horizontalCut, vector& verticalCut) {\n \n }\n};", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class Solution {\n public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class Solution(object):\n def minimumCost(self, m, n, horizontalCut, verticalCut):\n \"\"\"\n :type m: int\n :type n: int\n :type horizontalCut: List[int]\n :type verticalCut: List[int]\n :rtype: int\n \"\"\"\n ", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class Solution:\n def minimumCost(self, m: int, n: int, horizontalCut: List[int], verticalCut: List[int]) -> int:\n ", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "int minimumCost(int m, int n, int* horizontalCut, int horizontalCutSize, int* verticalCut, int verticalCutSize) {\n \n}", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class Solution {\n public int MinimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} m\n * @param {number} n\n * @param {number[]} horizontalCut\n * @param {number[]} verticalCut\n * @return {number}\n */\nvar minimumCost = function(m, n, horizontalCut, verticalCut) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function minimumCost(m: number, n: number, horizontalCut: number[], verticalCut: number[]): number {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class Solution {\n\n /**\n * @param Integer $m\n * @param Integer $n\n * @param Integer[] $horizontalCut\n * @param Integer[] $verticalCut\n * @return Integer\n */\n function minimumCost($m, $n, $horizontalCut, $verticalCut) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "class Solution {\n func minimumCost(_ m: Int, _ n: Int, _ horizontalCut: [Int], _ verticalCut: [Int]) -> Int {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\n fun minimumCost(m: Int, n: Int, horizontalCut: IntArray, verticalCut: IntArray): Int {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Dart", "langSlug": "dart", "code": "class Solution {\n int minimumCost(int m, int n, List horizontalCut, List verticalCut) {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "func minimumCost(m int, n int, horizontalCut []int, verticalCut []int) int {\n \n}", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer} m\n# @param {Integer} n\n# @param {Integer[]} horizontal_cut\n# @param {Integer[]} vertical_cut\n# @return {Integer}\ndef minimum_cost(m, n, horizontal_cut, vertical_cut)\n \nend", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "object Solution {\n def minimumCost(m: Int, n: Int, horizontalCut: Array[Int], verticalCut: Array[Int]): Int = {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "impl Solution {\n pub fn minimum_cost(m: i32, n: i32, horizontal_cut: Vec, vertical_cut: Vec) -> i32 {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define/contract (minimum-cost m n horizontalCut verticalCut)\n (-> exact-integer? exact-integer? (listof exact-integer?) (listof exact-integer?) exact-integer?)\n )", "__typename": "CodeSnippetNode" }, { "lang": "Erlang", "langSlug": "erlang", "code": "-spec minimum_cost(M :: integer(), N :: integer(), HorizontalCut :: [integer()], VerticalCut :: [integer()]) -> integer().\nminimum_cost(M, N, HorizontalCut, VerticalCut) ->\n .", "__typename": "CodeSnippetNode" }, { "lang": "Elixir", "langSlug": "elixir", "code": "defmodule Solution do\n @spec minimum_cost(m :: integer, n :: integer, horizontal_cut :: [integer], vertical_cut :: [integer]) :: integer\n def minimum_cost(m, n, horizontal_cut, vertical_cut) do\n \n end\nend", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"20.2K\", \"totalSubmission\": \"35.6K\", \"totalAcceptedRaw\": 20173, \"totalSubmissionRaw\": 35603, \"acRate\": \"56.7%\"}", "hints": [ "The intended solution uses Dynamic Programming.", "Let dp[sx][sy][tx][ty] denote the minimum cost to cut the rectangle into 1 x 1 pieces.", "Iterate on the row or column on which you will perform the next cut, after the cut, the current rectangle will be decomposed into two sub-rectangles." ], "solution": null, "status": null, "sampleTestCase": "3\n2\n[1,3]\n[5]", "metaData": "{\n \"name\": \"minimumCost\",\n \"params\": [\n {\n \"type\": \"integer\",\n \"name\": \"m\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"n\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"horizontalCut\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"verticalCut\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n }\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 17 using the latest C++ 23 standard, and libstdc++ provided by GCC 13.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\"], \"java\": [\"Java\", \"

OpenJDK 21. Using compile arguments: --enable-preview --release 21

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"c\": [\"C\", \"

Compiled with gcc 13 using the gnu11 standard.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. Adding an item to a hash.\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            /* we'll use this field as the key */\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

2. Looking up an item in a hash:\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

3. Deleting an item in a hash:\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n
\\r\\n

\"], \"csharp\": [\"C#\", \"

C# 12 with .NET 8 runtime

\"], \"javascript\": [\"JavaScript\", \"

Node.js 20.10.0.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.4.0 version of datastructures-js/priority-queue and 4.2.3 version of datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.2

\\r\\n\\r\\n

Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms

\"], \"swift\": [\"Swift\", \"

Swift 5.9.

\\r\\n\\r\\n

You may use swift-algorithms 1.2.0 and swift-collections 1.1.0.

\"], \"golang\": [\"Go\", \"

Go 1.21

\\r\\n

Support https://pkg.go.dev/github.com/emirpasic/gods@v1.18.1 and https://pkg.go.dev/github.com/emirpasic/gods/v2@v2.0.0-alpha library.

\"], \"python3\": [\"Python3\", \"

Python 3.11.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 3.3.1.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.9.0.

\\r\\n\\r\\n

We are using an experimental compiler provided by JetBrains.

\"], \"rust\": [\"Rust\", \"

Rust 1.79.0. Your code will be compiled with opt-level 2.

\\r\\n\\r\\n

Supports rand v0.8 and regex\\u00a0v1 from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.2.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 20.10.0.

\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Racket CS v8.11

\\r\\n\\r\\n

Using #lang racket

\\r\\n\\r\\n

Required data/gvector data/queue data/order data/heap automatically for your convenience

\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 26\"], \"elixir\": [\"Elixir\", \"Elixir 1.15 with Erlang/OTP 26\"], \"dart\": [\"Dart\", \"

Dart 3.2. You may use package collection

\\r\\n\\r\\n

Your code will be run directly without compiling

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }