question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
spiral-matrix-ii | Java | Simulation | Beats 100% | 15 lines | java-simulation-beats-100-15-lines-by-ju-a6a9 | Complexity\n- Time complexity: O(n^2)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n^2)\n Add your space complexity here, e.g. O(n) \n\n | judgementdey | NORMAL | 2023-05-10T05:48:38.589696+00:00 | 2023-05-10T20:29:29.715228+00:00 | 393 | false | # Complexity\n- Time complexity: $$O(n^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n^2)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int[][] generateMatrix(int n) {\n int x = 0, y = -1, cnt = 1;\n var xMoves = new i... | 5 | 0 | ['Matrix', 'Simulation', 'Java'] | 0 |
spiral-matrix-ii | Easy JAVA Code || Beats 100% | easy-java-code-beats-100-by-ayush__jain-drsi | \nclass Solution {\n public int[][] generateMatrix(int n) {\n int[][] ans = new int[n][n];\n //right -> bottom -> left -> top\n int top | Ayush__jain | NORMAL | 2023-05-10T04:10:50.831911+00:00 | 2023-05-10T04:12:52.998948+00:00 | 865 | false | ```\nclass Solution {\n public int[][] generateMatrix(int n) {\n int[][] ans = new int[n][n];\n //right -> bottom -> left -> top\n int top = 0, left = 0;\n int bottom = n-1, right = n-1;\n int k = 1;\n while(top<=bottom && left<=right){\n //right\n for(... | 5 | 0 | ['Array', 'Matrix', 'Java'] | 0 |
spiral-matrix-ii | Javascript || Easy to understand || Intuition Explained | javascript-easy-to-understand-intuition-2kepc | Intuition -> This problem is similar to Spiral matrix I in which we take 4 pointers and move them as per the condition, in this problem the pointer part in same | jitendra_jaria | NORMAL | 2022-04-13T05:36:33.665993+00:00 | 2022-04-13T05:45:54.222818+00:00 | 732 | false | **Intuition** -> This problem is similar to [Spiral matrix I](https://leetcode.com/problems/spiral-matrix/) in which we take 4 pointers and move them as per the condition, in this problem the pointer part in same only thing that has been changed is how many times the outer loop will run and how efficiently we can manag... | 5 | 0 | ['Array', 'JavaScript'] | 0 |
spiral-matrix-ii | Java | Simple | Reuse LC 54 | java-simple-reuse-lc-54-by-prashant404-aoyz | Idea: \n This question is near identical to LC 54. Spiral Matrix and the solution can be reused. \n Fill the matrix in this order: First Row \u279D Last Column | prashant404 | NORMAL | 2022-04-13T01:50:36.781158+00:00 | 2023-05-10T18:02:26.662355+00:00 | 589 | false | **Idea:** \n* This question is near identical to [LC 54. Spiral Matrix](https://leetcode.com/problems/spiral-matrix/discuss/3502622/Java-or-Simple-or-Explained) and the solution can be reused. \n* Fill the matrix in this order: First Row \u279D Last Column \u279D Last Row \u279D First Column\n* Repeat till a cross-over... | 5 | 0 | ['Java'] | 0 |
spiral-matrix-ii | [Golang] simple solution with rotation | golang-simple-solution-with-rotation-by-o88ss | We basically need to walk through the matrix, changing direction if we either a) got out of bounds, or b) got previously filled cell.\n\ngo\nfunc generateMatrix | oizg | NORMAL | 2022-04-13T00:10:25.697707+00:00 | 2022-04-13T00:10:25.697750+00:00 | 626 | false | We basically need to walk through the matrix, changing direction if we either a) got out of bounds, or b) got previously filled cell.\n\n```go\nfunc generateMatrix(n int) [][]int {\n res := make([][]int, n)\n for i := 0; i < n; i++ { res[i] = make([]int, n) }\n \n dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-... | 5 | 0 | ['Go'] | 0 |
spiral-matrix-ii | Spiral Matrix II | Python | O(n2) | Simple Offset | spiral-matrix-ii-python-on2-simple-offse-csx1 | The following solution keeps track of an x-y position (location in the resulting array) and an offset (used to move spiral toward the center).\n\n\nclass Soluti | alpeople | NORMAL | 2020-12-08T00:59:43.747746+00:00 | 2020-12-08T00:59:52.977344+00:00 | 583 | false | The following solution keeps track of an x-y position (location in the resulting array) and an offset (used to move spiral toward the center).\n\n```\nclass Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n spiral = [[0]*n for i in range(n)]\n x, y, off = 0, 0, 0\n \n for... | 5 | 0 | ['Python'] | 1 |
spiral-matrix-ii | [C++] Simulation: self-explained without comment | c-simulation-self-explained-without-comm-j3pb | \nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> ans(n, vector<int>(n));\n int begX = 0, endX = | codedayday | NORMAL | 2020-12-07T16:20:46.558703+00:00 | 2020-12-07T16:20:46.558736+00:00 | 235 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> ans(n, vector<int>(n));\n int begX = 0, endX = n - 1;\n int begY = 0, endY = n - 1;\n int counter = 0;\n while(true){\n for(int j = begX; j <= endX; j++) ans[begY][j] = ++c... | 5 | 0 | [] | 1 |
spiral-matrix-ii | C++ #minimalizm | c-minimalizm-by-votrubac-7om9 | Track the direction (left, down, right, and up). When your next position is out of bouds, or there is already a number, we change the direction.\n\ncpp\nint ds[ | votrubac | NORMAL | 2020-03-27T06:31:20.820066+00:00 | 2020-03-27T07:05:23.718595+00:00 | 147 | false | Track the direction (left, down, right, and up). When your next position is out of bouds, or there is already a number, we change the direction.\n\n```cpp\nint ds[4][2] = {{0,1}, {1,0}, {0,-1}, {-1,0}};\nvector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> res(n, vector<int>(n));\n for (auto i = 0, j... | 5 | 0 | [] | 0 |
spiral-matrix-ii | Clean JavaScript solution | clean-javascript-solution-by-hongbo-miao-w0tx | Reference from a similar question https://leetcode.com/problems/spiral-matrix/discuss/20573/A-concise-C++-implementation-based-on-Directions\n\n\n// When traver | hongbo-miao | NORMAL | 2019-10-19T20:48:27.234523+00:00 | 2020-09-02T15:44:16.270200+00:00 | 1,507 | false | Reference from a similar question https://leetcode.com/problems/spiral-matrix/discuss/20573/A-concise-C++-implementation-based-on-Directions\n\n```\n// When traversing the matrix in the spiral order, at any time we follow one out of the following four directions:\n// RIGHT DOWN LEFT UP. Suppose we are working on a 5 x ... | 5 | 0 | ['JavaScript'] | 3 |
spiral-matrix-ii | (( Swift )) Beating All ((Linear Solution)) [[KewL LewpZ]] ((Hacker Rank)) Scared Money | swift-beating-all-linear-solution-kewl-l-64fg | \nclass Solution {\n func generateMatrix(_ n: Int) -> [[Int]] {\n \n var m = [[Int]](repeating: [Int](repeating: 0, count: n), count: n)\n | nraptis | NORMAL | 2019-06-11T03:39:21.320626+00:00 | 2019-06-11T03:39:21.320665+00:00 | 146 | false | ```\nclass Solution {\n func generateMatrix(_ n: Int) -> [[Int]] {\n \n var m = [[Int]](repeating: [Int](repeating: 0, count: n), count: n)\n \n var l = 0, r = n - 1, t = 0, b = n - 1, x = 0, y = 0, val = 1\n \n while t <= b {\n \n x = l; y = t\n ... | 5 | 1 | [] | 0 |
spiral-matrix-ii | A Better Solution than Switching Directions | a-better-solution-than-switching-directi-8n55 | Rotate the Matrix is more easier :)\n\n\tpublic class Solution {\n\t\tint[][] step = {{0,1},{1,0},{0,-1},{-1,0}};\n\t\tint cnt = 1;\n\t\tvoid vortex(int[][] res | blue_y | NORMAL | 2014-09-26T09:59:36+00:00 | 2014-09-26T09:59:36+00:00 | 1,836 | false | Rotate the Matrix is more easier :)\n\n\tpublic class Solution {\n\t\tint[][] step = {{0,1},{1,0},{0,-1},{-1,0}};\n\t\tint cnt = 1;\n\t\tvoid vortex(int[][] res, int len, int wid, int x, int y, int sg){\n\t\t\tif(len == 0) return;\n\t\t\tfor(int i = 0; i < len; ++i){\n\t\t\t\tx += step[sg][0];\n\t\t\t\ty += step[sg][1]... | 5 | 0 | ['Java'] | 2 |
spiral-matrix-ii | ✅100%RUNTIME | 95.96%MEMORY | Easiest & Standard Intuition and traversalal | TimeO(N^2) | Space O(1) | 100runtime-9596memory-easiest-standard-i-thz8 | optimal solution for the spiral traversal in a matrix🧠 IntuitionWe need to generate an n x n matrix filled with numbers from 1 to n^2 in a spiral order 🌀.To ach | himanshukansal101 | NORMAL | 2025-02-27T21:15:24.740878+00:00 | 2025-02-27T21:15:24.740878+00:00 | 481 | false | # optimal solution for the spiral traversal in a matrix
# 🧠 Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to generate an n x n matrix filled with numbers from 1 to n^2 in a spiral order 🌀.
To achieve this, we can divide the problem into four movement directions:
1️⃣ Left → Rig... | 4 | 0 | ['C++'] | 0 |
spiral-matrix-ii | 0 ms || Beats 100% || Easy to understand || C++ | 0-ms-beats-100-easy-to-understand-c-by-h-swzv | Intuition\n Describe your first thoughts on how to solve this problem. \nThe generateMatrix function creates an n x n matrix filled with numbers from 1 to n^2 i | hardik-chauhan | NORMAL | 2024-08-08T10:54:05.080468+00:00 | 2024-08-08T10:54:05.080487+00:00 | 285 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe generateMatrix function creates an n x n matrix filled with numbers from 1 to n^2 in a spiral order. It starts at the top-left corner of the matrix and moves right across the top row, then down the rightmost column, then left across t... | 4 | 0 | ['C++'] | 1 |
spiral-matrix-ii | Spiral Matrix II - Java Solution (Beats 100%) | spiral-matrix-ii-java-solution-beats-100-oe09 | Intuition\n Describe your first thoughts on how to solve this problem. \nMy intuition for generating the matrix in spiral order involves systematically filling | Naveen-NaS | NORMAL | 2024-01-06T15:41:27.528244+00:00 | 2024-01-17T12:22:37.269722+00:00 | 573 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy intuition for generating the matrix in spiral order involves systematically filling each side of the current spiral boundary by iteratively moving in the top, right, bottom, and left directions. The approach ensures that each element i... | 4 | 0 | ['Java'] | 3 |
spiral-matrix-ii | Beats 100% || Easiest Approach | beats-100-easiest-approach-by-rishi_2330-s9fk | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Rishi_2330 | NORMAL | 2024-01-01T18:03:35.361353+00:00 | 2024-01-01T18:05:07.333535+00:00 | 31 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n*n)\n\n- Space complexity:\nO(n*n)\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n ... | 4 | 0 | ['Math', 'Matrix', 'C++'] | 1 |
spiral-matrix-ii | Best O(N^2) Solution | best-on2-solution-by-kumar21ayush03-k9u6 | Approach\nTraversal\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n vector<vector<int>> gen | kumar21ayush03 | NORMAL | 2023-09-24T09:55:09.972363+00:00 | 2023-09-24T09:55:09.972383+00:00 | 842 | false | # Approach\nTraversal\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> mat (n, vector<int>(n));\n int num = 1;\n int r1 = 0, r2 = n - 1, c1 = n - 1, c2 = ... | 4 | 0 | ['C++'] | 1 |
spiral-matrix-ii | beats 100% || beginner friendly | beats-100-beginner-friendly-by-wtfcoder-oqt5 | Intuition\n Describe your first thoughts on how to solve this problem. \ntraverse through matrix hit right when you hit a visited node or go out of limits. brea | wtfcoder | NORMAL | 2023-06-18T11:59:15.259891+00:00 | 2023-06-18T11:59:15.259914+00:00 | 25 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ntraverse through matrix hit right when you hit a visited node or go out of limits. break when count goes to n^2 +1.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nmaintain n*n matrix. change direction when you hit... | 4 | 0 | ['C++'] | 0 |
spiral-matrix-ii | C++|| Beats 100% || Easy || Like if it helps you || Fast || | c-beats-100-easy-like-if-it-helps-you-fa-23re | Intuition\n Describe your first thoughts on how to solve this problem. \nMatrix Simulation type question.\n# Approach\n Describe your approach to solving the pr | iamshreyash28 | NORMAL | 2023-05-10T11:13:57.888377+00:00 | 2023-05-13T20:46:05.558293+00:00 | 25 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMatrix Simulation type question.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n#Define a N*N vector initiated with value 0. And then move forward in the matrix in the directions of top->bottom, right->left, left->r... | 4 | 0 | ['C++'] | 0 |
spiral-matrix-ii | [Kotlin][2D Array] EASY solution with GREAT explanation! | kotlin2d-array-easy-solution-with-great-xjh3d | Intuition\nI solved this task yesterday lol, i just need to advance it :)\n\n# Approach\nSo, we need to initialize n * n matrix/grid with zeroes on each positio | 4wl2d | NORMAL | 2023-05-10T09:41:40.150180+00:00 | 2023-05-10T09:42:00.791482+00:00 | 1,182 | false | # Intuition\nI solved this task yesterday lol, i just need to advance it :)\n\n# Approach\nSo, we need to initialize `n * n` matrix/grid with zeroes on each position(which is our result, we need to edit this matrix/grid)\n\nNext we need to declare `direction` variable, which is equal to `\'R\'`, because start direction... | 4 | 0 | ['Array', 'Matrix', 'Kotlin'] | 0 |
spiral-matrix-ii | C++ || EASY TO UNDERSTAND || MATRIX | c-easy-to-understand-matrix-by-ganeshkum-ripp | sr : starting of row\ner : end of row\nsc : starting of row\nec : end of columns\n\n{first traverse first row increment starting row by one\nsecond traversal fo | ganeshkumawat8740 | NORMAL | 2023-05-10T05:33:53.757124+00:00 | 2023-05-10T05:33:53.757162+00:00 | 1,188 | false | sr : starting of row\ner : end of row\nsc : starting of row\nec : end of columns\n\n{first traverse first row increment starting row by one\nsecond traversal for insert last columns decrement last columns by 1\nthird time insert last row decrement last row by one\nfourth time insert first row increment 1st column by ro... | 4 | 0 | ['Array', 'Matrix', 'C++'] | 0 |
spiral-matrix-ii | 🔥C++ ✅✅ Best Solution ( 🔥 O(N*N) 🔥 ) Easy to Understand💯💯 ⬆⬆⬆ | c-best-solution-onn-easy-to-understand-b-kq7m | # Intuition \n\n\n# Approach\n\nHere we have total nn element for making nn matrix in spiral order.\nSo we can follow this 4 step\n\nStep-1 : printing the sta | Sandipan58 | NORMAL | 2023-05-10T02:23:14.127816+00:00 | 2023-05-10T02:23:14.127899+00:00 | 777 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHere we have total n*n element for making n*n matrix in spiral order.\nSo we can follow this **4 step**\n\n***Step-1*** : printing the starting row\n***Step-2... | 4 | 0 | ['Array', 'Depth-First Search', 'Matrix', 'Interactive', 'C++'] | 0 |
spiral-matrix-ii | ✔ [C++ / Python3] Solution | Clean & Concise | c-python3-solution-clean-concise-by-saty-d5pb | Complexity\n- Time complexity: O(n^2)\n- Space complexity: O(n^2)\n\n# Code\ncpp []\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\ | satyam2001 | NORMAL | 2023-01-30T12:18:26.088214+00:00 | 2023-01-30T12:18:59.135044+00:00 | 691 | false | # Complexity\n- Time complexity: $$O(n^2)$$\n- Space complexity: $$O(n^2)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> M(n, vector<int>(n));\n int val = 1;\n for(int i = 0; i < (n + 1) / 2; i++) {\n for(int j = i... | 4 | 0 | ['Matrix', 'Python', 'C++', 'Python3'] | 1 |
spiral-matrix-ii | ✔ C++ simple and concise solution || 0ms || 100% faster | c-simple-and-concise-solution-0ms-100-fa-rv6l | \nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> ans(n,vector<int>(n,0));\n int strow=0,endrow=n | vikas__6511 | NORMAL | 2022-08-08T14:18:57.572277+00:00 | 2022-08-08T14:18:57.572302+00:00 | 59 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> ans(n,vector<int>(n,0));\n int strow=0,endrow=n-1,stcol=0,endcol=n-1;\n int val=1;\n while(strow<=endrow && stcol<=endcol ){\n //first row\n for(int i= stcol; i<=en... | 4 | 0 | ['C', 'Simulation'] | 0 |
spiral-matrix-ii | Python | Beginner Friendly | Similar to Spiral Matrix - 1 | python-beginner-friendly-similar-to-spir-k1ex | Please upvote if you like the post\n\n\n class Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n matrix = [[0] * n for _ in rang | pvrittesh | NORMAL | 2022-05-30T05:22:33.900534+00:00 | 2022-05-30T05:22:49.075387+00:00 | 211 | false | Please upvote if you like the post\n\n```\n class Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n matrix = [[0] * n for _ in range(n)]\n matrix_size = n * n\n up = left = value = 0\n right = n - 1\n down = n - 1\n \n while value != matrix_size:... | 4 | 0 | ['Python'] | 0 |
spiral-matrix-ii | Python optimized spiral traversal in a few lines explained | python-optimized-spiral-traversal-in-a-f-yblu | We fill the matrix following a spiral pattern, changing direction when going outside of the matrix or when encountering a non-zero value.\nUsing nexton an itert | reupiey | NORMAL | 2022-04-13T08:08:43.815109+00:00 | 2022-04-13T08:08:43.815139+00:00 | 341 | false | We fill the matrix following a spiral pattern, changing direction when going outside of the matrix or when encountering a non-zero value.\nUsing `next`on an `itertools.cycle` is far easier to read than using modulos IMO.\n\n```python\nclass Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n ma... | 4 | 0 | ['Python'] | 1 |
spiral-matrix-ii | ✅ [C] || 100% || Simple Solution || O(n^2) | c-100-simple-solution-on2-by-bezlant-nsmn | \nint** generateMatrix(int n, int* returnSize, int** returnColumnSizes)\n{\n *returnSize = n;\n *returnColumnSizes = malloc(sizeof(int) * n);\n int **r | bezlant | NORMAL | 2022-04-13T05:41:07.820412+00:00 | 2022-04-13T05:41:07.820440+00:00 | 328 | false | ```\nint** generateMatrix(int n, int* returnSize, int** returnColumnSizes)\n{\n *returnSize = n;\n *returnColumnSizes = malloc(sizeof(int) * n);\n int **res = malloc(sizeof(int*) * n);\n for (int i = 0; i < n; i++) {\n (*returnColumnSizes)[i] = n;\n res[i] = malloc(sizeof(int) * n);\n }\n ... | 4 | 0 | ['C'] | 2 |
spiral-matrix-ii | C++|| 0ms|| 100% faster|| Easy and simple | c-0ms-100-faster-easy-and-simple-by-aash-6m6h | Here, we just have to understand the trend of matrix.\nLike how we are filling the matrix.\nfirst we are doing first column to last column,\nthen first row +1 t | aashuchoudhary52 | NORMAL | 2022-04-13T04:21:35.098751+00:00 | 2022-04-13T04:21:35.098794+00:00 | 44 | false | Here, we just have to understand the trend of matrix.\nLike how we are filling the matrix.\nfirst we are doing first column to last column,\nthen first row +1 to last row then again last column to first column and same with last row to first row...this way we can approach this question.\n\nyou will have more understan... | 4 | 1 | ['Matrix'] | 0 |
spiral-matrix-ii | Easy C++ Solution 100% fast | easy-c-solution-100-fast-by-apurvat18-cwid | ```\nclass Solution {\npublic:\n vector> generateMatrix(int n) {\n // Defining the boundaries of the matrix.\n int top = 0, bottom = n-1, right | apurvat18 | NORMAL | 2022-04-13T03:16:19.903080+00:00 | 2022-04-13T03:17:09.007327+00:00 | 33 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n // Defining the boundaries of the matrix.\n int top = 0, bottom = n-1, right = n-1, left = 0;\n // Defining the direction in which the array is to be traversed.\n int dir = 1, val = 1;\n vector<vector<in... | 4 | 0 | [] | 0 |
spiral-matrix-ii | Faster || Easy To Understand || C++ Code | faster-easy-to-understand-c-code-by-__kr-shs7 | Time Complexity : O(N * N)\n Space Complexity : O(1), because space taken for outputing the result doesn\'t count as space complexity*\n\n\nclass Solution {\npu | __KR_SHANU_IITG | NORMAL | 2022-04-13T01:48:47.146737+00:00 | 2022-04-13T01:48:47.146781+00:00 | 319 | false | * ***Time Complexity : O(N * N)***\n* ***Space Complexity : O(1), because space taken for outputing the result doesn\'t count as space complexity***\n\n```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n \n vector<vector<int>> mat(n, vector<int> (n, 0));\n \n in... | 4 | 0 | ['C', 'Matrix'] | 0 |
spiral-matrix-ii | Easy-Understanding Java Solution | easy-understanding-java-solution-by-lmeb-7oiq | ```\nprivate int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };\n\tprivate int direction = 0;\n\tpublic int[][] generateMatrix(int n) {\n\t\tint[][] r | lmebo | NORMAL | 2022-03-01T19:01:15.022627+00:00 | 2022-03-01T19:01:15.022662+00:00 | 103 | false | ```\nprivate int[][] dirs = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };\n\tprivate int direction = 0;\n\tpublic int[][] generateMatrix(int n) {\n\t\tint[][] res = new int[n][n];\n\t\tres[0][0] = 1;\n\t\tint row = 0;\n\t\tint col = 0;\n\t\tint count = 2;\n\t\twhile (count <= n * n) {\n\t\t\tif (canGo(row + dirs[direct... | 4 | 1 | ['Java'] | 0 |
spiral-matrix-ii | Python solution | python-solution-by-lexieeeee-cy2x | Need to be careful for the corner element traversal. \n```\nclass Solution(object):\n def generateMatrix(self, n):\n top, bottom, left, right = 0, n-1 | lexieeeee | NORMAL | 2022-02-13T06:09:25.279051+00:00 | 2022-02-13T06:09:25.279083+00:00 | 184 | false | Need to be careful for the corner element traversal. \n```\nclass Solution(object):\n def generateMatrix(self, n):\n top, bottom, left, right = 0, n-1, 0, n-1\n num = 1 \n if not n:\n return \n matrix = [[0]*n for _ in xrange(n)]\n while left <= right and top <= bottom:\... | 4 | 0 | ['Python'] | 0 |
spiral-matrix-ii | c++ 100 % | Each and Every steps Explained | Easy | Efficient | c-100-each-and-every-steps-explained-eas-amn3 | class Solution {\npublic:\n vector> generateMatrix(int n) {\n \n vector> a(n,vector(n,0));\n int val=1,r=0,c=0,m=n,i;\n \n \n / | ConsistentCoder27 | NORMAL | 2021-10-03T11:27:28.808938+00:00 | 2021-10-03T11:41:37.623626+00:00 | 343 | false | class Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n \n vector<vector<int>> a(n,vector<int>(n,0));\n int val=1,r=0,c=0,m=n,i;\n \n \n /* r - starting row index\n m - ending row index\n c - starting column index\n n - ending column index\n ... | 4 | 0 | ['C', 'Matrix', 'C++'] | 0 |
spiral-matrix-ii | [faster than 100.00%][beginner friendly][easy understanding] | faster-than-10000beginner-friendlyeasy-u-xfw5 | \n//Runtime: 0 ms, faster than 100.00%\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n int cnt=0;\n int rs=0,re=n-1; | rajat_gupta_ | NORMAL | 2020-12-12T13:57:01.708157+00:00 | 2020-12-12T13:57:01.708193+00:00 | 340 | false | ```\n//Runtime: 0 ms, faster than 100.00%\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n int cnt=0;\n int rs=0,re=n-1;\n int cs=0,ce=n-1;\n int j=1;\n vector<vector<int>>res(n,vector<int>(n,0));\n while(rs <= re && cs <= ce){\n for(int ... | 4 | 0 | ['C', 'C++'] | 1 |
spiral-matrix-ii | [c++] "0ms" solution, easy to understand | c-0ms-solution-easy-to-understand-by-its-uuiv | \'\'\'\nclass Solution {\npublic:\n vector> generateMatrix(int n) {\n vector>v(n,vector(n));\n int count=0;\n for(int i=0;i<n;i++)\n | its_pro_deep | NORMAL | 2020-12-08T06:44:33.037658+00:00 | 2020-12-08T06:44:33.037686+00:00 | 115 | false | \'\'\'\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>>v(n,vector<int>(n));\n int count=0;\n for(int i=0;i<n;i++)\n {\n for(int j=i;j<n-i;j++){ //left to right\n v[i][j]=++count;\n }\n for(int ... | 4 | 0 | [] | 1 |
spiral-matrix-ii | Python | python-by-gsan-swki | We do as told and construct the matrix via traversing the spiral.\n\npython\nclass Solution:\n def generateMatrix(self, n):\n ans = [[0]*n for _ in ra | gsan | NORMAL | 2020-12-07T08:21:53.692394+00:00 | 2020-12-07T08:22:19.695357+00:00 | 437 | false | We do as told and construct the matrix via traversing the spiral.\n\n```python\nclass Solution:\n def generateMatrix(self, n):\n ans = [[0]*n for _ in range(n)]\n x = y = j = 0\n dirs = [(0,1),(1,0),(0,-1),(-1,0)]\n for i in range(1,n**2+1):\n ans[x][y] = i\n dx, dy ... | 4 | 0 | [] | 0 |
spiral-matrix-ii | JAVA - My EASY Solution | java-my-easy-solution-by-anubhavjindal-wdhp | \npublic int[][] generateMatrix(int n) {\n\tint sol[][] = new int[n][n], count = 1;\n\tint rowMin = 0, colMin = 0, rowMax = n-1, colMax = n-1;\n\twhile(rowMin < | anubhavjindal | NORMAL | 2020-02-14T04:10:16.095357+00:00 | 2020-02-14T04:10:16.095407+00:00 | 168 | false | ```\npublic int[][] generateMatrix(int n) {\n\tint sol[][] = new int[n][n], count = 1;\n\tint rowMin = 0, colMin = 0, rowMax = n-1, colMax = n-1;\n\twhile(rowMin <= rowMax && colMin <= colMax) {\n\t\tfor(int i = colMin; i <= colMax; i++) \n\t\t\tsol[rowMin][i] = count++;\n\t\trowMin++;\n\t\tfor(int i = rowMin; i <= row... | 4 | 1 | [] | 2 |
spiral-matrix-ii | Javascript solution beats 60% | javascript-solution-beats-60-by-ggoranta-saew | Here is my solution to solve this problem in JS.\n\n\n\n/* startColumn endColumn\n startRow 1 2 3\n 4 | ggorantala | NORMAL | 2019-10-01T05:21:48.635182+00:00 | 2019-10-01T05:39:58.645363+00:00 | 174 | false | Here is my solution to solve this problem in JS.\n\n```\n\n/* startColumn endColumn\n startRow 1 2 3\n 4 5 6\n endRow 7 8 9\n */\nconst spiral = n => {\n const results = [];\n\n for (let i = 0; i < n; i++) {\n resu... | 4 | 0 | ['JavaScript'] | 0 |
spiral-matrix-ii | C++ clean! | c-clean-by-raunit-dnmx | \nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) \n {\n if(!n)\n return {};\n int rowStart = 0, rowEnd = n | raunit | NORMAL | 2019-09-17T19:18:52.518856+00:00 | 2019-09-24T19:29:07.925131+00:00 | 241 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) \n {\n if(!n)\n return {};\n int rowStart = 0, rowEnd = n - 1, colStart = 0, colEnd = n - 1;\n int count = 1;\n vector<vector<int>> result(n, vector<int>(n));\n while(count <= n * n)\n {... | 4 | 0 | [] | 0 |
spiral-matrix-ii | {{{ C++ }}} 100% Beat ((Using Smart Loops + Smart Memories)) [[ SUPER HACK VIDEO RENTAL ]] O(N) | c-100-beat-using-smart-loops-smart-memor-l6ga | \nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n \n vector<vector<int>> aResult(n, vector<int>(n));\n \n | nraptis | NORMAL | 2019-06-10T05:05:19.051896+00:00 | 2019-06-10T05:05:19.051927+00:00 | 108 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> generateMatrix(int n) {\n \n vector<vector<int>> aResult(n, vector<int>(n));\n \n int _U = 0, _R = n - 1, _D = n - 1, _L = 0, x = 0, y = 0, VAL = 1;\n \n while (_L <= _R) {\n \n x = _L; y = _U;\n ... | 4 | 1 | [] | 0 |
spiral-matrix-ii | Ruby Solution | ruby-solution-by-renyijiu-gfps | the solution learns from the discuss, here is the code:\n\n```ruby\n\ndef generate_matrix(n)\n arr = Array.new(n){ Array.new(n) }\n \n i, j, di, dj = 0, 0, 0 | renyijiu | NORMAL | 2019-02-17T08:00:00.617889+00:00 | 2019-02-17T08:00:00.617965+00:00 | 146 | false | the solution learns from the discuss, here is the code:\n\n```ruby\n\ndef generate_matrix(n)\n arr = Array.new(n){ Array.new(n) }\n \n i, j, di, dj = 0, 0, 0, 1\n (0...n*n).each do |k|\n arr[i][j] = k + 1 \n \n di, dj = dj, -di if arr[(i+di)%n][(j+dj)%n]\n \n i += di\n j += dj\n end\n \n arr\n... | 4 | 0 | [] | 1 |
spiral-matrix-ii | C++ template for Spiral Matrix & Spiral Matrix II | c-template-for-spiral-matrix-spiral-matr-xvsk | Spiral Matrix II code:\n\n class Solution {\n public:\n vector> generateMatrix(int n) {\n vector> result(n, vector(n, 0));\n | rainbowsecret | NORMAL | 2016-02-22T02:37:51+00:00 | 2016-02-22T02:37:51+00:00 | 883 | false | Spiral Matrix II code:\n\n class Solution {\n public:\n vector<vector<int>> generateMatrix(int n) {\n vector<vector<int>> result(n, vector<int>(n, 0));\n int l=0, r=n-1, u=0, d=n-1;\n int k=1;\n while(true){\n for(int i=l; i<=r; i++) result[u][i]=... | 4 | 0 | [] | 0 |
spiral-matrix-ii | 🌟 Spiral Matrix II | Elegant Simulation for Clockwise Traversal 🌀 | spiral-matrix-ii-elegant-simulation-for-is70m | Intuition:
To generate a spiral matrix, we fill an n×n grid in a spiral order starting at the top-left corner and moving clockwise. The key is to manage the dir | rshohruh | NORMAL | 2025-01-03T03:02:53.525496+00:00 | 2025-01-03T05:07:08.328424+00:00 | 234 | false | **Intuition:**
To generate a spiral matrix, we fill an $$n \times n$$ grid in a **spiral order** starting at the top-left corner and moving clockwise. The key is to manage the direction of movement dynamically when we encounter boundaries or already-filled cells.
---
**Approach:**
1. **Matrix Initialization:** Cr... | 3 | 0 | ['Dynamic Programming', 'Matrix', 'Simulation', 'C++'] | 0 |
spiral-matrix-ii | Easy JS.sol 👀 | easy-jssol-by-starboy609-pnxm | Intuition\n Describe your first thoughts on how to solve this problem. \nEasy && Simple \n\n# Approach\n Describe your approach to solving the problem. \nStep 1 | starboy609 | NORMAL | 2024-09-09T16:00:29.467783+00:00 | 2024-09-09T16:00:29.467820+00:00 | 206 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nEasy && Simple \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStep 1 : Create a 2D Array of length (m * n) and initialize the cells with \'-1\'\n```javascript []\nconst matrix = Array.from({length : m}, () => Arr... | 3 | 0 | ['JavaScript'] | 1 |
longest-increasing-subsequence-ii | [Python] Explanation with pictures, Segment Tree | python-explanation-with-pictures-segment-967n | We store the longest increasing subsequence ended by each number in array LIS (1-indexed). Let\'s say the input nums = [4,2,4,5,9]. Initailly, LIS[i] = 0 as we | Bakerston | NORMAL | 2022-09-11T04:02:18.463792+00:00 | 2022-09-11T04:53:08.721074+00:00 | 15,199 | false | We store the longest increasing subsequence ended by each number in array `LIS` **(1-indexed)**. Let\'s say the input `nums = [4,2,4,5,9]`. Initailly, `LIS[i] = 0` as we haven\'t add any number yet.\n\n\n\n> Th... | 215 | 1 | [] | 26 |
longest-increasing-subsequence-ii | [C++] Segment tree with illustration/explanation | c-segment-tree-with-illustrationexplanat-73bn | We use a segment tree to keep track of the max length of all the valid numbers that can come before this one.\nFor example, if k = 3 and the current number is 5 | ayman_eltemmsahy | NORMAL | 2022-09-11T04:00:29.653699+00:00 | 2022-09-13T18:28:49.161755+00:00 | 9,904 | false | We use a segment tree to keep track of the max length of all the valid numbers that can come before this one.\nFor example, if `k = 3` and the current number is `5`, then the valid numbers before it are `2, 3, 4`. We query the segment tree in `O(logM)` to get the longest sequence out of the 3 numbers and we add `1` to ... | 122 | 1 | ['Tree', 'C'] | 13 |
longest-increasing-subsequence-ii | Segment Tree || C++ | segment-tree-c-by-yowaimo-7bg5 | The number that can be behind number X is in the range [X - k, X - 1], and the best value for the LIS than ends with X is the maximum value in that range + 1, 1 | Yowaimo | NORMAL | 2022-09-11T04:00:27.942989+00:00 | 2022-09-11T04:06:34.198273+00:00 | 6,335 | false | The number that can be behind number **X** is in the range **[X - k, X - 1]**, and the best value for the LIS than ends with **X** is the maximum value in that range + 1, 1 for the current element **X**.\nSo we use a segment tree to find the maximum value in the range and update the current value for further use and ca... | 49 | 2 | ['Tree', 'C'] | 13 |
longest-increasing-subsequence-ii | [C++/Java] Segment Tree (Max Range Query) | cjava-segment-tree-max-range-query-by-st-8qfr | Strategy\nFor every element A[i], we check the range [A[i]-k, A[i]-1] for the current best length and add 1. \nThe problem here is that, we also need to update | Student2091 | NORMAL | 2022-09-11T04:02:38.631435+00:00 | 2022-09-12T08:47:57.367203+00:00 | 4,713 | false | #### Strategy\nFor every element `A[i]`, we check the range `[A[i]-k, A[i]-1]` for the current best length and add 1. \nThe problem here is that, we also need to update the max and it is not a prefix range from 0 to some number, so fenwick tree won\'t work.\n\nI figure segment tree is good for this because it supports ... | 25 | 2 | ['Tree', 'C', 'Java'] | 8 |
longest-increasing-subsequence-ii | Segment Tree | segment-tree-by-votrubac-9nff | Can be solved by DP, where dp[nums[i]] = max(dp[1] ... dp[nums[i] - 1]).\n\nHowever, this leads to a quadratic solution, and we need to do it in O(n log n) base | votrubac | NORMAL | 2022-09-15T06:47:58.021424+00:00 | 2022-09-17T11:43:14.058142+00:00 | 2,389 | false | Can be solved by DP, where `dp[nums[i]] = max(dp[1] ... dp[nums[i] - 1])`.\n\nHowever, this leads to a quadratic solution, and we need to do it in O(n log n) based on the problem constraints. \n\nI wasted a lot of time trying to adapt a monostack LIS solution, but only to realize that we must use DP. \n\nWe just need t... | 16 | 1 | ['C'] | 2 |
longest-increasing-subsequence-ii | Clean Java | clean-java-by-rexue70-2ekc | It is similar to https://leetcode.com/problems/count-of-smaller-numbers-after-self/\nand https://leetcode.com/problems/range-sum-query-mutable/\nIf you did ques | rexue70 | NORMAL | 2022-09-11T05:04:31.266268+00:00 | 2022-09-11T05:04:31.266305+00:00 | 3,152 | false | It is similar to https://leetcode.com/problems/count-of-smaller-numbers-after-self/\nand https://leetcode.com/problems/range-sum-query-mutable/\nIf you did question above, it should be similar to find range sum/max/avg\n```\nclass Solution {\n public int lengthOfLIS(int[] nums, int k) {\n SegmentTree root = n... | 15 | 0 | ['Tree', 'Java'] | 4 |
longest-increasing-subsequence-ii | Segemet Tree || C++ || Max Range Query | segemet-tree-c-max-range-query-by-surajp-ep4b | \n\n\tclass SegmentTree{\n\t\tpublic:\n vector<int> t;\n int n;\n \n SegmentTree(int n){\n this->n = n;\n t.as | surajpatel | NORMAL | 2022-09-11T04:03:26.047112+00:00 | 2022-09-11T04:10:22.007987+00:00 | 2,120 | false | ```\n\n\tclass SegmentTree{\n\t\tpublic:\n vector<int> t;\n int n;\n \n SegmentTree(int n){\n this->n = n;\n t.assign(4*n,0);\n }\n \n int get(int v,int l,int r,int tl,int tr){\n if(l>r) return 0;\n\n if(l==tl&&tr==r){\n ... | 13 | 1 | ['C'] | 3 |
longest-increasing-subsequence-ii | Approach of this question if asked in Interview. | approach-of-this-question-if-asked-in-in-ra2x | Think of the brute-force solution for this problem: for each element nums[i], you compare it with all previous elements nums[j]. If nums[i] is greater than nums | gaurav621 | NORMAL | 2024-10-21T19:33:04.719079+00:00 | 2024-10-21T19:33:04.719118+00:00 | 836 | false | 1. Think of the brute-force solution for this problem: for each element `nums[i]`, you compare it with all previous elements `nums[j]`. If `nums[i]` is greater than `nums[j]` and the difference between them is less than or equal to k, you can extend the subsequence. To keep track of the length of subsequences, you stor... | 12 | 0 | ['Java'] | 2 |
longest-increasing-subsequence-ii | why can't we use DP | why-cant-we-use-dp-by-dr-blue-cyber-h6pb | I trying my dp approach to solve this problem and hence it took me nearly 1 hour and 15 minutes and able to solve only two problems this time\n\nmy question is | dr-blue-cyber | NORMAL | 2022-09-11T04:25:01.912708+00:00 | 2022-09-11T04:25:01.912745+00:00 | 4,268 | false | I trying my dp approach to solve this problem and hence it took me nearly 1 hour and 15 minutes and able to solve only two problems this time\n\n**my question is that, why aren\'t we use DP here ??** | 9 | 1 | [] | 11 |
longest-increasing-subsequence-ii | C++ | Segment Tree | Cleanest Code | Easiest to understand | c-segment-tree-cleanest-code-easiest-to-gjslz | \nconst int N=100005;\nclass Solution {\npublic:\n vector<int> tree;\n void update(int p,int val,int idx,int l,int r){\n if(p<l or p>r){\n | GeekyBits | NORMAL | 2022-10-30T14:04:15.711596+00:00 | 2022-10-30T14:04:15.711632+00:00 | 1,084 | false | ```\nconst int N=100005;\nclass Solution {\npublic:\n vector<int> tree;\n void update(int p,int val,int idx,int l,int r){\n if(p<l or p>r){\n return;\n }\n if(l==r){\n tree[idx]=val;\n return;\n }\n int mid=(l+r)>>1;\n update(p,val,2*idx+1... | 8 | 0 | [] | 1 |
longest-increasing-subsequence-ii | Python Easy Solution Well Explained | python-easy-solution-well-explained-by-s-1zcp | \n# Iterative Segment Tree\nclass SegmentTree:\n def __init__(self, n, fn):\n self.n = n\n self.fn = fn\n self.tree = [0] * (2 * n)\n | shiv-codes | NORMAL | 2022-09-11T11:12:39.792573+00:00 | 2022-09-13T07:19:17.013200+00:00 | 2,621 | false | ```\n# Iterative Segment Tree\nclass SegmentTree:\n def __init__(self, n, fn):\n self.n = n\n self.fn = fn\n self.tree = [0] * (2 * n)\n \n def query(self, l, r):\n l += self.n\n r += self.n\n res = 0\n while l < r:\n if l & 1:\n res... | 8 | 0 | ['Tree', 'Python', 'Python3'] | 2 |
longest-increasing-subsequence-ii | 💥Memory beats 100% [EXPLAINED] | memory-beats-100-explained-by-r9n-ywo6 | IntuitionFind the longest increasing subsequence where the difference between any two adjacent numbers is at most k. To solve it, we need a data structure that | r9n | NORMAL | 2024-12-24T11:35:38.841635+00:00 | 2024-12-24T11:35:38.841635+00:00 | 257 | false | # Intuition
Find the longest increasing subsequence where the difference between any two adjacent numbers is at most k. To solve it, we need a data structure that can efficiently store and update the maximum length of valid subsequences for each possible number.
# Approach
I've used a segment tree to maintain the maxi... | 7 | 0 | ['Array', 'Divide and Conquer', 'Dynamic Programming', 'Binary Indexed Tree', 'Segment Tree', 'Queue', 'C#'] | 0 |
longest-increasing-subsequence-ii | C++ | Sqrt Decomposition + dp | O(n*sqrt(n)) | explanation | c-sqrt-decomposition-dp-onsqrtn-explanat-hpwj | \nclass Solution {\npublic:\n int lengthOfLIS(vector<int>& nums, int k) {\n int ma=0,dp[318][318];\n memset(dp,0,sizeof(dp));\n for(int | SunGod1223 | NORMAL | 2022-09-11T19:30:47.391604+00:00 | 2022-09-11T19:30:47.391642+00:00 | 1,658 | false | ```\nclass Solution {\npublic:\n int lengthOfLIS(vector<int>& nums, int k) {\n int ma=0,dp[318][318];\n memset(dp,0,sizeof(dp));\n for(int i=0;i<nums.size();++i){\n if(i&&nums[i]==nums[i-1])continue;\n int v=nums[i]-1,t=max(nums[i]-k,0),vh=v%317,vn=v/317,vnh=nums[i]%317,vnn... | 7 | 0 | ['Dynamic Programming', 'C'] | 3 |
longest-increasing-subsequence-ii | Python Binary Search and dp without segment tree | python-binary-search-and-dp-without-segm-2mdz | \nfrom collections import defaultdict\nfrom bisect import insort, bisect_left\nclass Solution:\n def lengthOfLIS(self, nums: List[int], k: int) -> int:\n | chinmaybharti00 | NORMAL | 2022-09-11T06:14:05.782534+00:00 | 2022-09-11T06:16:18.580855+00:00 | 2,925 | false | ```\nfrom collections import defaultdict\nfrom bisect import insort, bisect_left\nclass Solution:\n def lengthOfLIS(self, nums: List[int], k: int) -> int:\n \n dp_tracker = defaultdict(list)\n n = len(nums)\n dp = [1]*n\n max_till_now = 1\n for i in range(n):\n te... | 7 | 1 | ['Binary Search', 'Dynamic Programming', 'Python'] | 3 |
longest-increasing-subsequence-ii | [Python] n^2 solution but AC - numpy | python-n2-solution-but-ac-numpy-by-penol-j4so | I\'m sorry that using numpy \n\npython\nimport numpy as np\nclass Solution:\n def lengthOfLIS(self, nums: List[int], k: int) -> int:\n\t\t# original thinking | penolove | NORMAL | 2022-09-11T04:38:06.826460+00:00 | 2022-09-11T04:45:28.258381+00:00 | 949 | false | I\'m sorry that using numpy \n\n```python\nimport numpy as np\nclass Solution:\n def lengthOfLIS(self, nums: List[int], k: int) -> int:\n\t\t# original thinking, but I know it must TLE\n # len_mapping = {}\n # max_len = 0\n # for i in nums:\n # len_mapping[i] = max([c + 1 for v, c in ... | 7 | 2 | [] | 2 |
longest-increasing-subsequence-ii | [Python] Segment Tree - Clean & Concise | python-segment-tree-clean-concise-by-hie-v3ze | Intuition\n- Use Segment Tree to query maximum of element in range [num-k, num-1], which mean to find the maximum existed Longest Increase Subsequence such that | hiepit | NORMAL | 2022-12-15T00:45:17.123551+00:00 | 2022-12-15T00:46:58.552916+00:00 | 749 | false | # Intuition\n- Use Segment Tree to query maximum of element in range [num-k, num-1], which mean to find the maximum existed Longest Increase Subsequence such that the maximum difference between consecutives element <= k.\n\n# Complexity\n- Time: `O(N * logMAX + MAX)`, where `N <= 10^5` is length of nums array, `MAX <= ... | 5 | 0 | ['Segment Tree', 'Python3'] | 2 |
longest-increasing-subsequence-ii | C++ || Fast || Easy || Segment Tree | c-fast-easy-segment-tree-by-kniccknacc-nmo9 | Link to the submission stats:\nhttps://leetcode.com/submissions/detail/797375784/\n\n\nclass Solution {\npublic:\n vector<int> seg;\nvector<int> ar;\nint x;\ | kniccknacc | NORMAL | 2022-09-11T19:39:53.365405+00:00 | 2022-09-11T19:39:53.365443+00:00 | 1,860 | false | Link to the submission stats:\nhttps://leetcode.com/submissions/detail/797375784/\n\n```\nclass Solution {\npublic:\n vector<int> seg;\nvector<int> ar;\nint x;\n\nvoid build(int v, int l, int r) {\n if (l == r) {\n seg[v] = ar[l];\n }\n else {\n int mid = (l + r) / 2;\n build(2 * v, l, ... | 5 | 0 | [] | 2 |
longest-increasing-subsequence-ii | Java Easy Segment Tree | java-easy-segment-tree-by-gurkaran_s-eh1l | ```\nclass Solution {\n int N = 1_00_001;\n int seg[];\n void update(int idx , int x , int low , int high , int i){\n if(low == high){\n | gurkaran_s | NORMAL | 2022-09-11T05:56:30.117669+00:00 | 2022-09-11T05:56:30.117706+00:00 | 783 | false | ```\nclass Solution {\n int N = 1_00_001;\n int seg[];\n void update(int idx , int x , int low , int high , int i){\n if(low == high){\n seg[idx] = x;\n return;\n }\n int mid = low + (high - low) / 2;\n if(i <= mid){\n update(2 * idx + 1 , x , low , ... | 5 | 0 | ['Tree', 'Java'] | 1 |
longest-increasing-subsequence-ii | Python | Easy | Queue | Longest Increasing Subsequence II | python-easy-queue-longest-increasing-sub-2vsi | \nsee the Successfully Accepted Submission\nPython\n# Define a MaxSegmentTree class with a constructor that takes the size \'n\' as input.\nclass MaxSegmentTree | Khosiyat | NORMAL | 2023-10-11T15:19:48.008395+00:00 | 2023-10-11T15:19:48.008419+00:00 | 384 | false | \n[see the Successfully Accepted Submission](https://leetcode.com/submissions/detail/1071709047/)\n```Python\n# Define a MaxSegmentTree class with a constructor that takes the size \'n\' as input.\nclass MaxSegmentTree:\n def __init__(self, n):\n # Store the size \'n\' as an instance variable.\n self.n... | 4 | 0 | ['Queue', 'Python'] | 0 |
longest-increasing-subsequence-ii | Does anyone know a monotonic queue solution? | does-anyone-know-a-monotonic-queue-solut-av4w | Does anyone know a monotonic queue solution? | roishevah | NORMAL | 2023-06-22T07:08:38.948317+00:00 | 2023-06-22T07:08:38.948340+00:00 | 325 | false | Does anyone know a monotonic queue solution? | 4 | 0 | ['Monotonic Queue'] | 1 |
longest-increasing-subsequence-ii | Q (Help) | Why python segment tree is TLE | q-help-why-python-segment-tree-is-tle-by-kfaj | I don\'t understand why this python Segment Tree solution TLE\'s.\n\nWhat am I doing wrong? help will be appreciated\n\n\nclass SegmentTree:\n \n def __in | nadaralp | NORMAL | 2022-09-11T09:06:20.957134+00:00 | 2022-09-11T09:07:11.773498+00:00 | 897 | false | I don\'t understand why this python Segment Tree solution TLE\'s.\n\nWhat am I doing wrong? help will be appreciated\n\n```\nclass SegmentTree:\n \n def __init__(self, A: list[int]):\n n = len(A)\n closest_2_power = math.ceil(math.log2(n))\n # pad\n A += [0] * ((2 << (closest_2_power-1... | 4 | 0 | ['Python'] | 4 |
longest-increasing-subsequence-ii | [Python3] MonoQ Without SegTree, 100% TC | python3-monoq-without-segtree-100-tc-by-j3m5h | Intuition\nWould it be easier if we change the constraint from 0 < nums[i]-nums[j] <= k to 0 < i-j <= k for adjacent elements in the subsequence? \n\n# Approach | zsq007 | NORMAL | 2024-05-01T04:19:39.848425+00:00 | 2024-11-12T17:36:01.792936+00:00 | 391 | false | # Intuition\nWould it be easier if we change the constraint from `0 < nums[i]-nums[j] <= k` to `0 < i-j <= k` for adjacent elements in the subsequence? \n\n# Approach\n\nTo convert the condition, we invert the array from `nums[ind]` to `inds[num]`, sort the indices by the numbers, and search for the LIS on it. Imagine ... | 3 | 0 | ['Sliding Window', 'Monotonic Queue', 'Python', 'Python3'] | 1 |
longest-increasing-subsequence-ii | Segment Tree Approach - Classical Segment Tree Implementation | segment-tree-approach-classical-segment-zrj93 | Intuition\n Describe your first thoughts on how to solve this problem. \nNotice that the subsequence is strickly increasing and the difference between adjacent | jimkirk | NORMAL | 2024-04-13T20:41:18.551996+00:00 | 2024-04-15T04:02:27.739444+00:00 | 499 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNotice that the subsequence is **strickly increasing** and the difference between adjacent elements in the subsequence is **at most k**.\n\nSo that given the current number is $num$, I need to find the longest-increasing-subsequence of [$... | 3 | 0 | ['Segment Tree', 'Java'] | 0 |
longest-increasing-subsequence-ii | [Python] A Solution without SegmentTree | python-a-solution-without-segmenttree-by-eu8o | Intuition\n Describe your first thoughts on how to solve this problem. \nSince the constraint is n \leq 10^5, we can\'t use the normal n^2 approach. In the norm | duyvt6663 | NORMAL | 2024-04-09T14:41:48.742035+00:00 | 2024-04-11T02:27:58.857394+00:00 | 335 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince the constraint is $$n \\leq 10^5$$, we can\'t use the normal $$n^2$$ approach. In the normal LIS problem, there is the $$O(n\\log n)$$ solution where you update the longest lowest increasing sequences within a single list. My intuit... | 3 | 0 | ['Binary Search', 'Python3'] | 0 |
longest-increasing-subsequence-ii | DP to Segment Tree (Python) | dp-to-segment-tree-python-by-ewackerbart-3wa3 | Intuition\n Describe your first thoughts on how to solve this problem. \nTo make sense of the accepted segment tree approach, we first need to briefly discuss t | ewackerbarth | NORMAL | 2023-06-22T03:39:21.776782+00:00 | 2023-06-22T03:39:21.776814+00:00 | 424 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo make sense of the accepted segment tree approach, we first need to briefly discuss the relevant dynamic programming approach with $$O(nk)$$ time complexity and $$O(n)$$ space complexity.\n\nWe\'re going to traverse our array nums. For ... | 3 | 0 | ['Dynamic Programming', 'Segment Tree', 'Python3'] | 1 |
longest-increasing-subsequence-ii | C++| Segment Tree | O(NlogN) | c-segment-tree-onlogn-by-kumarabhi98-m5x2 | For any nums[i], its last element in the increasing seq will range from nums[i]-k to nums[i]-1. Find the max length of elements in this Range.\n\nclass Solution | kumarabhi98 | NORMAL | 2022-09-11T06:16:59.533167+00:00 | 2022-09-11T06:16:59.533210+00:00 | 462 | false | For any `nums[i]`, its last element in the increasing seq will range from `nums[i]-k` to `nums[i]-1`. Find the max length of elements in this Range.\n```\nclass Solution {\npublic:\n const int n = 100001;\n int nums[4*100001] = {0};\n void update(int n,int id,int val,int l,int h){\n if(l<=h){\n ... | 3 | 0 | ['Tree', 'C'] | 0 |
longest-increasing-subsequence-ii | Segment Tree + 1D DP || C++ || Intutive Approach | segment-tree-1d-dp-c-intutive-approach-b-pc04 | USED IN THIS QUESTION\nSegment Tree -> To find the maximum value from the previous K numbers i.e. in range [a[i]-k,a[i]-1] . \nDP -> To store the LIS ending at | ayush_a1111 | NORMAL | 2022-09-11T04:10:35.325689+00:00 | 2022-09-11T04:46:23.390101+00:00 | 550 | false | **USED IN THIS QUESTION**\nSegment Tree -> To find the maximum value from the previous K numbers i.e. in range [a[i]-k,a[i]-1] . \nDP -> To store the LIS ending at that number ( dp[i] represents the LIS ending at that element ) \n\n```\nclass Solution {\n\n//CODE FOR SEGMENT TREE\n private:\n // BUILD FUNCTION (... | 3 | 1 | ['Dynamic Programming', 'Tree'] | 0 |
longest-increasing-subsequence-ii | Standard SegmentTree + DP || JAVA | standard-segmenttree-dp-java-by-priyank-idc5l | DP Defination\ndp[i] = length of LIS such that last element is i\nUsing this idea, if we are at arr[j], then previous element of this LIS ending at arr[j] can | priyank-doshi | NORMAL | 2022-09-11T04:08:28.826914+00:00 | 2022-09-11T04:11:43.088827+00:00 | 1,598 | false | **DP Defination**\ndp[i] = length of LIS such that last element is i\nUsing this idea, if we are at ```arr[j]```, then previous element of this LIS ending at ```arr[j]``` can be in the range of ```[arr[j] - k, arr[j] - 1]```. So we need to find max value of dp in the range of this.\nThis can be done faster using Segme... | 3 | 0 | ['Dynamic Programming', 'Tree', 'Java'] | 2 |
longest-increasing-subsequence-ii | [Python 3] Max Segment Tree | python-3-max-segment-tree-by-huangshan01-hqjk | Use a segment tree to track and query max length LIS. For leaf nodes in the 2nd half of the tree, tree index - len(nums) represents values appearing in nums. Fo | huangshan01 | NORMAL | 2022-09-11T04:07:33.421041+00:00 | 2022-09-11T13:31:38.259642+00:00 | 747 | false | Use a segment tree to track and query max length LIS. For leaf nodes in the 2nd half of the tree, `tree index - len(nums)` represents values appearing in nums. For a given val in nums, left query boundary = max(0, val - k), right query boundary = max(0, val - 1). val can be connected to all elements within the query ra... | 3 | 1 | ['Tree'] | 2 |
longest-increasing-subsequence-ii | [Javascript] Segment Tree | javascript-segment-tree-by-anna-hcj-qbzx | Solution: Segment Tree\n\nUse a segment tree to keep track of the longest subsequence ending with each number.\nFor each nums[i], take the maximum subsequence l | anna-hcj | NORMAL | 2022-09-11T04:00:37.494768+00:00 | 2022-09-11T04:00:37.494809+00:00 | 430 | false | **Solution: Segment Tree**\n\nUse a segment tree to keep track of the longest subsequence ending with each number.\nFor each `nums[i]`, take the maximum subsequence length ending within range `(nums[i] - k, nums[i] - 1)`.\n\n`n = length of nums`, `m = max(nums[i])`\nTime Complexity: `O(n log(m)) `\nSpace Complexity: `O... | 3 | 0 | ['Tree', 'JavaScript'] | 2 |
longest-increasing-subsequence-ii | C++ | Segment Tree Solution | c-segment-tree-solution-by-avadhut7969-hh9k | \n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\npublic:\n vector<int>tree;\n void update(int | avadhut7969 | NORMAL | 2024-05-24T04:48:15.749179+00:00 | 2024-05-24T04:48:15.749211+00:00 | 558 | false | \n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\npublic:\n vector<int>tree;\n void update(int node,int st,int end,int i,int val){\n if(st==end){\n tree[node]=max(tree[node],val);\n return;\n }\n int mid=(st+e... | 2 | 0 | ['Segment Tree', 'C++'] | 0 |
longest-increasing-subsequence-ii | Java modified LIS | Intuitive but TLE | java-modified-lis-intuitive-but-tle-by-s-stsu | Approach:\n- Posting just for intuition, LC doesn\'t accept this solution as it\'s getting TLE\n\n\nclass Solution {\n public int lengthOfLIS(int[] nums, int | sbd5_941 | NORMAL | 2024-02-08T15:10:18.703806+00:00 | 2024-02-08T15:10:18.703871+00:00 | 302 | false | # Approach:\n- Posting just for intuition, LC doesn\'t accept this solution as it\'s getting TLE\n\n```\nclass Solution {\n public int lengthOfLIS(int[] nums, int k) {\n int n = nums.length;\n int[] dp = new int[n];\n Arrays.fill(dp, 1);\n \n int max = 1;\n for(int i = n-1; ... | 2 | 0 | ['Java'] | 0 |
longest-increasing-subsequence-ii | Beats 81% || Segment Tree || DP || C++ | beats-81-segment-tree-dp-c-by-krishiiitp-zlmc | Code\n\n#define ll int\nclass Solution {\npublic:\n void build(vector<ll>& seg,vector<ll>& a,ll low,ll high,ll ind){\n if(low==high){\n seg | krishiiitp | NORMAL | 2024-01-24T14:07:50.251902+00:00 | 2024-01-24T14:07:50.251936+00:00 | 1,001 | false | # Code\n```\n#define ll int\nclass Solution {\npublic:\n void build(vector<ll>& seg,vector<ll>& a,ll low,ll high,ll ind){\n if(low==high){\n seg[ind]=a[low];\n return;\n } \n ll mid=(low + high)/2;\n build(seg,a,low,mid,2*ind + 1);\n build(seg,a,mid+1,high,2... | 2 | 0 | ['Dynamic Programming', 'Segment Tree', 'C++'] | 0 |
longest-increasing-subsequence-ii | C++ solution using segment tree | c-solution-using-segment-tree-by-ankit_6-f5yx | \n// // Template reference: https://github.com/ankit6776/CPP_LIBRARIES/blob/master/src/segTree.hpp\ntemplate<typename T> inline T Max(T a, T b){return a>b?a:b;} | ankit_6776 | NORMAL | 2022-10-10T07:15:48.383122+00:00 | 2022-10-10T07:15:48.383160+00:00 | 468 | false | ```\n// // Template reference: https://github.com/ankit6776/CPP_LIBRARIES/blob/master/src/segTree.hpp\ntemplate<typename T> inline T Max(T a, T b){return a>b?a:b;}\ntemplate<typename T> inline T Min(T a, T b){return a<b?a:b;}\ntemplate<typename T> inline T __Gcd(T a, T b){ return b==0 ? a : __Gcd(b,a%b);}\ntemplate<typ... | 2 | 0 | [] | 0 |
longest-increasing-subsequence-ii | Python, runtime O(n logn), memory O(n) | python-runtime-on-logn-memory-on-by-tsai-qtm6 | \n\nclass Solution:\n def getmax(self, st, start, end):\n maxi = 0\n \n while start < end:\n if start%2:#odd\n | tsai00150 | NORMAL | 2022-09-18T05:57:37.899557+00:00 | 2022-09-18T05:57:45.524175+00:00 | 640 | false | \n```\nclass Solution:\n def getmax(self, st, start, end):\n maxi = 0\n \n while start < end:\n if start%2:#odd\n maxi = max(maxi, st[start])\n start += 1\n if end%2:#odd\n end -= 1\n maxi = max(maxi, st[end])\n ... | 2 | 0 | ['Tree', 'Python', 'Python3'] | 0 |
longest-increasing-subsequence-ii | Segment Tree || C++ | segment-tree-c-by-vvd4-6317 | \n\nvoid update(vector<int> &seg,int si,int sl,int sr,int i,int val){\n if(sl==sr){\n seg[si]=max(seg[si],val);\n return;\n }\n int mid=( | vvd4 | NORMAL | 2022-09-17T08:21:20.110582+00:00 | 2022-09-17T08:21:20.110619+00:00 | 388 | false | ```\n\nvoid update(vector<int> &seg,int si,int sl,int sr,int i,int val){\n if(sl==sr){\n seg[si]=max(seg[si],val);\n return;\n }\n int mid=(sl+sr)/2;\n if(i<=mid)update(seg,si*2,sl,mid,i,val);\n else update(seg,si*2+1,mid+1,sr,i,val);\n seg[si]=max(seg[si*2],seg[si*2+1]);\n}\n\nint Q(vec... | 2 | 0 | [] | 0 |
longest-increasing-subsequence-ii | [JAVA] DP + Segment Tree, Time: NLogN, Space: (max - min), Commented | java-dp-segment-tree-time-nlogn-space-ma-id8d | Use the segment tree to store DP results and query them\n\n\nclass Solution {\n\tpublic int lengthOfLIS(int[] nums, int k) {\n\t\t// find the boundaries of the | yurokusa | NORMAL | 2022-09-13T14:11:21.429798+00:00 | 2022-09-16T17:21:35.313710+00:00 | 661 | false | Use the segment tree to store DP results and query them\n\n```\nclass Solution {\n\tpublic int lengthOfLIS(int[] nums, int k) {\n\t\t// find the boundaries of the segment tree\n\t\tvar minVal = Integer.MAX_VALUE;\n\t\tvar maxVal = Integer.MIN_VALUE;\n\t\tfor (int n : nums) {\n\t\t\tminVal = Math.min(minVal, n);\n\t\t\t... | 2 | 0 | ['Tree', 'Java'] | 1 |
longest-increasing-subsequence-ii | C# Solution | Time: O(n*log(n)), Memory: O(n) | Segment tree, Max range query | c-solution-time-onlogn-memory-on-segment-mayp | C#\npublic class Solution {\n public int LengthOfLIS(int[] nums, int k) {\n var max = nums.Max();\n var segmentTree = new SegmentTree(max + 1, | tonytroeff | NORMAL | 2022-09-12T20:50:27.163633+00:00 | 2022-09-12T20:50:27.163674+00:00 | 298 | false | ```C#\npublic class Solution {\n public int LengthOfLIS(int[] nums, int k) {\n var max = nums.Max();\n var segmentTree = new SegmentTree(max + 1, new MaxSegmentTreeNodeMerger());\n \n for (int i = 0; i < nums.Length; i++) {\n int left = Math.Max(0, nums[i] - k), right = nums[i]... | 2 | 0 | ['Tree'] | 0 |
longest-increasing-subsequence-ii | Simple Java Solution using TreeSet | simple-java-solution-using-treeset-by-pi-fmle | \nclass Solution {\n public int lengthOfLIS(int[] nums, int k) {\n ArrayList<TreeSet> arr=new ArrayList<>();\n arr.add(new TreeSet());\n | piyushkumar2000 | NORMAL | 2022-09-11T06:35:47.265900+00:00 | 2022-09-11T06:35:47.265946+00:00 | 846 | false | ```\nclass Solution {\n public int lengthOfLIS(int[] nums, int k) {\n ArrayList<TreeSet> arr=new ArrayList<>();\n arr.add(new TreeSet());\n for(int i=0;i<nums.length;i++){\n int val=nums[i];\n int n=arr.size();\n if(arr.get(0).lower(val)==null ){\n ... | 2 | 0 | ['Tree', 'Binary Tree', 'Ordered Set', 'Java'] | 2 |
longest-increasing-subsequence-ii | zkw segment tree | zkw-segment-tree-by-yachiyo-l3xp | \ndef lengthOfLIS(self, nums: List[int], k: int) -> int:\n N = 1+max(nums)\n tree = [0] * (2*N+1)\n def update(x, l):\n i = x+N\ | yachiyo | NORMAL | 2022-09-11T04:13:29.379301+00:00 | 2022-09-11T04:13:29.379337+00:00 | 297 | false | ```\ndef lengthOfLIS(self, nums: List[int], k: int) -> int:\n N = 1+max(nums)\n tree = [0] * (2*N+1)\n def update(x, l):\n i = x+N\n while i:\n tree[i] = max(tree[i],l)\n i>>=1\n def query(l,r):\n ans =0 \n l += N-1\n ... | 2 | 0 | ['Tree', 'Python'] | 0 |
longest-increasing-subsequence-ii | Memoization-Tabulation-Space Optimization Explained || DP | memoization-tabulation-space-optimizatio-q0yv | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | blackhorse_ | NORMAL | 2024-06-18T18:19:39.073060+00:00 | 2024-06-18T18:19:39.073105+00:00 | 173 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Dynamic Programming', 'Segment Tree', 'C++'] | 0 |
longest-increasing-subsequence-ii | Stretch that Sequence: Long and Increasing! | stretch-that-sequence-long-and-increasin-7kkx | Intuition\nMy first thought was to use dynamic programming to solve this problem. Given that we need to find the longest strictly increasing subsequence where t | pavan_riser | NORMAL | 2024-06-10T16:56:59.566203+00:00 | 2024-06-10T16:56:59.566239+00:00 | 220 | false | # Intuition\nMy first thought was to use dynamic programming to solve this problem. Given that we need to find the longest strictly increasing subsequence where the difference between adjacent elements is at most k, the challenge is to efficiently find and update the lengths of such subsequences.\n\n# Approach\n**Dynam... | 1 | 0 | ['Dynamic Programming', 'Segment Tree', 'C++'] | 1 |
longest-increasing-subsequence-ii | C++ concise solution with helpful video | c-concise-solution-with-helpful-video-by-aiic | Video not made by me. Still a very difficult algorithm to understand even with such a helpful video.\nhttps://www.youtube.com/watch?v=xztU7lmDLv8\n\nclass Solut | user5976fh | NORMAL | 2024-04-19T20:11:19.027351+00:00 | 2024-04-19T20:16:25.070886+00:00 | 4 | false | Video not made by me. Still a very difficult algorithm to understand even with such a helpful video.\nhttps://www.youtube.com/watch?v=xztU7lmDLv8\n```\nclass Solution {\npublic:\n int s = 100001;\n int a[200002] = {};\n int findMax(int l, int r){\n int m = 0;\n l += s, r += s;\n while (l <... | 1 | 0 | [] | 0 |
longest-increasing-subsequence-ii | Node Based Recursive Implementation Segment Tree Example Run | node-based-recursive-implementation-segm-5mpf | Intuition\nInspired by shiv-code\'s solution and explanation. Replaced the Iterative Segment Tree with Node based recursive segment tree which is inspired by ne | rajsinghsudhanshu930 | NORMAL | 2024-01-15T08:12:22.907545+00:00 | 2024-01-15T08:15:32.198702+00:00 | 22 | false | # Intuition\nInspired by shiv-code\'s solution and explanation. Replaced the Iterative Segment Tree with Node based recursive segment tree which is inspired by neetcode.\n \n# Approach\nWe store the longest increasing sequence ending at every number in nums in a segment tree.\n\nThe array which this segment tree repres... | 1 | 0 | ['Python3'] | 0 |
longest-increasing-subsequence-ii | C++ simple and short, no segment tree. | c-simple-and-short-no-segment-tree-by-ir-dk2o | Intuition\n\n1. When an element is added to an existing sequence, this breaks the sequence into two sequences: with and without the added element, unless the ne | irisonia | NORMAL | 2023-09-02T08:04:08.203499+00:00 | 2023-11-09T13:22:52.522199+00:00 | 489 | false | # Intuition\n\n1. When an element is added to an existing sequence, this breaks the sequence into two sequences: with and without the added element, unless the newly added value is exactly following the last element of the sequence, in which case we only keep the sequence with the new element.\nExample:\nInput: 1, 2, 1... | 1 | 0 | ['C++'] | 1 |
longest-increasing-subsequence-ii | C++ Bottom-up Segment Tree (beats 100%) | c-bottom-up-segment-tree-beats-100-by-su-ohqo | Intuition\nImplement a naive DP, namely if we have state dp[i][value] be the best we can do with the first i elements and such that the last element is value. T | suhdavid11 | NORMAL | 2023-08-14T06:50:48.947474+00:00 | 2023-08-14T17:36:39.866198+00:00 | 237 | false | # Intuition\nImplement a naive DP, namely if we have state `dp[i][value]` be the best we can do with the first `i` elements and such that the last element is `value`. Then it is not hard to see that \n\n$$\n\\tt dp[i][nums[i]] = 1 + max_{v\\in [nums[i]-k, nums[i])} dp[i][v].\n$$\n\nBut this naive approach is only $O(n^... | 1 | 0 | ['C++'] | 1 |
longest-increasing-subsequence-ii | [Python] Segment Tree Template (3 mins) with thought process when being asked during interviews | python-segment-tree-template-3-mins-with-t3f7 | Segment Tree\nYour first intuition might be using binary search on the longest sequence maintained as iterating each num similarly to the quite popluar problem | jandk | NORMAL | 2023-07-23T21:20:14.970125+00:00 | 2023-07-23T21:20:14.970144+00:00 | 11 | false | ### Segment Tree\nYour first intuition might be using binary search on the longest sequence maintained as iterating each `num` similarly to the quite popluar problem `longest increasing subsequence`. However, it doesn\'t work with the requirement of having difference between adjacent elements of at most `k`. An anti-ex... | 1 | 0 | ['Tree', 'Python'] | 0 |
longest-increasing-subsequence-ii | Segment Tree Solution | Java | Explained | segment-tree-solution-java-explained-by-hcudn | Intuition\n Describe your first thoughts on how to solve this problem. \nThis questions is somehow similar to 315. Count of Smaller Numbers after Self which is | utkrsh003 | NORMAL | 2023-06-18T10:41:32.455505+00:00 | 2023-06-18T10:41:32.455526+00:00 | 98 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis questions is somehow similar to [315. Count of Smaller Numbers after Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) which is similar to [307. Range Sum Query - Mutable](https://leetcode.com/problems/range-s... | 1 | 0 | ['Segment Tree', 'Java'] | 0 |
longest-increasing-subsequence-ii | thought process, 100%, nlog(k), segment tree, optimization | thought-process-100-nlogk-segment-tree-o-3yfz | Intuition\n1. based on already-well-explained segment tree solution\n - https://leetcode.com/problems/longest-increasing-subsequence-ii/solutions/2560085/pyt | jimhorng | NORMAL | 2023-01-13T13:15:37.678841+00:00 | 2023-01-15T01:57:11.205505+00:00 | 235 | false | # Intuition\n1. based on already-well-explained segment tree solution\n - https://leetcode.com/problems/longest-increasing-subsequence-ii/solutions/2560085/python-explanation-with-pictures-segment-tree/\n - https://codeforces.com/blog/entry/18051\n2. these common solutions set the segment tree\'s leaves as `0 ~ m... | 1 | 0 | ['Python'] | 0 |
longest-increasing-subsequence-ii | [C++] Segment Tree || Short Implementation + Fast | c-segment-tree-short-implementation-fast-ybuh | cpp\nclass Solution {\npublic:\n vector<int> seg;\n \n int getSize(int n){\n int size = 1;\n for(;size < n; size<<=1);\n return si | sandrikosxila | NORMAL | 2022-09-17T18:03:29.274157+00:00 | 2022-09-17T18:03:29.274190+00:00 | 244 | false | ```cpp\nclass Solution {\npublic:\n vector<int> seg;\n \n int getSize(int n){\n int size = 1;\n for(;size < n; size<<=1);\n return size;\n }\n \n void insert(int index, int value, int size){\n for(int i = index + size - 1; i > 0; seg[i]=max(seg[i], value), i>>=1);\n }\n ... | 1 | 0 | ['Tree', 'C'] | 0 |
longest-increasing-subsequence-ii | [Java] Segment Tree beats 100% | java-segment-tree-beats-100-by-luoyunfen-67zm | \nclass Solution {\n private int[] segments;\n private int n;\n public int lengthOfLIS(int[] nums, int k) {\n int max = 0;\n for (int num | luoyunfeng1853 | NORMAL | 2022-09-12T09:24:18.153475+00:00 | 2022-09-12T09:24:18.153510+00:00 | 122 | false | ```\nclass Solution {\n private int[] segments;\n private int n;\n public int lengthOfLIS(int[] nums, int k) {\n int max = 0;\n for (int num : nums) {\n max = Math.max(max, num);\n }\n \n n = max;\n segments = new int[n*2];\n \n int res = 0;\n ... | 1 | 0 | ['Tree', 'Java'] | 0 |
longest-increasing-subsequence-ii | Segment Tree C++ Clean Code | segment-tree-c-clean-code-by-jinyeli1996-855f | \nclass Solution {\npublic:\n vector<int> seg;\n int n=100001;\n \n int query(int left, int right){\n int ans=0;\n for(left+=n, right+ | jinyeli1996 | NORMAL | 2022-09-12T08:37:50.196096+00:00 | 2022-09-12T08:37:50.196135+00:00 | 158 | false | ```\nclass Solution {\npublic:\n vector<int> seg;\n int n=100001;\n \n int query(int left, int right){\n int ans=0;\n for(left+=n, right+=n; left<right; left>>=1, right>>=1){\n if(left&1){\n ans=max(ans, seg[left++]);\n }\n if(right&1){\n ... | 1 | 0 | [] | 1 |
longest-increasing-subsequence-ii | Does not work for sequential numbers from 1 to 10^5. - JAVA - dp - with memoization - TLE | does-not-work-for-sequential-numbers-fro-3zar | This one does not work for large test cases typically 1 to 100000 with unit increase. Looks like it should - any idea\n\n\nclass Solution {\n int res;\n p | venkim | NORMAL | 2022-09-12T02:10:03.328867+00:00 | 2022-09-12T02:10:09.668390+00:00 | 237 | false | This one does not work for large test cases typically 1 to 100000 with unit increase. Looks like it should - any idea\n\n```\nclass Solution {\n int res;\n public int lengthOfLIS(int[] nums, int k) {\n int n = nums.length;\n // keep track of state\n Integer[] dp = new Integer [n];\n dp... | 1 | 0 | [] | 0 |
longest-increasing-subsequence-ii | Python Iterativr segment tree | python-iterativr-segment-tree-by-vignesh-vx7u | \nclass ST:\n def __init__(self,n):\n self.arr=[0 for i in range(4*n)]\n self.n=n\n\n def update(self,ind,val):\n def rec(i,l,r):\n | vigneshreddyjulakanti | NORMAL | 2022-09-11T16:44:38.175804+00:00 | 2022-09-11T16:44:38.175863+00:00 | 107 | false | ```\nclass ST:\n def __init__(self,n):\n self.arr=[0 for i in range(4*n)]\n self.n=n\n\n def update(self,ind,val):\n def rec(i,l,r):\n if l==r:\n self.arr[i]=val\n return\n m=(l+r)//2\n if m>=ind:\n rec(2*i+1,l,m)\n... | 1 | 0 | [] | 0 |
longest-increasing-subsequence-ii | [Kotlin] Segment Tree | kotlin-segment-tree-by-roka-ydrt | kotlin\nimport kotlin.math.max\nimport kotlin.math.min\n\nclass Solution {\n companion object {\n const val MAX_VALUE = 100010\n }\n\n fun lengt | roka | NORMAL | 2022-09-11T16:04:42.636868+00:00 | 2022-09-11T16:04:42.636910+00:00 | 42 | false | ```kotlin\nimport kotlin.math.max\nimport kotlin.math.min\n\nclass Solution {\n companion object {\n const val MAX_VALUE = 100010\n }\n\n fun lengthOfLIS(nums: IntArray, k: Int): Int {\n val tree = Tree(MAX_VALUE)\n var answer = 0\n\n nums.forEach { cur ->\n val prev = ma... | 1 | 0 | ['Tree', 'Kotlin'] | 0 |
longest-increasing-subsequence-ii | Rust Segment Tree | rust-segment-tree-by-xiaoping3418-zosb | ~~~\nimpl Solution {\n pub fn length_of_lis(nums: Vec, k: i32) -> i32 {\n let n = *nums.iter().max().unwrap() as usize + 1;\n let mut tree = ve | xiaoping3418 | NORMAL | 2022-09-11T10:50:21.081981+00:00 | 2022-09-11T10:51:35.569107+00:00 | 88 | false | ~~~\nimpl Solution {\n pub fn length_of_lis(nums: Vec<i32>, k: i32) -> i32 {\n let n = *nums.iter().max().unwrap() as usize + 1;\n let mut tree = vec![0; 4 * n];\n let mut ret = 1;\n \n for a in nums {\n let temp = Self::find(&tree, 1, 0, n - 1, i32::max(0, a - k) as usi... | 1 | 0 | ['Tree', 'Rust'] | 0 |
longest-increasing-subsequence-ii | Segment Tree | Java | segment-tree-java-by-pradhan222-b36f | For eg: 4,2,1,4,3,4,5,8,15\nat each index i,\nnums[i] -> find longest subsequence between in segment tree [max(1, nums[i] -k), nums[i] - 1)]\nand then update se | pradhan222 | NORMAL | 2022-09-11T06:01:09.773805+00:00 | 2022-09-11T06:08:36.239107+00:00 | 355 | false | For eg: 4,2,1,4,3,4,5,8,15\nat each index i,\nnums[i] -> find longest subsequence between in segment tree [max(1, nums[i] -k), nums[i] - 1)]\nand then update segment tree for nums[i] with (longest subsequence + 1).\n\nfor index 6, nums[6] = 5\nstart range = 5 - 3 = 2\nend range = 5 - 1 = 4\nlongest subsequece in range ... | 1 | 0 | ['Tree', 'Java'] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.