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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Why test case has leading zero? | why-test-case-has-leading-zero-by-k48723-3prs | In constraints, said that would not contain leading zeros.\n\nnum consists of only digits and does not contain leading zeros.\n\nHowever, when I submit my answe | k487237 | NORMAL | 2022-04-12T14:47:35.125730+00:00 | 2022-04-12T14:47:35.125784+00:00 | 94 | false | In constraints, said that would not contain leading zeros.\n```\nnum consists of only digits and does not contain leading zeros.\n```\nHowever, when I submit my answer, show my answer is `Memory Limit Exceeded`\nAnd I look the Input, there have leading zero in input.\n Simple Bruteforce (91.77/95.88) | c-on2-simple-bruteforce-91779588-by-ahol-oleg | \nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.length();\n string ans(n,\'-\');\n int i = 0;\n | aholtzman | NORMAL | 2021-12-16T20:33:00.498545+00:00 | 2021-12-19T18:57:22.709722+00:00 | 283 | false | ```\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.length();\n string ans(n,\'-\');\n int i = 0;\n int first = \'0\';\n int start = 0;\n int end = n-1;\n bool found = true;\n while (found == true)\n {\n fou... | 1 | 1 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ O(nlogn) with segment tree | c-onlogn-with-segment-tree-by-yinyueemo-292v | \nstruct Node {\n int l, r;\n int v;\n Node() {\n v = 0;\n };\n};\n\n\nclass Solution {\npublic:\n vector<Node> tree;\n void build(int | yinyueemo | NORMAL | 2021-05-30T06:24:43.671759+00:00 | 2021-05-30T06:24:43.671785+00:00 | 710 | false | ```\nstruct Node {\n int l, r;\n int v;\n Node() {\n v = 0;\n };\n};\n\n\nclass Solution {\npublic:\n vector<Node> tree;\n void build(int l, int r, int p) {\n tree[p].l = l;\n tree[p].r = r;\n if (l != r) {\n int mid = (l + r) >> 1;\n build(l, mid, 2 *... | 1 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Java] Brute Force via Bubble Sort | java-brute-force-via-bubble-sort-by-shen-hyp0 | In short, the idea is to bubble up the smallest element so far (need to consider k swaps constraint)\n\n\tpublic class Solution1505 {\n\t\tpublic static void ma | shentianjie466357304 | NORMAL | 2021-03-04T01:40:34.073903+00:00 | 2021-03-04T01:41:38.588833+00:00 | 251 | false | In short, the idea is to bubble up the smallest element so far (need to consider k swaps constraint)\n\n\tpublic class Solution1505 {\n\t\tpublic static void main(String[] args) {\n\t\t\tString num = "9438957234785635408";\n\t\t\tint k = 23;\n\n\t\t\tSolution1505 solution1505 = new Solution1505();\n\n\t\t\tSystem.out.p... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python3 - O(n^2) and O(n*log(n)) | python3-on2-and-onlogn-by-yunqu-a4qj | Solution 1: The straightfoward O(n^2) solution will use a similar technique as the bubble sort; this will be TLE.\npython\nclass Solution:\n def minInteger(s | yunqu | NORMAL | 2021-03-01T03:35:12.601900+00:00 | 2021-03-01T03:41:47.085685+00:00 | 325 | false | **Solution 1**: The straightfoward O(n^2) solution will use a similar technique as the bubble sort; this will be TLE.\n```python\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n """O(n^2) TLE"""\n i, size = 0, len(num)\n while k > 0 and i < size:\n mini = num[i]\n ... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java Solution | java-solution-by-balenduteterbay-cier | java\n\nclass Solution {\n public String minInteger(String num, int k) {\n char[] ca = num.toCharArray();\n helper(ca, 0, k);\n return n | balenduteterbay | NORMAL | 2020-12-19T09:33:27.987828+00:00 | 2020-12-19T09:33:27.987866+00:00 | 340 | false | java\n```\nclass Solution {\n public String minInteger(String num, int k) {\n char[] ca = num.toCharArray();\n helper(ca, 0, k);\n return new String(ca);\n }\n \n public void helper(char[] ca, int I, int k) {\n if(k==0 || I==ca.length)\n return ;\n int min=ca[I]... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] Fenwick tree O(n log n) | c-fenwick-tree-on-log-n-by-aleksey12345-nyj8 | \nclass FenwickTree\n{\n public:\n explicit FenwickTree(int size)\n : arr(size + 1, 0)\n {\n }\n \n int get(int index)\n {\n ++i | aleksey12345 | NORMAL | 2020-08-19T17:40:31.283384+00:00 | 2020-08-19T17:40:31.283435+00:00 | 340 | false | ```\nclass FenwickTree\n{\n public:\n explicit FenwickTree(int size)\n : arr(size + 1, 0)\n {\n }\n \n int get(int index)\n {\n ++index;\n int value = 0;\n \n while (index)\n {\n value += arr[index];\n index -= index & (-index);\n ... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java Solution. Complexity O(k * n) | java-solution-complexity-ok-n-by-rutvikb-483l | \nclass Solution {\n public String minInteger(String num, int swaps) {\n char[] arr = num.toCharArray();\n arr = minArray(arr, arr.length, swap | rutvikbk | NORMAL | 2020-07-07T17:20:02.154109+00:00 | 2020-07-07T17:20:02.154159+00:00 | 138 | false | ```\nclass Solution {\n public String minInteger(String num, int swaps) {\n char[] arr = num.toCharArray();\n arr = minArray(arr, arr.length, swaps); \n return new String(arr);\n }\n char[] minArray(char[] arr, int length, int swaps) { \n if (swaps == 0) \n return arr; \n... | 1 | 1 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] O(NogN) BIT/FenwickTree solution | python-onogn-bitfenwicktree-solution-by-dkq0u | BIT : to get number of elements shifted before processing index\nind : to store occurence of digit\n\nclass Solution:\n def minInteger(self, num: str, k: | vaibhavd143 | NORMAL | 2020-07-06T19:49:47.232183+00:00 | 2020-07-06T19:49:47.232231+00:00 | 158 | false | `BIT` : to get number of elements shifted before processing index\n`ind` : to store occurence of digit\n```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n def update(ind,bit):\n ind=ind+1\n while ind<len(bit):\n bit[ind]+=1\n ind += (... | 1 | 0 | ['Binary Indexed Tree'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] O(N) Count the used index | c-on-count-the-used-index-by-zakyking-wv7f | The ituition of the problem is to collect the smallest reachable number iteratively.\nObviously, Greedy method will be used by looping from 0 to 9, and check if | zakyking | NORMAL | 2020-07-06T19:49:04.327888+00:00 | 2020-07-07T02:31:46.798211+00:00 | 255 | false | The ituition of the problem is to collect the smallest reachable number iteratively.\nObviously, Greedy method will be used by looping from 0 to 9, and check if any index reachable.\n\nThere are several ways to check if the index is reachable.\nThe straightfoward way is to loop and find number within K, by recording if... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] O(n log n) Fenwick Tree Solution Inspired By @awice | c-on-log-n-fenwick-tree-solution-inspire-88m6 | You can watch his full explanation starting at 33:47: https://www.youtube.com/watch?v=pO_TtGTe6GQ\n\nclass FenwickTree {\nprivate:\n vector<int> arr; \n\npub | blackspinner | NORMAL | 2020-07-06T02:52:13.556329+00:00 | 2020-07-06T02:53:00.108047+00:00 | 156 | false | You can watch his full explanation starting at 33:47: https://www.youtube.com/watch?v=pO_TtGTe6GQ\n```\nclass FenwickTree {\nprivate:\n vector<int> arr; \n\npublic:\n FenwickTree(int n) {\n n++;\n arr = vector<int>(n, 0);\n }\n \n void add(int index, const int num) {\n index++;\n ... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++} O(NlogN) Solution Faster Than 100% soln | c-onlogn-solution-faster-than-100-soln-b-3s04 | Well Idea is to reduce the simple n^2 solution using Binary Indexed Tree. In O(n^2) solution we check for all non selected indices but only ten digits are possi | abhutani06 | NORMAL | 2020-07-05T09:13:11.671710+00:00 | 2020-07-06T10:58:21.467526+00:00 | 127 | false | Well Idea is to reduce the simple n^2 solution using Binary Indexed Tree. In O(n^2) solution we check for all non selected indices but only ten digits are possible from \'0\' to \'9\' well we can maintain a queue for each digit from \'0\' to \'9\' that consists first occurance of the the digit at the front. We can use ... | 1 | 1 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | java n^2 solution / nlogn segment tree solution/ nlogn BIT solution | java-n2-solution-nlogn-segment-tree-solu-r7l4 | BIT\n\nclass Solution {\n public String minInteger(String s, int k) {\n int arr[]=new int[s.length()];\n Arrays.fill(arr,1);\n Fenwick f | 66brother | NORMAL | 2020-07-05T08:32:16.290684+00:00 | 2020-11-11T23:39:42.711676+00:00 | 127 | false | BIT\n```\nclass Solution {\n public String minInteger(String s, int k) {\n int arr[]=new int[s.length()];\n Arrays.fill(arr,1);\n Fenwick fe=new Fenwick(arr);\n StringBuilder res=new StringBuilder();\n Queue<Integer>q[]=new LinkedList[10];\n for(int i=0;i<q.length;i++){\n ... | 1 | 1 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] greedy with index preprocessing | c-greedy-with-index-preprocessing-by-phi-irpt | \nclass Solution {\npublic:\n string minInteger(string num, int k) {\n std::vector<std::vector<int>> digit_to_inds(10);\n std::vector<int> void | phi9t | NORMAL | 2020-07-05T07:53:07.293711+00:00 | 2020-07-05T07:53:07.293760+00:00 | 174 | false | ```\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n std::vector<std::vector<int>> digit_to_inds(10);\n std::vector<int> voids; // locations that are empty, no price to pay\n for (int i = num.size() - 1; i >= 0; --i) {\n digit_to_inds[static_cast<int>(num[i] - \'0... | 1 | 0 | [] | 1 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java,Binary Indexed Tree,O(nlogn),17ms | javabinary-indexed-treeonlogn17ms-by-tbl-l6qk | idea : Every turn we find the smallest number that we can move to the head of the left string, it will make the answer minimum. We can calculate how many chara | tblade-caohui | NORMAL | 2020-07-05T06:25:59.024146+00:00 | 2020-07-05T06:27:55.059087+00:00 | 100 | false | idea : Every turn we find the smallest number that we can move to the head of the left string, it will make the answer minimum. We can calculate how many characters in the left string before the most left index of current number, if it is less than or equal to k, then we can move a current number of most left index t... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python (NlogN) using Binary Index Tree | python-nlogn-using-binary-index-tree-by-2qrqs | Well.. the average performance should be NlogN, but it is hard to prove whether the worst case still holds. Anyway, I still share my solution.\n\n\nfrom heapq i | pze168 | NORMAL | 2020-07-05T05:14:02.727597+00:00 | 2020-07-05T05:15:29.606345+00:00 | 218 | false | Well.. the average performance should be NlogN, but it is hard to prove whether the worst case still holds. Anyway, I still share my solution.\n\n```\nfrom heapq import heapify, heappop, heappush\n\ndef getsum(tree, i): \n s = 0\n i += 1\n while i > 0: \n s += tree[i] \n i -= i & (-i) \n retur... | 1 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [Python] O(N log N) 316ms Greedy Solution, finished 5 min after contest end 😭 | python-on-log-n-316ms-greedy-solution-fi-s93c | Probably being distracted by the Independence Day fireworks, I finished coding this O(N log N) solution 5 min after the end of the weekly contest \uD83D\uDE2D\n | wwssyy5511 | NORMAL | 2020-07-05T04:10:03.509475+00:00 | 2020-07-05T04:34:14.104620+00:00 | 529 | false | Probably being distracted by the Independence Day fireworks, I finished coding this O(N log N) solution 5 min after the end of the weekly contest \uD83D\uDE2D\n\n\n**O(N log N)** Solution\n```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n ind = [[] for _ in range(10)]\n for i in r... | 1 | 0 | [] | 2 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Explained | Clean Code | Fenwick | 7ms | explained-clean-code-fenwick-7ms-by-ivan-7ha9 | IntuitionTo minimize a number, place the smallest digits as early as possible, using a greedy approach where each adjacent swap costs 1 move from our budget of | ivangnilomedov | NORMAL | 2025-03-23T09:42:42.050272+00:00 | 2025-03-23T09:42:42.050272+00:00 | 4 | false | # Intuition
To minimize a number, place the smallest digits as early as possible, using a greedy approach where each adjacent swap costs 1 move from our budget of k swaps.
# Approach
1. **Build Digit Lookup Structure**: Create linked lists for each digit (0-9) to find all occurrences in the input.
2. **Fenwick Tree f... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | py3 82%beats | py3-82beats-by-fareedbaba-xhke | IntuitionGiven a string num representing the digits of a large integer and an integer k, you can swap any two adjacent digits at most k times. The goal is to re | fareedbaba | NORMAL | 2025-02-23T10:27:09.418857+00:00 | 2025-02-23T10:27:09.418857+00:00 | 12 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Given a string num representing the digits of a large integer and an integer k, you can swap any two adjacent digits at most k times. The goal is to return the smallest possible integer that can be formed.
# Approach
<!-- Describe your app... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits | 1505-minimum-possible-integer-after-at-m-82gf | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-13T05:41:40.935828+00:00 | 2025-01-13T05:41:40.935828+00:00 | 10 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Backtracking approach ( TLE) | backtracking-approach-tle-by-anushakanda-v9ri | Code | anushakandagal | NORMAL | 2024-12-29T06:30:06.948433+00:00 | 2024-12-29T06:30:06.948433+00:00 | 11 | false |
# Code
```java []
class Solution {
String minNum = "";
public String minInteger(String num, int k) {
minNum = num;
char[] array = num.toCharArray();
findNum(array , k);
return minNum;
}
public void findNum(char[] array , int k){
String num = new String(array);
... | 0 | 0 | ['Backtracking', 'Recursion', 'Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | For every position i , replace it with smallest element found within k window size. ACCEPTED!! | for-every-position-i-with-smallest-eleme-y7gc | Intuition
For every position i , replace it with smallest element found within k window size.
Fill ith position by bringing smallest element till ith position u | anushakandagal | NORMAL | 2024-12-29T06:20:36.532247+00:00 | 2024-12-29T06:23:11.387883+00:00 | 8 | false | # Intuition
- For every position i , replace it with smallest element found within k window size.
- Fill ith position by bringing smallest element till ith position using bubble sort technique and decrease k with each swap.
# Complexity
- Time complexity: O(N^2)
- Space complexity: O(N)
# Code
```java []
class Solut... | 0 | 0 | ['Greedy', 'Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | find the min value in k region | find-the-min-value-in-k-region-by-linda2-r8ba | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | linda2024 | NORMAL | 2024-12-17T00:26:47.515817+00:00 | 2024-12-17T00:26:47.515817+00:00 | 5 | 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)$$ --... | 0 | 0 | ['C#'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | The greedy **non Segment-Tree** solution you have been searching for | the-greedy-non-segment-tree-solution-you-ua5i | Intuition\nWe need to place the numerically smallest digit at the most significant place(i.e leftmost place).\n# Approach\nWe find the smallest digit in the rig | pramodhv_28 | NORMAL | 2024-11-11T09:31:30.236064+00:00 | 2024-11-11T09:31:30.236100+00:00 | 7 | false | # Intuition\nWe need to place the numerically smallest digit at the most significant place(i.e leftmost place).\n# Approach\nWe find the smallest digit in the right from i and perform x operations such that x <= k.\nAfter which we subtract x from k, \nIn 4321, we perform 3 operations to bring 1 from num[3] to nums[0].N... | 0 | 0 | ['String', 'Greedy', 'Sliding Window', 'C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits- JavaSolution | minimum-possible-integer-after-at-most-k-ki16 | \n\n# Code\njava []\nclass Solution {\n public String minInteger(String num, int k) {\n //pqs stores the location of each digit.\n List<Queue<I | himashusharma | NORMAL | 2024-10-04T07:54:26.443740+00:00 | 2024-10-04T07:54:26.443773+00:00 | 16 | false | \n\n# Code\n```java []\nclass Solution {\n public String minInteger(String num, int k) {\n //pqs stores the location of each digit.\n List<Queue<Integer>> pqs = new ArrayList<>();\n for (int i = 0; i <= 9; ++i) {\n pqs.add(new LinkedList<>());\n }\n\n for (int i = 0; i <... | 0 | 0 | ['String', 'Greedy', 'Binary Indexed Tree', 'Segment Tree', 'Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | 1505. Minimum Possible Integer After at Most K Adjacent Swaps On Digits.cpp | 1505-minimum-possible-integer-after-at-m-0q4t | Code\n\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.size();\n if(k > n*(n+1)/2)\n {\n so | 202021ganesh | NORMAL | 2024-09-30T12:27:07.807980+00:00 | 2024-09-30T12:27:07.808020+00:00 | 1 | false | **Code**\n```\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n int n = num.size();\n if(k > n*(n+1)/2)\n {\n sort(num.begin(), num.end());\n }\n for(int i=0;i<n && k >0;i++)\n {\n int pos = i;\n for(int j=i+1;j<n;j++)\n ... | 0 | 0 | ['C'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Simplest Solution (code commented) | simplest-solution-code-commented-by-vidy-4h9i | Code\ncpp []\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n // idea: find the min num that is within\n // k and bring th | vidyatheerthan | NORMAL | 2024-09-16T03:34:26.620512+00:00 | 2024-09-16T03:34:26.620549+00:00 | 11 | false | # Code\n```cpp []\nclass Solution {\npublic:\n string minInteger(string num, int k) {\n // idea: find the min num that is within\n // k and bring them forward 1 at a time\n // then start next search after previously\n // placed min num.\n // eg: 4321\n // 1st itr => 1432 (of... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | O(N) python with explanation | on-python-with-explanation-by-swarbrickj-iie3 | Approach\nWe need to bring the small numbers to the front to make the start of the string sorted low->high. It\'s tough to keep track of how far each number nee | swarbrickjones | NORMAL | 2024-09-15T22:01:17.921102+00:00 | 2024-09-15T22:01:17.921123+00:00 | 19 | false | # Approach\nWe need to bring the small numbers to the front to make the start of the string sorted low->high. It\'s tough to keep track of how far each number needs to move to get to sorted position, as this changes over time. The trick: first scan for 0s going left to right, then move them to the front (or rather the ... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | 👍Runtime 57 ms Beats 100.00% | runtime-57-ms-beats-10000-by-pvt2024-lmvk | Code\n\ntype SegmentTree struct {\n\tnodes []int\n\tn int\n}\n\nfunc NewSegmentTree(max int) *SegmentTree {\n\treturn &SegmentTree{nodes: make([]int, 4*max) | pvt2024 | NORMAL | 2024-06-30T03:01:48.968648+00:00 | 2024-06-30T03:01:48.968678+00:00 | 7 | false | # Code\n```\ntype SegmentTree struct {\n\tnodes []int\n\tn int\n}\n\nfunc NewSegmentTree(max int) *SegmentTree {\n\treturn &SegmentTree{nodes: make([]int, 4*max), n: max}\n}\n\nfunc (st *SegmentTree) add(num, l, r, node int) {\n\tif num < l || num > r {\n\t\treturn\n\t}\n\tif l == r {\n\t\tst.nodes[node]++\n\t\tret... | 0 | 0 | ['Go'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Using Walk on Segment Tree | using-walk-on-segment-tree-by-theabbie-hm9p | \nfrom collections import defaultdict\nfrom sortedcontainers import SortedList\n\nclass SegTree:\n def __init__(self, n):\n self.ctr = defaultdict(int | theabbie | NORMAL | 2024-05-21T15:40:01.613970+00:00 | 2024-05-21T15:40:01.614004+00:00 | 11 | false | ```\nfrom collections import defaultdict\nfrom sortedcontainers import SortedList\n\nclass SegTree:\n def __init__(self, n):\n self.ctr = defaultdict(int)\n self.init = (float(\'inf\'), n)\n self.vals = defaultdict(lambda: self.init)\n \n def update(self, i, v, x, y, k = 1):\n i... | 0 | 0 | ['Python'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Direct Simulation | direct-simulation-by-theabbie-ltnp | \nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n w = k + 1\n res = []\n while num and w:\n | theabbie | NORMAL | 2024-05-21T03:59:33.004988+00:00 | 2024-05-21T03:59:33.005008+00:00 | 3 | false | ```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n w = k + 1\n res = []\n while num and w:\n s = num[:w]\n j = s.index(min(s))\n res.append(s[j])\n w -= j\n num = num[:j] + num[j+1:]\n return... | 0 | 0 | ['Python'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Brute force | brute-force-by-maxorgus-wmip | Did not expect this to pass\n\n# Code\n\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n if k >= n*(n-1) // 2 | MaxOrgus | NORMAL | 2024-05-08T02:38:18.694400+00:00 | 2024-05-08T02:38:18.694429+00:00 | 37 | false | Did not expect this to pass\n\n# Code\n```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n if k >= n*(n-1) // 2:return \'\'.join(sorted(num))\n res = \'\'\n i = 0\n while k > 0:\n if len(res) == n:\n break\n mi... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Fenwick Tree solution | fenwick-tree-solution-by-dagmarc-g5qe | \n# Code\n\npackage main\n\nimport (\n\t"container/list"\n)\n\ntype FenwickTree struct {\n\tsize int // n: length of the array\n\tbitTree []int // bit: sto | DagmarC | NORMAL | 2024-04-15T13:24:52.633780+00:00 | 2024-04-15T13:24:52.633800+00:00 | 11 | false | \n# Code\n```\npackage main\n\nimport (\n\t"container/list"\n)\n\ntype FenwickTree struct {\n\tsize int // n: length of the array\n\tbitTree []int // bit: store the sum of range\n}\n\n// The NewFenwickTree function initializes a Fenwick tree data structure with the given array.\nfunc NewFenwickTree(n int) *Fenwick... | 0 | 0 | ['Binary Indexed Tree', 'Go'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python BinaryIndexedTree detailed solution | Time O(n*log(n)) | Space O(n) | python-binaryindexedtree-detailed-soluti-csnt | https://leetcode.ca/2020-01-13-1505-Minimum-Possible-Integer-After-at-Most-K-Adjacent-Swaps-On-Digits/\n\n# Intuition\n\nI highly recommend you to read this (ht | qodewerty | NORMAL | 2024-03-31T20:53:25.086989+00:00 | 2024-03-31T20:53:25.087020+00:00 | 36 | false | * https://leetcode.ca/2020-01-13-1505-Minimum-Possible-Integer-After-at-Most-K-Adjacent-Swaps-On-Digits/\n\n# Intuition\n\nI highly recommend you to read this (https://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/solutions/720548/o-n-logn-detailed-explanation/) well explained ... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python 3: FT 75: TC O(n*10*log(n)), SC O(n): Fenwick Tree and Adjusted Indices | python-3-ft-75-tc-on10logn-sc-on-fenwick-dxaa | Intuition\n\nThis is a tough one.\n\nWhat I realized is this:\n to minimize a number, you want to minimize the first digit\n that\'s because for any two numbers | biggestchungus | NORMAL | 2024-03-17T06:16:00.895474+00:00 | 2024-03-17T06:16:00.895510+00:00 | 10 | false | # Intuition\n\nThis is a tough one.\n\nWhat I realized is this:\n* to minimize a number, you want to minimize the first digit\n* that\'s because for any two numbers with prefixes p and p\' of the same length, if p < p\', then the first number is smaller\n* this is due to each digit being 10x larger than the next, so an... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | segment tree+lazy propagation solution | segment-treelazy-propagation-solution-by-qh1j | \n\n# Code\n\nclass Solution {\npublic:\nvector<int>seg,lazy;\nint query(int ind,int low,int high,int i){\n if(lazy[ind]!=0){\n seg[ind]+=(high-low+1) | Saiteja6 | NORMAL | 2024-01-29T12:56:56.219873+00:00 | 2024-01-29T12:56:56.219900+00:00 | 58 | false | \n\n# Code\n```\nclass Solution {\npublic:\nvector<int>seg,lazy;\nint query(int ind,int low,int high,int i){\n if(lazy[ind]!=0){\n seg[ind]+=(high-low+1)*lazy[ind];\n if(low!=high){\n lazy[2*ind+1]+=lazy[ind];\n lazy[2*ind+2]+=lazy[ind];\n }\n lazy[ind]=0;\n }\n ... | 0 | 0 | ['Segment Tree', 'C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Go - 100% Runtime & Memory | go-100-runtime-memory-by-frankdizzle-dtkn | Intuition\nStarting from the first digit and iterating forwards, swap in the lowest digit possible.\n\n# Approach\nBuild the solution in a new array of bytes.\n | frankdizzle | NORMAL | 2024-01-10T20:42:16.929038+00:00 | 2024-01-10T20:42:16.929078+00:00 | 6 | false | # Intuition\nStarting from the first digit and iterating forwards, swap in the lowest digit possible.\n\n# Approach\nBuild the solution in a new array of bytes.\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$ is an upper bound\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nfunc minInteger(num string, k int) string {\... | 0 | 0 | ['Go'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ Simple Segment Tree and Fenwick Tree O(n*log n) | c-simple-segment-tree-and-fenwick-tree-o-aezh | Code\n\nclass Solution {\n using Indices = std::vector<int>;\n\n static constexpr int kMaxIndex = 3 * 1e4;\n\n struct BIT{\n explicit BIT(int n) | nnuttertools | NORMAL | 2023-12-31T02:18:44.389622+00:00 | 2023-12-31T02:18:44.389638+00:00 | 45 | false | # Code\n```\nclass Solution {\n using Indices = std::vector<int>;\n\n static constexpr int kMaxIndex = 3 * 1e4;\n\n struct BIT{\n explicit BIT(int n) : n(n), t(n + 1, 0) {}\n\n void update(int i, int v) noexcept {\n for(++i; i < n; i += i & (-i)) {\n t[i] += v;\n ... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Solution using Fenwick Tree | solution-using-fenwick-tree-by-isheoran-hw8x | Complexity\n- Time complexity:\nO(n*log(n))\n\n- Space complexity:\nO(n) \n\n# Code\n\n\nclass BIT {\n vector<int>tree;\n int n;\npublic:\n BIT(){}\n\n | isheoran | NORMAL | 2023-11-18T13:26:04.153923+00:00 | 2023-11-18T13:26:04.153952+00:00 | 21 | false | # Complexity\n- Time complexity:\n$$O(n*log(n))$$\n\n- Space complexity:\n$$O(n)$$ \n\n# Code\n```\n\nclass BIT {\n vector<int>tree;\n int n;\npublic:\n BIT(){}\n\n BIT(int n) {\n tree.resize(n);\n this->n = n;\n }\n\n void add(int i,int val) {\n while(i < n) {\n tree[i... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Very Easy C++: Solution: Beats 94% | very-easy-c-solution-beats-94-by-raj_y-bjg2 | 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 | raj_y | NORMAL | 2023-09-04T22:32:48.268130+00:00 | 2023-09-04T22:32:48.268148+00:00 | 58 | 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- Space complexity:\n- O(3*n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npub... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | BIT|| COMMENTED |||QUEUE | bit-commented-queue-by-aasmaan_vs_aasmaa-9b4e | *Binary Indexed Tree (BIT):\n\nPurpose: The Binary Indexed Tree, also known as a Fenwick Tree, is used to efficiently perform prefix sum queries and updates. It | Aasmaan_Vs_Aasmaan | NORMAL | 2023-09-04T16:12:14.374532+00:00 | 2023-09-04T16:12:14.374550+00:00 | 14 | false | ****Binary Indexed Tree (BIT):\n****\nPurpose: The Binary Indexed Tree, also known as a Fenwick Tree, is used to efficiently perform prefix sum queries and updates. It helps keep track of the number of digits that have been removed from the original string, which is essential for selecting and processing digits during ... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | commented +segment tree+queue | commented-segment-treequeue-by-aasmaan_v-1olv | see this for best explanation\nhttps://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/solutions/720548/o-n-logn-detaile | Aasmaan_Vs_Aasmaan | NORMAL | 2023-09-04T15:59:02.885021+00:00 | 2023-09-04T16:00:18.879554+00:00 | 16 | false | see this for best explanation\nhttps://leetcode.com/problems/minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits/solutions/720548/o-n-logn-detailed-explanation/\n\nwhy\n\n1. **Segment Tree (`SegTree`):**\n - **Purpose:** The segment tree is used to keep track of which digits have been removed from the o... | 0 | 0 | ['C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Easy Java solution | easy-java-solution-by-thomasshelbyobe-2bik | Intuition\nGet the minimum in window of size k that starts from i+1. bring that minimum to front and decerease k accordingly.\n\n# Approach\n Describe your appr | ThomasShelbyOBE | NORMAL | 2023-07-16T16:47:12.522515+00:00 | 2023-07-16T16:47:12.522547+00:00 | 138 | false | # Intuition\nGet the minimum in window of size k that starts from i+1. bring that minimum to front and decerease k accordingly.\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<!-- A... | 0 | 0 | ['Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | simple segment tree solution || C++ | simple-segment-tree-solution-c-by-__a__j-a0gu | \n# Code\n\nclass Solution {\npublic:\n\n void build(int nl, int nr, int tidx, vector<int> &nums, vector<pair<int, int>> &tree) {\n if(nl == nr) {\n | __a__j | NORMAL | 2023-06-08T04:59:11.555337+00:00 | 2023-06-08T04:59:11.555405+00:00 | 77 | false | \n# Code\n```\nclass Solution {\npublic:\n\n void build(int nl, int nr, int tidx, vector<int> &nums, vector<pair<int, int>> &tree) {\n if(nl == nr) {\n tree[tidx] = {nums[nl], nl};\n return;\n }\n int mid = (nl+nr)/2;\n\n build(nl, mid, 2*tidx+1, nums, tree);\n ... | 0 | 0 | ['Segment Tree', 'C++'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python (Simple BIT) | python-simple-bit-by-rnotappl-r0ss | 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 | rnotappl | NORMAL | 2023-04-08T08:41:17.707439+00:00 | 2023-04-08T08:41:17.707481+00:00 | 139 | 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)$$ --... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | c++ segment tree with 46ms runtime | c-segment-tree-with-46ms-runtime-by-idvj-mgts | \nclass Solution {\npublic:\n string ans;\n int tree[12*10001],lazy[12*10001],count;\n void Lazy(int id)\n {\n tree[id*2]+=lazy[id];\n | idvjojo123 | NORMAL | 2023-03-10T17:36:43.168726+00:00 | 2023-03-10T17:36:43.168759+00:00 | 43 | false | ```\nclass Solution {\npublic:\n string ans;\n int tree[12*10001],lazy[12*10001],count;\n void Lazy(int id)\n {\n tree[id*2]+=lazy[id];\n lazy[id*2]+=lazy[id];\n tree[id*2+1]+=lazy[id];\n lazy[id*2+1]+=lazy[id];\n lazy[id]=0;\n }\n void update(int id,int l,int r,int ... | 0 | 0 | ['Queue', 'C'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Just a runnable solution | just-a-runnable-solution-by-ssrlive-nh6h | Code\n\nimpl Solution {\n pub fn min_integer(num: String, k: i32) -> String {\n let mut k = k as usize;\n let num = num.into_bytes();\n | ssrlive | NORMAL | 2023-02-09T04:05:58.861774+00:00 | 2023-02-09T04:11:23.949995+00:00 | 18 | false | # Code\n```\nimpl Solution {\n pub fn min_integer(num: String, k: i32) -> String {\n let mut k = k as usize;\n let num = num.into_bytes();\n let n = num.len();\n let mut res = Vec::with_capacity(n);\n let mut q = vec![n; 10];\n for (i, &item) in num.iter().enumerate() {\n ... | 0 | 0 | ['Rust'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | O(NlogN), a SortedList and 10 queues | onlogn-a-sortedlist-and-10-queues-by-kaa-u04c | Intuition\nIterating from the left, we want to find the smallest reacheable digit and put it on each place.\n\n# Approach\nWe keep 10 queues, containing sequenc | kaathewise | NORMAL | 2023-01-17T15:44:27.524674+00:00 | 2023-01-17T15:44:27.524701+00:00 | 126 | false | # Intuition\nIterating from the left, we want to find the smallest reacheable digit and put it on each place.\n\n# Approach\nWe keep 10 queues, containing sequences of indices for each digit. At each step we pick the smallest digit that is not further than `k` steps away. After that we remove it from its queue, and red... | 0 | 0 | ['Python3'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Dart solution, O(n^2), LinkedList | dart-solution-on2-linkedlist-by-yakiv_ga-litb | Intuition\nWhen swapping (similar to bubble sort) numbers in a string - we should start minimising number from the left side by swapping every digit with the lo | yakiv_galkin | NORMAL | 2022-12-12T09:07:58.498765+00:00 | 2022-12-12T09:07:58.498812+00:00 | 40 | false | # Intuition\nWhen swapping (similar to bubble sort) numbers in a string - we should start minimising number from the left side by swapping every digit with the lowest possible number from the remaining ight side within the K limit.\n\n\n# Approach\n\nWhile the idea is prerry simple I faced timeout error on submission w... | 0 | 0 | ['Dart'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | O(nlogn) sliding window and AVL tree | onlogn-sliding-window-and-avl-tree-by-ma-kaq5 | \n from sortedcontainers import SortedList\n\n n = len(num)\n window = SortedList() # sliding window of size k + 1, store (value, index) o | mathorlee | NORMAL | 2022-11-27T00:57:08.438280+00:00 | 2022-11-27T00:57:54.166330+00:00 | 69 | false | ```\n from sortedcontainers import SortedList\n\n n = len(num)\n window = SortedList() # sliding window of size k + 1, store (value, index) of num\n remains = SortedList(range(n)) # remained remains\n pops = [] # popped indices\n\n while k > 0:\n # add to keep win... | 0 | 0 | ['Sliding Window'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | segment tree + BIT || CPP | segment-tree-bit-cpp-by-vvd4-bdpr | \nclass Solution {\npublic:\n \n int Do(int a,int b,string &s){\n if(a==s.size())return b;\n if(b==s.size())return a;\n if(s[a]<=s[b] | vvd4 | NORMAL | 2022-09-22T16:30:39.607305+00:00 | 2022-09-22T16:30:39.607351+00:00 | 34 | false | ```\nclass Solution {\npublic:\n \n int Do(int a,int b,string &s){\n if(a==s.size())return b;\n if(b==s.size())return a;\n if(s[a]<=s[b])return a;\n return b;\n }\n \n void build(vector<int> &seg,int si,int sl,int sr,string &s){\n if(sl==sr){\n seg[si]=sl;\n ... | 0 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | C++ Segment Tree | O(NlogN) | Cleanest Code | c-segment-tree-onlogn-cleanest-code-by-g-d3in | \nclass SegTree {\npublic:\n vector<int> nodes;\n int n;\n\n //constructor\n SegTree(int n) {\n this->n = n;\n nodes = vector<int>(thi | GeekyBits | NORMAL | 2022-09-13T12:36:12.656712+00:00 | 2022-09-13T16:54:22.628296+00:00 | 56 | false | ```\nclass SegTree {\npublic:\n vector<int> nodes;\n int n;\n\n //constructor\n SegTree(int n) {\n this->n = n;\n nodes = vector<int>(this->n << 2);\n }\n\n void update(int ss, int se, int ql, int idx, int val) {\n //update[ql,ql]\n if (ql<ss or ql>se) {\n return... | 0 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++, O(nlogn)] Segment Tree | c-onlogn-segment-tree-by-arpitpandey992-ih65 | \nclass Solution {\n class SegTree {\n vector<int> st;\n int siz;\n\n void upd(int i, int k) {\n i += siz;\n st[i] | arpitpandey992 | NORMAL | 2022-09-06T10:16:50.527968+00:00 | 2022-09-06T10:16:50.528001+00:00 | 49 | false | ```\nclass Solution {\n class SegTree {\n vector<int> st;\n int siz;\n\n void upd(int i, int k) {\n i += siz;\n st[i]+= k;\n i /= 2;\n while (i > 0) {\n st[i] = st[i * 2] + st[i * 2 + 1];\n i /= 2;\n }\n ... | 0 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python Code | python-code-by-athenalei-arz9 | \nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n num = list(num)\n BIT = [0] *(n+1)\n counter | athenalei | NORMAL | 2022-08-21T17:16:41.177650+00:00 | 2022-08-21T17:17:21.039172+00:00 | 64 | false | ```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n num = list(num)\n BIT = [0] *(n+1)\n counter = defaultdict(deque)\n for idx, val in enumerate(num):\n counter[val].append(idx)\n def update(i,x):\n while i< len(BIT):... | 0 | 0 | [] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Java | O(nlogn) 30ms | Check Pos By Pos NOT Digit by Digit | java-onlogn-30ms-check-pos-by-pos-not-di-2kvw | Wrong Strategy\nInitially I tried to arrange all the 0s first, then 1s, 2s ..., but that strategy is flawed when there are multiple identical numbers together l | Student2091 | NORMAL | 2022-08-08T21:07:47.418323+00:00 | 2022-08-08T21:14:45.381263+00:00 | 281 | false | #### Wrong Strategy\nInitially I tried to arrange all the 0s first, then 1s, 2s ..., but that strategy is flawed when there are multiple identical numbers together like "235288888...1" It is because when we are looking at number 1, we conclude that the last 1 can\'t be moved to the head of whatever current is, so we sk... | 0 | 0 | ['Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | MY INTUITIVE JAVA SOLUTION | my-intuitive-java-solution-by-divyaratho-i7d4 | \nclass Solution {\n public String minInteger(String s, int k) {\n int length = s.length();\n int[] num = new int[length];\n int i = 0;\ | DivyaRathod3D | NORMAL | 2022-08-08T09:19:05.567315+00:00 | 2022-08-08T09:19:05.567353+00:00 | 128 | false | ```\nclass Solution {\n public String minInteger(String s, int k) {\n int length = s.length();\n int[] num = new int[length];\n int i = 0;\n for(char ch : s.toCharArray()) \n num[i++] = Integer.parseInt(String.valueOf(ch));\n \n\t\tfor (i = 0; i < length && k > 0; i++){\... | 0 | 0 | ['Java'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | Python Fenwick Tree solution | python-fenwick-tree-solution-by-liulaoye-ouj9 | \nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n tr = [0 for i in range(n+1)]\n def add(ind,d):\n | liulaoye135 | NORMAL | 2022-06-02T05:06:51.629339+00:00 | 2022-06-02T05:06:51.629381+00:00 | 147 | false | ```\nclass Solution:\n def minInteger(self, num: str, k: int) -> str:\n n = len(num)\n tr = [0 for i in range(n+1)]\n def add(ind,d):\n while ind<=n:\n tr[ind] += d\n ind += -ind&ind\n def query(ind):\n res = 0\n while ind:\n ... | 0 | 0 | ['Tree'] | 0 |
minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits | [C++] Segment Tree | Greedy | O(nlogn) Time | c-segment-tree-greedy-onlogn-time-by-mek-lxoe | \n/* \n Time: O(nlogn)\n Space: O(n)\n Tag: Segment Tree, Greedy (put the possible feasible smallest index in place of cur index), Queue\n Difficult | meKabhi | NORMAL | 2022-05-30T19:53:29.962990+00:00 | 2022-05-30T19:53:29.963017+00:00 | 564 | false | ```\n/* \n Time: O(nlogn)\n Space: O(n)\n Tag: Segment Tree, Greedy (put the possible feasible smallest index in place of cur index), Queue\n Difficulty: H (Both Logic and Implementation)\n*/\n\nclass SegmentTree {\n vector<int> tree;\n\npublic:\n SegmentTree(int size) {\n tree.resize(4 * size ... | 0 | 0 | ['Greedy', 'Tree', 'Queue', 'C', 'C++'] | 0 |
stone-game-v | [Java] O(n^3), O(n^2 log n) and O(n^2) with explanation | java-on3-on2-log-n-and-on2-with-explanat-3ic1 | O(n^3)\n\nBasic approach\ndp[i][j]: max score you can obtain from stones[i..j]\nsum[i][j]: sum of stoneValues[i..j]\nTry all possible k i.e. k goes from i to j- | shk10 | NORMAL | 2020-10-26T23:14:46.177330+00:00 | 2020-10-26T23:27:12.930447+00:00 | 6,444 | false | **O(n^3)**\n\n**Basic approach**\ndp[i][j]: max score you can obtain from stones[i..j]\nsum[i][j]: sum of stoneValues[i..j]\nTry all possible k i.e. k goes from i to j-1:\nwe have 2 choices for score: **sum[i][k] + dp[i][k]** and **sum[k+1][j] + dp[k+1][j]**\nbut we can only pick the side where sum is smaller or either... | 167 | 2 | ['Binary Search', 'Dynamic Programming', 'Java'] | 13 |
stone-game-v | [C++] Prefix Sum + DP (Memoization) | c-prefix-sum-dp-memoization-by-phoenixdd-17xr | [UPDATE] \nNew test cases added, forcing the solution to be O(n^2). Please consider looking at other solutions, I\'ll update this post once I find time to do so | phoenixdd | NORMAL | 2020-08-23T04:01:07.108510+00:00 | 2021-05-15T23:35:07.175634+00:00 | 7,848 | false | **[UPDATE]** \nNew test cases added, forcing the solution to be `O(n^2)`. Please consider looking at other solutions, I\'ll update this post once I find time to do so.\n**Observation**\n1. We can simply do what\'s stated in the question and try all the possible partitions.\n1. If the sum of left row is less recur on th... | 74 | 13 | [] | 11 |
stone-game-v | [Java] Detailed Explanation - Easy Understand DFS + Memo - Top-Down DP | java-detailed-explanation-easy-understan-bj02 | Key Notes:\n- Classic DP problem\n- Top-Down (DFS + Memo) is easy to understand\n- Try split array at every possible point:\n\t- get left sum and right sum, cho | frankkkkk | NORMAL | 2020-08-23T04:00:43.118102+00:00 | 2020-08-23T04:00:43.118160+00:00 | 3,413 | false | **Key Notes:**\n- Classic DP problem\n- Top-Down (DFS + Memo) is **easy to understand**\n- Try split array at every possible point:\n\t- get left sum and right sum, choose the smaller one and keep searching\n\t- to save some time, we can pre-compute a **prefix array** to quick calculate sum\n\t- if left == right: try b... | 46 | 5 | [] | 4 |
stone-game-v | [Python] DP | python-dp-by-lee215-fhar | Update 2020/09/01:\nSeems new test cases added and enforce it to be O(N^2).\nThis solution is TLE now.\n\n## Intuition\nWe have done stone games many times.\nJu | lee215 | NORMAL | 2020-08-23T04:02:45.226779+00:00 | 2020-09-02T14:34:26.130979+00:00 | 6,140 | false | **Update 2020/09/01:**\nSeems new test cases added and enforce it to be O(N^2).\nThis solution is TLE now.\n\n## **Intuition**\nWe have done stone games many times.\nJust dp it.\n<br>\n\n## **Complexity**\nTime `O(N^3)`\nSpace `O(N^2)`\n<br>\n\n\n**Python:**\n```py\n def stoneGameV(self, A):\n n = len(A)\n ... | 44 | 8 | [] | 22 |
stone-game-v | [C++] O(n^2log(n)) solution and why top-down approach is faster | c-on2logn-solution-and-why-top-down-appr-lkra | I get my O(n^3) solution TLE in the contest and I passed my O(n^2log(n)) solution at 1:32 (so sad....). I managed to make my O(n^3) solution accepted at last :) | chenhao4 | NORMAL | 2020-08-23T04:18:12.181690+00:00 | 2020-08-23T07:19:23.213541+00:00 | 2,628 | false | I get my O(n^3) solution TLE in the contest and I passed my O(n^2log(n)) solution at 1:32 (so sad....). I managed to make my O(n^3) solution accepted at last :)\n\nConsider the initialized state of this problem and we have an interval [1, N] to solve. For all the possible ways to split this interval, we will always ign... | 34 | 1 | [] | 9 |
stone-game-v | [C++] O(N^2) DP | c-on2-dp-by-lee0560-euhr | In the intuitive O(N^3) DP, to calculate the maximum score h[i][j], we need to iterate through any k that i <= k < j. But if we examine the relationship amon | lee0560 | NORMAL | 2020-08-23T09:36:01.028088+00:00 | 2020-08-23T09:36:01.028123+00:00 | 2,706 | false | In the intuitive O(N^3) DP, to calculate the maximum score h[i][j], we need to iterate through any k that i <= k < j. But if we examine the relationship among h[i][j], h[i][j+1] and h[i-1][j], it\'s possible to derive an optimized O(N^2) solution.\n\nLet\'s split the candidates of h[i][j] into 2 groups, depending on... | 33 | 0 | [] | 5 |
stone-game-v | Why my C++ O(n^3) solution will TLE | why-my-c-on3-solution-will-tle-by-szzzwn-v8ga | Code as follows:\n\n\nclass Solution {\npublic:\n int stoneGameV(vector<int>& s) {\n int n = s.size();\n vector<int> prefix(n);\n prefix | szzzwno123 | NORMAL | 2020-08-23T04:04:34.537312+00:00 | 2020-08-23T04:25:57.028205+00:00 | 2,149 | false | Code as follows:\n\n```\nclass Solution {\npublic:\n int stoneGameV(vector<int>& s) {\n int n = s.size();\n vector<int> prefix(n);\n prefix[0] = s[0];\n \n for (int i = 1; i < n; i++)\n prefix[i] = prefix[i - 1] + s[i];\n \n vector<vector<int>> dp(n, vector... | 28 | 2 | [] | 21 |
stone-game-v | Java Top-down DP | java-top-down-dp-by-hobiter-00y7 | dp[fr][to] is the max points that you can gain if you pick sub array from fr to to;\nCache the result to avoid duplicate calculation\n\n\nclass Solution {\n | hobiter | NORMAL | 2020-08-23T04:01:56.038524+00:00 | 2020-08-23T04:01:56.038574+00:00 | 1,151 | false | dp[fr][to] is the max points that you can gain if you pick sub array from fr to to;\nCache the result to avoid duplicate calculation\n\n```\nclass Solution {\n int[][] dp;\n int[] acc;\n public int stoneGameV(int[] ss) {\n int m = ss.length;\n dp = new int[m][m];\n acc = new int[m + 1];\n ... | 24 | 1 | [] | 3 |
stone-game-v | Please do not assume monotonicity like stoneGameV(A) ≥ stoneGameV(A[:-1]) | please-do-not-assume-monotonicity-like-s-4bda | I read a few posts and some of the "fast" DP solutions implicitly assumed that the result is non-decreasing as the list goes longer and longer. This is not true | 3abc | NORMAL | 2020-10-11T05:49:44.785289+00:00 | 2020-10-11T07:40:28.847665+00:00 | 460 | false | I read a few posts and some of the "fast" DP solutions implicitly assumed that the result is non-decreasing as the list goes longer and longer. This is not true. The simpliest counter example being `[1,3,2,2,1]`, we have:\n\n`stoneGameV([1,3,2,2]) = 6` and `stoneGameV([1,3,2,2,1]) = 5`.\n\n(To achieve the max for `[1,3... | 20 | 0 | [] | 5 |
stone-game-v | [Python / Golang] Simple DP Solution with a Trick for Python | python-golang-simple-dp-solution-with-a-i6jx5 | Idea\nThe idea is straight forward. we just try out all the possibilities to find the answer. Naive brute force solution will lead to O(n!) complexity, so we ne | yanrucheng | NORMAL | 2020-08-23T04:01:31.985745+00:00 | 2020-08-23T04:08:24.284720+00:00 | 1,509 | false | **Idea**\nThe idea is straight forward. we just try out all the possibilities to find the answer. Naive brute force solution will lead to O(n!) complexity, so we need to store the interim results to improve the complexity to O(n^3).\n\n**A trick for slower languages**\nSince I got TLE for the O(n^3) solution in Python.... | 20 | 6 | ['Go', 'Python3'] | 3 |
stone-game-v | [C++] O(n^2)-Time DP Solution: No Binary Search Needed | c-on2-time-dp-solution-no-binary-search-2h0qb | Let a[1..n] denote the values of stones, s(i, j) denote the sum a[i]+a[i+1]+...+a[j], and D(i, j) denote the maximum score of a[i..j]. We first calculate D(1, 1 | xavier13540 | NORMAL | 2020-08-23T17:01:34.189572+00:00 | 2020-08-23T17:10:06.876255+00:00 | 1,353 | false | Let *a[1..n]* denote the values of stones, *s(i, j)* denote the sum *a[i]+a[i+1]+...+a[j]*, and *D(i, j)* denote the maximum score of *a[i..j]*. We first calculate *D(1, 1), D(2, 2), ..., D(n, n)*, then calculate *D(1, 2), D(2, 3), ..., D(n-1, n)*, then calculate *D(1, 3), D(2, 4), ..., D(n-2, n)*, and so on. Note that... | 19 | 1 | [] | 3 |
stone-game-v | Some test cases that refuse plausible assumptions | some-test-cases-that-refuse-plausible-as-mljt | Some may wonder if we can turn typical O(n^3) dynamic programming algorithm (can be seen in many other posts) into O(n^2), by some strong assumptions. Here are | szp14 | NORMAL | 2020-08-23T09:37:36.222828+00:00 | 2021-06-13T01:30:50.573080+00:00 | 456 | false | Some may wonder if we can turn typical O(n^3) dynamic programming algorithm (can be seen in many other posts) into O(n^2), by some strong assumptions. Here are some assumptions seeming to be correct but in fact wrong:\n\n1. If array **A** is a subarray of **B**, then Alice\'s score over **B** can\'t be lower than that ... | 15 | 0 | [] | 4 |
stone-game-v | C++/Python partial_sum + DFS | cpython-partial_sum-dfs-by-votrubac-d48p | This is similar to other DP problems when you find the best split point k for a range [i, j]. To avoid recomputation, we memoise the best answer in dp[i][j].\n\ | votrubac | NORMAL | 2020-08-23T04:01:47.062178+00:00 | 2020-08-23T05:09:43.637461+00:00 | 1,763 | false | This is similar to other DP problems when you find the best split point `k` for a range `[i, j]`. To avoid recomputation, we memoise the best answer in `dp[i][j]`.\n\nTo speed things up, we first convert our array into a prefix sum array, so that we can easily get the sum of interval `[i, j]` as `v[j] - v[i - 1]`. \n\n... | 15 | 0 | [] | 4 |
stone-game-v | Java | TopDown DP | Prefix Sum | Optimized: pruning recursion using sum of geometric sequence | java-topdown-dp-prefix-sum-optimized-pru-ix0f | Look for this key pruning idea to speed up recursion: if(2 x Math.min(l,r)<max) continue; (the DP works without it but is much slower).\nNo need to consider an | prezes | NORMAL | 2021-06-01T23:27:41.785021+00:00 | 2021-08-02T04:25:50.410187+00:00 | 449 | false | Look for this key pruning idea to speed up recursion: if(2 x Math.min(l,r)<max) continue; (the DP works without it but is much slower).\nNo need to consider any branches with the sum smaller than the max score known so far, as for a given subset of stones with sum of values equal a, the total score cannot be bigger th... | 9 | 0 | [] | 3 |
stone-game-v | Optimal Strategy for Stone Game: Maximizing Alice's Score with Memoization | optimal-strategy-for-stone-game-maximizi-g5yc | Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve this problem, we can use dynamic programming. We can create a memoization tabl | Ratan_md | NORMAL | 2023-08-03T03:07:28.349617+00:00 | 2023-08-03T03:07:28.349640+00:00 | 327 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, we can use dynamic programming. We can create a memoization table to keep track of the maximum scores Alice can obtain for each subproblem. The subproblem can be defined as the range of stones from index i to j.\n\n... | 8 | 0 | ['C++'] | 7 |
stone-game-v | 💥💥Beats 100% on runtime and memory [EXPLAINED] | beats-100-on-runtime-and-memory-explaine-th3p | \n\n\n\n# Intuition\nThe goal is to maximize Alice\'s score by strategically splitting the stones into two non-empty parts, ensuring she always benefits from th | r9n | NORMAL | 2024-09-26T21:05:24.715310+00:00 | 2024-09-26T21:06:58.727602+00:00 | 75 | false | \n\n\n\n# Intuition\nThe goal is to maximize Alice\'s score by strategically splitting the stones into two non-empty parts, ensuring she always benefits from the remaining stones after Bob discards the high... | 7 | 0 | ['C#'] | 0 |
stone-game-v | [Python] 350ms - beats 98% | python-350ms-beats-98-by-wdhiuq-voiz | Intuition\nMemoized solution can take advantage of some constraints to shrink search space. \n\n# Approach\nWhen considering a subproblem (solution for contiguo | wdhiuq | NORMAL | 2022-12-18T00:06:58.300216+00:00 | 2022-12-18T21:03:11.195910+00:00 | 530 | false | # Intuition\nMemoized solution can take advantage of some constraints to shrink search space. \n\n# Approach\nWhen considering a subproblem (solution for contiguos sublist of the original list) there is no need to check all possible divisions of the sublist, since there is an upper bound on the value of the solution. S... | 6 | 0 | ['Python3'] | 0 |
stone-game-v | Python O(n^2) optimized solution. O(n^3) cannot pass. | python-on2-optimized-solution-on3-cannot-edfb | Python is slow sometimes!\n\nTraditional DP: \n\nfor i in (0, length): \n\tfor j in (0, length): \n\t\tfor k in (i, j): \n\t\t\tdp[i][j] = dp[i][k] + s[i][k]] o | pureme | NORMAL | 2021-10-05T17:33:51.862093+00:00 | 2021-10-05T17:39:58.543435+00:00 | 820 | false | Python is slow sometimes!\n\nTraditional DP: \n```\nfor i in (0, length): \n\tfor j in (0, length): \n\t\tfor k in (i, j): \n\t\t\tdp[i][j] = dp[i][k] + s[i][k]] or dp[k+1][j] + s[k+1][j]\n```\n\nThe hardest thing for us to optimize the O(n^3) is finding a way to reduce the innest loop for k in (i, j), but it\'s not so... | 6 | 0 | ['Dynamic Programming', 'Python3'] | 2 |
stone-game-v | Is this question not meant for python? | is-this-question-not-meant-for-python-by-xgss | \nI have submitted most optimized code but even that one gives TLE in python.\n | shreyashnahar0 | NORMAL | 2021-05-19T06:29:28.531936+00:00 | 2021-05-19T06:31:49.262945+00:00 | 195 | false | ```\nI have submitted most optimized code but even that one gives TLE in python.\n``` | 5 | 0 | [] | 0 |
stone-game-v | JAVA Solution USING MCM (matrix chain multiplication) and prefix sum | java-solution-using-mcm-matrix-chain-mul-2y8l | \'\'\' \nclass Solution {\n public int stoneGameV(int[] s) {\n \n int pre[]=new int[s.length];\n pre[0]=s[0];\n\t\t// calculate prefix s | abhishekbabbar1989 | NORMAL | 2021-03-30T09:50:49.940167+00:00 | 2021-03-30T09:54:50.459296+00:00 | 353 | false | \'\'\' \nclass Solution {\n public int stoneGameV(int[] s) {\n \n int pre[]=new int[s.length];\n pre[0]=s[0];\n\t\t// calculate prefix sum\n for(int i=1;i<s.length;i++)\n {\n pre[i]=pre[i-1]+s[i];\n }\n \n int dp[][]=new int[501][501];\n for(i... | 5 | 0 | [] | 1 |
stone-game-v | python top down dp with thinking process | python-top-down-dp-with-thinking-process-4l1q | The first time Alice is picking, she needs to decide a cut point s.t. she get max score. The first intuision is we go through each potential cut point and find | ytb_algorithm | NORMAL | 2021-01-19T02:31:38.202307+00:00 | 2021-01-19T02:31:38.202338+00:00 | 709 | false | The first time Alice is picking, she needs to decide a cut point s.t. she get max score. The first intuision is we go through each potential cut point and find the most optimized score. The following is the code. \n```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n def dfs(start, end... | 5 | 1 | ['Dynamic Programming', 'Binary Tree', 'Python3'] | 0 |
stone-game-v | [Python3] Game Theory + DP + Prefix Sum - Simple Solution | python3-game-theory-dp-prefix-sum-simple-p6r0 | 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 | dolong2110 | NORMAL | 2024-09-10T12:24:14.903353+00:00 | 2024-09-10T14:03:50.661535+00:00 | 142 | 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)$$ --... | 4 | 0 | ['Array', 'Math', 'Dynamic Programming', 'Prefix Sum', 'Python3'] | 1 |
stone-game-v | Cpp || dynamic programming || memoisation | cpp-dynamic-programming-memoisation-by-s-fwoz | Intuition\n Describe your first thoughts on how to solve this problem. \nAs problem gives us option to choose which part to throw , this gives the intuition tha | shankar999 | NORMAL | 2023-05-26T09:22:52.790741+00:00 | 2023-05-26T09:23:47.889365+00:00 | 991 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs problem gives us option to choose which part to throw , this gives the intuition that we try all the possibilities and hence dp can be used to memoise those states. \n\n# Approach\n<!-- Describe your approach to solving the problem. --... | 4 | 0 | ['C++'] | 0 |
stone-game-v | C++ (Recursion to DP) Intitutive Way | c-recursion-to-dp-intitutive-way-by-vfor-tg1r | ```\nclass Solution {\npublic:\n //map,int>mp;\n //I got tle using map\n \n int memo[501][501];\n int score(vector&nums,int st,int end)\n {\n | vforvibhu | NORMAL | 2020-10-26T07:52:20.233968+00:00 | 2020-10-26T07:52:20.234012+00:00 | 570 | false | ```\nclass Solution {\npublic:\n //map<pair<int,int>,int>mp;\n //I got tle using map\n \n int memo[501][501];\n int score(vector<int>&nums,int st,int end)\n {\n if(st>=end)return 0;\n int ans=0;\n //if(mp.find({st,end})!=mp.end())return mp[{st,end}];\n if(memo[st][end]!=-1)... | 4 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++'] | 0 |
stone-game-v | O(N^2) A (hopefully) confusion-free, intuitive explanation | on2-a-hopefully-confusion-free-intuitive-k7mr | Intuitive brute force\nIt is quite simple to com up with O(N^3) solution. Say we have input array size 8, [1,7,3,5,2,9,4,7], we can just try to "cut" the line a | XavierWantMoreMoney | NORMAL | 2022-02-08T08:13:49.821704+00:00 | 2022-02-08T08:13:49.821761+00:00 | 261 | false | ### Intuitive brute force\nIt is quite simple to com up with O(N^3) solution. Say we have input array size 8, `[1,7,3,5,2,9,4,7]`, we can just try to "cut" the line at all the possible places\n\n```\n[1, | 7,3,5,2,9,4,7]\n[1,7, | 3,5,2,9,4,7]\n[1,7,3, | 5,2,9,4,7]\n...\n```\nFor each cut, we calculate which half is big... | 3 | 0 | ['C'] | 1 |
stone-game-v | python3 O(N^2) beats 98% | python3-on2-beats-98-by-miao_txy-nne6 | idx is the first point in [l, r]\nwhere sum([l, idx]) > sum([idx + 1, r])\n\n\tfrom functools import lru_cache\n\timport itertools\n\n\n\tclass Solution:\n\t\td | miao_txy | NORMAL | 2020-08-26T11:49:04.312771+00:00 | 2020-08-26T11:51:00.910100+00:00 | 440 | false | idx is the first point in [l, r]\nwhere sum([l, idx]) > sum([idx + 1, r])\n\n\tfrom functools import lru_cache\n\timport itertools\n\n\n\tclass Solution:\n\t\tdef stoneGameV(self, A) -> int:\n\n\t\t\tpre = [0] + list(itertools.accumulate(A))\n\n\t\t\tn = len(A)\n\t\t\tidx = [[0] * n for _ in range(n)]\n\t\t\tfor i in r... | 3 | 0 | [] | 2 |
stone-game-v | Simple DP solution | simple-dp-solution-by-leoooooo-nqxv | \npublic class Solution\n{\n public int StoneGameV(int[] stoneValue)\n {\n int[] presum = new int[stoneValue.Length + 1];\n for (int i = 0; | leoooooo | NORMAL | 2020-08-23T04:22:13.546938+00:00 | 2020-08-23T04:29:47.014426+00:00 | 360 | false | ```\npublic class Solution\n{\n public int StoneGameV(int[] stoneValue)\n {\n int[] presum = new int[stoneValue.Length + 1];\n for (int i = 0; i < stoneValue.Length; i++)\n {\n presum[i + 1] = presum[i] + stoneValue[i];\n }\n return DFS(presum, 0, stoneValue.Length, n... | 3 | 0 | [] | 1 |
stone-game-v | Why TLE on O(n^3)? DP Merging intervals. C++ | why-tle-on-on3-dp-merging-intervals-c-by-ah42 | Time Limit Exceeded on 131th test case. During contest I was hitting TLE continuously.\nBut copying and pasting the larger test cases in custom input was work | mr_robot11 | NORMAL | 2020-08-23T04:16:42.318469+00:00 | 2020-08-23T04:31:15.254681+00:00 | 332 | false | Time Limit Exceeded on 131th test case. During contest I was hitting TLE continuously.\nBut copying and pasting the larger test cases in custom input was working fine.\n```\nWhy there was no TLE when I was running the same test case using custom input? \n```\n\n```\nIn the discussion everyone\'s solution is O(n^3) ti... | 3 | 0 | [] | 0 |
stone-game-v | [Python3] top-down dp | python3-top-down-dp-by-ye15-nrjy | \n\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n # prefix sum \n prefix = [0]\n for x in stoneValue: prefix.a | ye15 | NORMAL | 2020-08-23T04:04:33.492789+00:00 | 2020-08-23T23:58:03.141134+00:00 | 370 | false | \n```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n # prefix sum \n prefix = [0]\n for x in stoneValue: prefix.append(prefix[-1] + x)\n \n @lru_cache(None)\n def fn(lo, hi):\n """Return the score of arranging values from lo (inclusive) t... | 3 | 2 | ['Python3'] | 2 |
stone-game-v | Java | Memoization | DP | Beats 80% | java-memoization-dp-beats-80-by-nishant7-7lev | java\nclass Solution {\n Integer[][] dp;\n public int stoneGameV(int[] stoneValue) {\n dp = new Integer[stoneValue.length][stoneValue.length];\n | nishant7372 | NORMAL | 2023-07-15T09:11:04.677184+00:00 | 2023-07-15T09:11:04.677246+00:00 | 180 | false | ``` java\nclass Solution {\n Integer[][] dp;\n public int stoneGameV(int[] stoneValue) {\n dp = new Integer[stoneValue.length][stoneValue.length];\n int total = 0;\n for(int x:stoneValue){\n total+=x;\n }\n return solve(stoneValue,0,stoneValue.length-1,total);\n }\... | 2 | 0 | ['Memoization', 'Java'] | 0 |
stone-game-v | C++ Memoization | c-memoization-by-rajatrajoria-mtld | \nclass Solution \n{\npublic:\n \n int fn(vector<int> &nums, int l, int r, int total, vector<vector<int>> &dp)\n {\n if(l>=r) // Game | rajatrajoria | NORMAL | 2022-07-29T13:04:54.204952+00:00 | 2022-07-29T13:04:54.205015+00:00 | 286 | false | ```\nclass Solution \n{\npublic:\n \n int fn(vector<int> &nums, int l, int r, int total, vector<vector<int>> &dp)\n {\n if(l>=r) // Game ends here.\n return 0;\n if(dp[l][r]!=-1) // This state was already precomputed.\n return dp[l][r];\n \n int s... | 2 | 0 | ['Dynamic Programming', 'Memoization', 'C', 'C++'] | 0 |
stone-game-v | C++ || EASY TO UNDERSTAND || Memoization || DP | c-easy-to-understand-memoization-dp-by-a-a199 | \nclass Solution {\npublic:\n int fun(vector<int> &v,int l,int r,vector<vector<int> > &dp)\n {\n if(dp[l][r]!=-1)\n return dp[l][r];\n | aarindey | NORMAL | 2022-04-12T19:30:59.091871+00:00 | 2022-04-12T19:30:59.091911+00:00 | 160 | false | ```\nclass Solution {\npublic:\n int fun(vector<int> &v,int l,int r,vector<vector<int> > &dp)\n {\n if(dp[l][r]!=-1)\n return dp[l][r];\n int total=accumulate(v.begin()+l,v.begin()+r+1,0);\n int sum=0,res=0; \n if(l>=r)\n return 0;\n for(int i=l;i<r;i++)\n ... | 2 | 0 | [] | 0 |
stone-game-v | Java Simple and easy DP - memoization solution,, clean code with comments | java-simple-and-easy-dp-memoization-solu-0hiz | PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\n\nclass Solution {\n Integer[][] cache;\n \n public int stoneGameV(int[] stoneValue) {\n int n = st | satyaDcoder | NORMAL | 2021-06-18T03:51:18.912888+00:00 | 2021-06-18T03:51:18.912920+00:00 | 317 | false | **PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n\n```\nclass Solution {\n Integer[][] cache;\n \n public int stoneGameV(int[] stoneValue) {\n int n = stoneValue.length;\n \n cache = new Integer[n + 1][n + 1];\n \n //cumlative sum of stone values\n int[] prefixSum = new ... | 2 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 1 |
stone-game-v | C++ MCM solution, easy to understand. | c-mcm-solution-easy-to-understand-by-sha-d6hz | \n\t class Solution {\npublic:\n vector<int> pre;\n vector<vector<int>> dp;\n int helper(vector<int>&arr,int i,int j){\n if(i>=j){ // base case | shakiralam2017 | NORMAL | 2021-03-30T11:53:20.972660+00:00 | 2021-04-04T14:40:30.630008+00:00 | 266 | false | ```\n\t class Solution {\npublic:\n vector<int> pre;\n vector<vector<int>> dp;\n int helper(vector<int>&arr,int i,int j){\n if(i>=j){ // base case , element should be atleat one to divide the array.\n return 0;\n }\n if(dp[i][j] != -1){\n return dp[i][j];\n }\n... | 2 | 0 | [] | 0 |
stone-game-v | C++. Simple, DP + recursion, O(n^3), Straightforward, Easy to understand | c-simple-dp-recursion-on3-straightforwar-7t4n | pref is used to store prefix sum of the array.\n dp[i][j] is used to store answer for subarray {i, i+1, i+2,....., j-1, j}\n\n\nclass Solution {\npublic:\n | zeddie | NORMAL | 2020-11-06T11:15:21.740578+00:00 | 2020-12-07T06:47:29.326127+00:00 | 264 | false | <strong> pref </strong> is used to store prefix sum of the array.\n<strong> dp[i][j]</strong> is used to store answer for subarray {i, i+1, i+2,....., j-1, j}\n\n```\nclass Solution {\npublic:\n vector< vector<long> > dp;\n vector< long> pref;\n \n int helper(int i, int j, vector<int> &val) {\n if(i>... | 2 | 0 | [] | 1 |
stone-game-v | memoization c++ solutions | memoization-c-solutions-by-atulkumar1-pdj6 | \n \n int solve(vector<int>& a,int s,int e, vector<vector<int>>&dp)\n {\n if(s>e)\n {\n return 0; \n }\n \n | ATulKumaR1 | NORMAL | 2020-08-23T05:39:23.603669+00:00 | 2020-08-23T05:39:23.603700+00:00 | 91 | false | ```\n \n int solve(vector<int>& a,int s,int e, vector<vector<int>>&dp)\n {\n if(s>e)\n {\n return 0; \n }\n \n if(dp[s][e]!=-1)\n {\n return dp[s][e]; \n }\n \n int i,j,k,l; \n int sum=0;\n int ans=0; \n \... | 2 | 0 | [] | 0 |
stone-game-v | [Java] DFS + dp memo | java-dfs-dp-memo-by-marvinbai-idd7 | \nclass Solution {\n public int stoneGameV(int[] sv) {\n int n = sv.length;\n int[][] dp = new int[n + 1][n + 1];\n int[] sum = new int[ | marvinbai | NORMAL | 2020-08-23T04:11:57.546449+00:00 | 2020-08-23T04:19:59.254265+00:00 | 217 | false | ```\nclass Solution {\n public int stoneGameV(int[] sv) {\n int n = sv.length;\n int[][] dp = new int[n + 1][n + 1];\n int[] sum = new int[n + 1];\n for(int i = 1; i <= n; i++) {\n sum[i] = sv[i - 1] + sum[i - 1];\n } \n return dfs(sv, 1, n, dp, sum);\n ... | 2 | 0 | [] | 0 |
stone-game-v | [C++] Top Down DP + prefix sum solution. | c-top-down-dp-prefix-sum-solution-by-che-ee0f | \ndp[i][j] represent the maximum value from i to j. We try to spilt the array to two part left and right, There are 3 situation we need to cosider: \n1. if the | chejianchao | NORMAL | 2020-08-23T04:07:50.980553+00:00 | 2020-08-23T04:11:38.277957+00:00 | 148 | false | \ndp[i][j] represent the maximum value from i to j. We try to spilt the array to two part `left` and `right`, There are 3 situation we need to cosider: \n1. if the sum of `left` is less than `right` part, then we do dfs(l, i); \n2. if the sum of `left` is greater than `right` part, then we do dfs(l, i + 1, r); \n3. ... | 2 | 0 | [] | 0 |
stone-game-v | Python, elegant memoization | python-elegant-memoization-by-warmr0bot-eyj8 | \nfrom functools import lru_cache\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n prefix = [0]\n for i in range(len(st | warmr0bot | NORMAL | 2020-08-23T04:02:50.189289+00:00 | 2020-08-23T04:05:22.584943+00:00 | 456 | false | ```\nfrom functools import lru_cache\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n prefix = [0]\n for i in range(len(stoneValue)):\n prefix += [prefix[-1] + stoneValue[i]]\n print(prefix)\n \n @lru_cache(None)\n def dfs(left=0, right=len... | 2 | 0 | ['Memoization', 'Python'] | 0 |
stone-game-v | Python DFS + Memo | python-dfs-memo-by-tomzy-bshx | \nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n n = len(stoneValue)\n pre = [0]\n for i in range(n):\n | tomzy | NORMAL | 2020-08-23T04:02:36.749536+00:00 | 2020-08-23T04:27:00.863765+00:00 | 538 | false | ```\nclass Solution:\n def stoneGameV(self, stoneValue: List[int]) -> int:\n n = len(stoneValue)\n pre = [0]\n for i in range(n):\n pre.append(stoneValue[i] + pre[-1])\n @lru_cache(None)\n def dfs(l, r):\n if r <= l:\n return 0\n res ... | 2 | 0 | ['Depth-First Search', 'Memoization', 'Python', 'Python3'] | 1 |
stone-game-v | Simple Java memoization solution | simple-java-memoization-solution-by-hee_-qs7z | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | hee_maan_shee | NORMAL | 2025-03-23T19:43:09.282047+00:00 | 2025-03-23T19:43:09.282047+00:00 | 8 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 1 | 0 | ['Dynamic Programming', 'Java'] | 0 |
stone-game-v | Easy to Understand || Memoization || DP (Top Down) | easy-to-understand-memoization-dp-top-do-krva | Intuition\nThe problem at hand requires finding the maximum score a player can achieve given a series of stone piles where the player can choose to split the ar | tinku_tries | NORMAL | 2024-06-03T04:43:18.755492+00:00 | 2024-06-03T04:43:18.755511+00:00 | 344 | false | # Intuition\nThe problem at hand requires finding the maximum score a player can achieve given a series of stone piles where the player can choose to split the array into two parts. The score is determined by the sum of stones in the smaller segment. To solve this, we can use dynamic programming to efficiently compute ... | 1 | 0 | ['Array', 'Math', 'Dynamic Programming', 'Game Theory', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.