Skip to content

Instantly share code, notes, and snippets.

View ttsugriy's full-sized avatar

Taras Tsugrii ttsugriy

  • Meta
  • Seattle, WA
View GitHub Profile
@ttsugriy
ttsugriy / min_moves.cpp
Created December 19, 2016 05:16
Minimum moves to equal array.
int minMoves(const vector& nums) {
int base = *min_element(nums.cbegin(), nums.cend());
return accumulate(nums.cbegin(), nums.cend(), 0) - base * nums.size();
}
int minMoves(vector<int>& nums) {
int base = numeric_limits<int>::max();
int sum = 0;
for (int i = 0; i < nums.size(); ++i) {
sum += nums[i];
base = min(base, nums[i]);
}
return sum - base * nums.size();
}
@ttsugriy
ttsugriy / vectorized_addition.S
Created December 19, 2016 06:47
Vectorized element summation.
movdqa xmm1, xmm0
psrldq xmm1, 8
paddd xmm0, xmm1
movdqa xmm1, xmm0
psrldq xmm1, 4
paddd xmm0, xmm1
@ttsugriy
ttsugriy / skylark-lang.patch
Created August 15, 2017 18:46
Patch to turn skylark-lang into java_binary.
diff --git a/src/main/java/com/google/devtools/build/lib/BUILD b/src/main/java/com/google/devtools/build/lib/BUILD
index d4e18ecf8..0beac7230 100644
--- a/src/main/java/com/google/devtools/build/lib/BUILD
+++ b/src/main/java/com/google/devtools/build/lib/BUILD
@@ -434,14 +434,34 @@ java_library(
#
# IMPORTANT: NOT A PUBLIC INTERFACE. TARGETS SHOULDN'T DEPEND ON THIS.
#
-java_library(
+java_binary(
@ttsugriy
ttsugriy / MyBenchmark.java
Created July 25, 2018 01:07
Dynamic proxy vs plain getter JMH benchmark.
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
diff --git a/rules/attributes/printer.bzl b/rules/attributes/printer.bzl
index 1a7abf1..facd739 100644
--- a/rules/attributes/printer.bzl
+++ b/rules/attributes/printer.bzl
@@ -18,8 +18,8 @@ def _impl(ctx):
printer = rule(
implementation = _impl,
attrs = {
+ "deps": attr.label_list(allow_files = True),
# Do not declare "name": It is added automatically.
impl Solution {
pub fn is_valid(s: String) -> bool {
let mut stack = Vec::new();
let match_of = |c: char| -> char {
match c {
')' => '(',
'}' => '{',
']' => '[',
_ => panic!("Invalid closing bracket"),
}
@ttsugriy
ttsugriy / intersection_of_two_linked_lists.cpp
Created January 1, 2019 05:21
intersection-of-two-linked-lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
@ttsugriy
ttsugriy / maximum_width_ramp.cpp
Created January 4, 2019 03:53
Maximum width ramp (leetcode 962)
class Solution {
public:
int maxWidthRamp(vector<int>& A) {
map<int, int> largest;
largest[A.back()] = A.size()-1;
int max_width = 0;
for (int i = A.size()-2; i >= 0; --i) {
auto found = largest.lower_bound(A[i]);
if (found != largest.cend()) {
max_width = max(max_width, found->second - i);
impl Solution {
pub fn max_width_ramp(a: Vec<i32>) -> i32 {
let mut largest: Vec<usize> = vec![(a.len()-1) as usize];
for (i, val) in a.iter().enumerate().rev().skip(1) {
if *val > a[*largest.last().unwrap()] {
largest.push(i);
}
}
let mut max_width = 0;
for (i, val) in a.iter().enumerate() {