aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-12-30 22:28:47 +0100
committerJack Sangdahl <[email protected]>2024-12-30 22:28:47 +0100
commite8c6049f79e0102afa0917cca08062232d465650 (patch)
tree6c23439498b177d662e7f55cfbbdb850e9d4005c
parent00d43ad8605d97544c17cb1775ca884ef0857869 (diff)
downloadleetcode-e8c6049f79e0102afa0917cca08062232d465650.tar.gz
leetcode-e8c6049f79e0102afa0917cca08062232d465650.zip
384. shuffle an array
-rw-r--r--shuffle_an_array.c69
-rw-r--r--shuffle_an_array.py22
-rw-r--r--shuffle_the_array.c (renamed from shuffle_array.c)0
-rw-r--r--shuffle_the_array.cpp (renamed from shuffle_array.cpp)0
4 files changed, 91 insertions, 0 deletions
diff --git a/shuffle_an_array.c b/shuffle_an_array.c
new file mode 100644
index 0000000..54a0ddd
--- /dev/null
+++ b/shuffle_an_array.c
@@ -0,0 +1,69 @@
+// 384. Shuffle an Array
+#include "leetcode.h"
+
+/*
+ * given an integer array 'nums', design an algorithm to randomly shuffle the
+ * array. all permutations of the array should be equally likely as a result of
+ * the shuffling. implement the 'Solution' class: 'Solution(int[] nums)'
+ * initialises the object with the integer array 'nums' 'int[] reset()' resets
+ * the array to its original configuration and returns it 'int[] shuffle()'
+ * return a random shuffling of the array
+ */
+
+typedef struct {
+ int *original;
+ int *nums;
+ int size;
+} Solution;
+
+Solution *solutionCreate(int *nums, int numsSize) {
+ Solution *obj = (Solution *)malloc(sizeof(Solution));
+ obj->original = (int *)malloc(numsSize * sizeof(int));
+ obj->nums = (int *)malloc(numsSize * sizeof(int));
+ obj->size = numsSize;
+ memcpy(obj->original, nums, numsSize * sizeof(int));
+ memcpy(obj->nums, nums, numsSize * sizeof(int));
+ return obj;
+}
+
+int *solutionReset(Solution *obj, int *retSize) {
+ *retSize = obj->size;
+ memcpy(obj->nums, obj->original, obj->size * sizeof(int));
+ return obj->original;
+}
+
+int *solutionShuffle(Solution *obj, int *retSize) {
+ *retSize = obj->size;
+ for (int i = 0; i < obj->size; i++) {
+ int tmp = obj->nums[i];
+ int pos = rand() % obj->size;
+ obj->nums[i] = obj->nums[pos];
+ obj->nums[pos] = tmp;
+ }
+ return obj->nums;
+}
+
+void solutionFree(Solution *obj) {
+ free(obj->nums);
+ free(obj->original);
+ free(obj);
+}
+
+int main() {
+ int n1[] = {1, 2, 3}, rs1, rs2, rs3;
+ Solution *obj = solutionCreate(n1, ARRAY_SIZE(n1));
+ int *ss1 = solutionShuffle(obj, &rs1);
+ int *sr1 = solutionReset(obj, &rs2);
+ int *ss2 = solutionShuffle(obj, &rs3);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", ss1[i]); // expect: 3 1 2
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", sr1[i]); // expect: 1 2 3
+ printf("\n");
+ for (int i = 0; i < rs3; i++)
+ printf("%d ", ss2[i]); // expect: 1 3 2
+ printf("\n");
+ free(ss1), free(sr1), free(ss2);
+ solutionFree(obj);
+}
diff --git a/shuffle_an_array.py b/shuffle_an_array.py
new file mode 100644
index 0000000..5b94f3c
--- /dev/null
+++ b/shuffle_an_array.py
@@ -0,0 +1,22 @@
+# 384. Shuffle an Array
+from random import sample
+
+"""
+given an integer array 'nums', design an algorithm to randomly shuffle the
+array. all permutations of the array should be equally likely as a result of
+the shuffling. implement the 'Solution' class: 'Solution(int[] nums)'
+initialises the object with the integer array 'nums' 'int[] reset()' resets
+the array to its original configuration and returns it 'int[] shuffle()'
+return a random shuffling of the array
+"""
+
+
+class Solution(list):
+ reset, shuffle = list, lambda x: sample(x, len(x))
+
+
+if __name__ == "__main__":
+ obj = Solution([1, 2, 3])
+ print(obj.shuffle())
+ print(obj.reset())
+ print(obj.shuffle())
diff --git a/shuffle_array.c b/shuffle_the_array.c
index 0192604..0192604 100644
--- a/shuffle_array.c
+++ b/shuffle_the_array.c
diff --git a/shuffle_array.cpp b/shuffle_the_array.cpp
index f7dddea..f7dddea 100644
--- a/shuffle_array.cpp
+++ b/shuffle_the_array.cpp