Jelajahi Sumber

Adding solution for 2006

Vinicius Teshima 6 hari lalu
induk
melakukan
732dcaccdd
3 mengubah file dengan 44 tambahan dan 0 penghapusan
  1. 1 0
      .python-version
  2. 36 0
      Python/2006.py
  3. 7 0
      Python/build.sh

+ 1 - 0
.python-version

@@ -0,0 +1 @@
+3.13

+ 36 - 0
Python/2006.py

@@ -0,0 +1,36 @@
+# 2006. Count Number of Pairs With Absolute Difference K
+
+import sys
+from typing import List
+
+
+class Solution:
+    def countKDifference(self, nums: List[int], k: int) -> int:
+        return sum(
+            len(
+                [
+                    1
+                    for j in range(len(nums))
+                    if i < j and abs(nums[i] - nums[j]) == k
+                ]
+            )
+            for i in range(len(nums))
+        )
+    pass
+
+
+def main() -> int:
+    def r(ns: List[int], k: int) -> None:
+        print(
+            f"Solution().countKDifference({ns}, {k}) = {Solution().countKDifference(ns, k)}"
+        )
+        pass
+
+    r([1, 2, 2, 1], 1)
+    r([1, 3], 3)
+    r([3, 2, 1, 5, 4], 2)
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())

+ 7 - 0
Python/build.sh

@@ -0,0 +1,7 @@
+#!/bin/sh
+
+newest_file="$(find ./ -type f -printf "%T@ %p\n" | sort -n | cut -d' ' -f 2- | tail -n 1)"
+
+set -x
+python $newest_file
+