|
Download cp/cp-10026.jsonl from winapiadmin/tinycodinglm: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/cp/cp-10026.jsonl
- Command line
-
hf download hf://datasets/winapiadmin/tinycodinglm/cp/cp-10026.jsonl
-
curl -L -o cp-10026.jsonl https://huggingface.co/datasets/winapiadmin/tinycodinglm/resolve/main/cp/cp-10026.jsonl
2.7 kB
| {"type": "session", "harness": "opencode", "id": "cp-10026", "name": "LeetCode 0519: Random Flip Matrix"} | |
| {"type": "message", "message": {"role": "system", "content": "You are an expert LeetCode problem solver. You solve problems efficiently with clean code."}} | |
| {"type": "message", "message": {"role": "user", "content": "Problem: Random Flip Matrix (Difficulty: Medium)\nTags: Hash Table, Math, Reservoir Sampling, Randomized\n\nThere is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned.\nOptimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity.\nImplement the Solution class:\n\nExample 1:\nInput\n[\"Solution\", \"flip\", \"flip\", \"flip\", \"reset\", \"flip\"]\n[[3, 1], [], [], [], [], []]\nOutput\n[null, [1, 0], [2, 0], [0, 0], null, [2, 0]]\n\nExplanation\nSolution solution = new Solution(3, 1);\nsolution.flip(); // return [1, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.\nsolution.flip(); // return [2, 0], Since [1,0] was returned, [2,0] and [0,0]\nsolution.flip(); // return [0, 0], Based on the previously returned indices, only [0,0] can be returned.\nsolution.reset(); // All the values are reset to 0 and can be returned.\nsolution.flip(); // return [2, 0], [0,0], [1,0], and [2,0] should be equally likely to be returned.\n\nConstraints:\n- 1 <= m, n <= 10^4\n- There will be at least one free cell for each call to flip.\n- At most 1000 calls will be made to flip and reset."}} | |
| {"type": "message", "message": {"role": "assistant", "content": "# Time: ctor: O(1)\n# flip: O(1)\n# reset: O(min(f, r * c))\n# Space: O(min(f, r * c))\n\nimport random\n\n\nclass Solution(object):\n\n def __init__(self, n_rows, n_cols):\n \"\"\"\n :type n_rows: int\n :type n_cols: int\n \"\"\"\n self.__n_rows = n_rows\n self.__n_cols = n_cols\n self.__n = n_rows*n_cols\n self.__lookup = {}\n \n\n def flip(self):\n \"\"\"\n :rtype: List[int]\n \"\"\"\n self.__n -= 1\n target = random.randint(0, self.__n)\n x = self.__lookup.get(target, target)\n self.__lookup[target] = self.__lookup.get(self.__n, self.__n)\n return divmod(x, self.__n_cols)\n \n\n def reset(self):\n \"\"\"\n :rtype: void\n \"\"\"\n self.__n = self.__n_rows*self.__n_cols\n self.__lookup = {}\n \n\n\n"}} | |