A 4/default.nix => 4/default.nix +15 -0
@@ 0,0 1,15 @@
+{ stdenv, rustc }:
+
+stdenv.mkDerivation {
+ pname = "leetcode-cn_2181";
+ version = "0.0.1";
+
+ src = ./.;
+
+ nativeBuildInputs = [ rustc ];
+
+ buildPhase = ''
+ mkdir -p $out/bin
+ rustc --out-dir $out/bin ./solution.rs
+ '';
+}
A 4/flake.lock => 4/flake.lock +27 -0
@@ 0,0 1,27 @@
+{
+ "nodes": {
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1649117019,
+ "narHash": "sha256-ID7nw/8MDgqj/cbJ0wy6AtQ9wp58hSnE6+weZwuHnso=",
+ "owner": "NixOS",
+ "repo": "nixpkgs",
+ "rev": "ccb90fb9e11459aeaf83cc28d5f8910816d90dd0",
+ "type": "github"
+ },
+ "original": {
+ "owner": "NixOS",
+ "ref": "nixos-21.11",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "root": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
A 4/flake.nix => 4/flake.nix +14 -0
@@ 0,0 1,14 @@
+{
+ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-21.11";
+ outputs = { self, nixpkgs }:
+ let
+ system = "x86_64-linux";
+ pkgs = nixpkgs.legacyPackages.${system};
+ in rec {
+ defaultPackage.${system} = pkgs.callPackage ./. { };
+ devShell.${system} = with pkgs; mkShell {
+ inputsFrom = [ defaultPackage.${system} ];
+ buildInputs = [ rust-analyzer ];
+ };
+ };
+}
A 4/solution.rs => 4/solution.rs +67 -0
@@ 0,0 1,67 @@
+use std::cmp::{max, min};
+
+pub struct Solution {
+ inputs: Vec<(Vec<i32>, Vec<i32>)>
+}
+
+impl Solution {
+ pub fn find_median_sorted_arrays(nums1: Vec<i32>, nums2: Vec<i32>) -> f64 {
+ let len1 = nums1.len();
+ let len2 = nums2.len();
+ if len1 > len2 {
+ Solution::find_median_sorted_arrays(nums2, nums1)
+ } else {
+ let mut left = 0;
+ let mut right = len1;
+
+ let mut left_max = 0;
+ let mut right_min = 0;
+
+ while left <= right {
+ let i: usize = (left + right) / 2;
+ let j: usize = (len1 + len2 + 1) / 2 - i;
+
+ let nums_li = if i == 0 { i32::MIN } else { nums1[i - 1] };
+ let nums_ri = if i == len1 { i32::MAX } else { nums1[i] };
+ let nums_lj = if j == 0 { i32::MIN } else { nums2[j- 1] };
+ let nums_rj = if j == len2 { i32::MAX } else { nums2[j] };
+
+ if nums_li <= nums_rj {
+ left_max = max(nums_li, nums_lj);
+ right_min = min(nums_ri, nums_rj);
+ left = i + 1;
+ } else {
+ right = i - 1;
+ }
+ }
+ let left_f = left_max as f64;
+ let right_f = right_min as f64;
+ if (len1 + len2) % 2 == 0 { (left_f + right_f) / 2.0 } else { left_f }
+ }
+ }
+}
+
+fn main() {
+ let sln = Solution {
+ inputs: vec![(vec![1, 2], vec![3]), (vec![1,2,4], vec![1,3,5]),
+ (vec![1, 2], vec![3,4])],
+ };
+
+ for input in sln.inputs {
+ print!("input: ");
+ print!("[");
+ for x in &input.0 {
+ print!(" {}", x)
+ }
+ print!(" ], ");
+ print!("[");
+ for x in &input.1 {
+ print!(" {}", x)
+ }
+ println!(" ]");
+
+ let output = Solution::find_median_sorted_arrays(input.0, input.1);
+
+ println!("output: {}", output);
+ }
+}