1
0

5 کامیت‌ها 1689bc7f9d ... 6bd54cdf74

نویسنده SHA1 پیام تاریخ
  Vinicius Teshima 6bd54cdf74 Removing PHP cause God knows that i'm not strong enough 1 ماه پیش
  Vinicius Teshima 556d6cc149 Adding solution for 965 1 ماه پیش
  Vinicius Teshima f97a87626a Adding solution for 2073 1 ماه پیش
  Vinicius Teshima 6f910d6f53 Adding solution for 67 1 ماه پیش
  Vinicius Teshima ee8b1a4611 Adding failed attempt at solving 1200 1 ماه پیش
5فایلهای تغییر یافته به همراه196 افزوده شده و 35 حذف شده
  1. 50 0
      C++/2073.cpp
  2. 122 0
      C/965.c
  3. 0 31
      PHP/2315.php
  4. 0 4
      PHP/build.sh
  5. 24 0
      Python/67.py

+ 50 - 0
C++/2073.cpp

@@ -0,0 +1,50 @@
+/* 2073. Time Needed to Buy Tickets */
+
+#include <iostream>
+#include <cstdint>
+#include <vector>
+
+using std::vector;
+
+class Solution {
+public:
+    int timeRequiredToBuy(vector<int>& tickets, int k) {
+        int time = 0;
+
+        int i = 0;
+        int limit = (int)tickets.size();
+        while ( true ) {
+            if ( i >= limit ) { i = 0; }
+
+            int cur = tickets[i];
+            if ( cur == 0 ) { ++i; continue; }
+            --cur;
+            ++time;
+            if ( i == k && cur == 0 ) { return time; }
+            tickets[i] = cur;
+            ++i;
+        }
+
+        return time;
+    }
+};
+
+void r(vector<int>& tickets, int k, int exp) {
+    std::cout << "Solution().timeRequiredToBuy([";
+    for ( uint64_t i = 0; i < tickets.size(); ++i ) {
+        if ( i == 0 ) { std::cout << tickets[i]; continue; }
+        std::cout << ", " << tickets[i];
+    }
+    std::cout << "], " << k << ") = " << Solution().timeRequiredToBuy(tickets, k) << " | exp: " << exp;
+    std::cout << "\n";
+};
+
+int main(void) {
+
+    vector<int> t1 = {2, 3, 2};
+    r(t1, 2, 6);
+    vector<int> t2 = {5, 1, 1, 1};
+    r(t2, 0, 8);
+
+    return 0;
+}

+ 122 - 0
C/965.c

@@ -0,0 +1,122 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+struct TreeNode {
+    int val;
+    struct TreeNode *left;
+    struct TreeNode *right;
+};
+
+/**
+ * Definition for a binary tree node.
+ * struct TreeNode {
+ *     int val;
+ *     struct TreeNode *left;
+ *     struct TreeNode *right;
+ * };
+ */
+bool isUnivalTree(struct TreeNode *root) {
+    int v = root->val;
+
+    struct TreeNode *nodes[124] = {0};
+    int size = 0;
+
+    if ( root->left != NULL ) nodes[size++] = root->left;
+    if ( root->right != NULL ) nodes[size++] = root->right;
+
+    for ( int i = 0; i < size; ++i ) {
+        struct TreeNode *r = nodes[i];
+        if ( r->val != v ) { return false; }
+        if ( r->left != NULL ) nodes[size++] = r->left;
+        if ( r->right != NULL ) nodes[size++] = r->right;
+    }
+
+    return true;
+}
+
+char *
+treenode_to_cstr(const struct TreeNode *root)
+{
+    char *data = 0;
+    unsigned long cap = 1024;
+    unsigned long size = 0;
+
+    if ( root == NULL ) {
+        char *data = calloc(4, sizeof(char));
+        return memcpy(data, "nil", 3);
+    }
+
+    data = calloc(cap, sizeof(*data));
+
+    // TreeNode(%d, R, R)
+    data[size++] = 'T';
+    data[size++] = 'r';
+    data[size++] = 'e';
+    data[size++] = 'e';
+    data[size++] = 'N';
+    data[size++] = 'o';
+    data[size++] = 'd';
+    data[size++] = 'e';
+    data[size++] = '(';
+    if ( root->val > 99 ) { data[size++] = ((root->val / 100) % 10) + '0'; }
+    if ( root->val > 9 ) { data[size++] = ((root->val / 10) % 10) + '0'; }
+    data[size++] = (root->val % 10) + '0';
+    data[size++] = ',';
+    data[size++] = ' ';
+
+    char *l = treenode_to_cstr(root->left);
+    unsigned long lsize = strlen(l);
+    if ( size+lsize >= cap ) {
+        while (size+lsize >= cap) cap *= 2;
+        data = realloc(data, cap);
+    }
+    for ( unsigned int i = 0; i < lsize; ++i ) { data[size++] = l[i]; }
+    free(l);
+    data[size++] = ',';
+    data[size++] = ' ';
+
+    char *r = treenode_to_cstr(root->right);
+    unsigned long rsize = strlen(r);
+    if ( size+rsize >= cap ) {
+        while (size+rsize >= cap) cap *= 2;
+        data = realloc(data, cap);
+    }
+    for ( unsigned int i = 0; i < rsize; ++i ) { data[size++] = r[i]; }
+    free(r);
+
+    data[size++] = ')';
+    return data;
+}
+
+
+void
+r(struct TreeNode *root, bool exp)
+{
+
+    char *root_cstr = treenode_to_cstr(root);
+    printf("isUnivalTree(%s) = %b | exp: %b\n", root_cstr, isUnivalTree(root), exp);
+    free(root_cstr);
+}
+
+int
+main(void)
+{
+    struct TreeNode t1ll = {1, NULL, NULL};
+    struct TreeNode t1lr = {1, NULL, NULL};
+    struct TreeNode t1l = {1, &t1ll, &t1lr};
+    struct TreeNode t1rr = {1, NULL, NULL};
+    struct TreeNode t1r = {1, NULL, &t1rr};
+    struct TreeNode t1 = {1, &t1l, &t1r};
+    r(&t1, true);
+
+    struct TreeNode t2ll = {5, NULL, NULL};
+    struct TreeNode t2lr = {2, NULL, NULL};
+    struct TreeNode t2l = {2, &t2ll, &t2lr};
+    struct TreeNode t2r = {2, NULL, NULL};
+    struct TreeNode t2 = {2, &t2l, &t2r};
+    r(&t2, false);
+
+    return 0;
+}

+ 0 - 31
PHP/2315.php

@@ -1,31 +0,0 @@
-<?php
-
-class Solution {
-    /**
-     * @param String $s
-     * @return Integer
-     */
-    function countAsterisks($s) {
-        $in = false;
-        $count = 0;
-        foreach ( str_split($s) as $c ) {
-            if ( $c == '|' ) { $in = !$in; }
-            if ( $in == false && $c == '*' ) { ++$count; }
-        }
-        return $count;
-    }
-}
-
-function main() {
-    function r($s) {
-        $S = new Solution;
-        $t = $S->countAsterisks($s);
-        echo "Solution().countAsterisks(\"$s\") = $t\n";
-    }
-
-    r("l|*e*et|c**o|*de|");
-    r("iamprogrammer");
-    r("yo|uar|e**|b|e***au|tifu|l");
-}
-
-main();

+ 0 - 4
PHP/build.sh

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

+ 24 - 0
Python/67.py

@@ -0,0 +1,24 @@
+# 67. Add Binary
+
+import sys
+
+class Solution:
+    def addBinary(self, a: str, b: str) -> str:
+        return str(bin(int(a, 2) + int(b, 2)))[2:]
+    pass
+
+
+def main() -> int:
+    def r(a: str, b: str, exp: str) -> None:
+        print(
+            f"Solution().addBinary({a}, {b}) = {Solution().addBinary(a, b)} | exp: {exp}"
+        )
+        pass
+
+    r('11', '1', '100')
+    r('1010', '1011', '10101')
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())